Getting the Expression Text with Variable Substitution

5 posts, 0 answers
  1. Gerald
    Gerald avatar
    6 posts
    Member since:
    Nov 2010

    Posted 23 Jun 2011 Link to this post

    Hi,

    I am looking for a way to get the Expression Text with the variables substituted with their values. For example, let's say that you have two variables A and B.
    A = 2
    B = 5
    Then let's say that the expression text is A*B. Is there a way to retrieve the expression text with the variables substituted? This should look like:
    2*5.

    I have given a simple an example but you can imagine that the expression can be much more complicated than this.

    Thanks,
    -Gerry
  2. Stefan Dobrev
    Admin
    Stefan Dobrev avatar
    790 posts

    Posted 24 Jun 2011 Link to this post

    Hi Gerald,

    Reading through your current requirement description it seems that you can do this with simple string replace operation, which I believe is not your case. Can you please provide more details information about your actual scenario?

    Best wishes,
    Stefan Dobrev
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. DevCraft banner
  4. Gerald
    Gerald avatar
    6 posts
    Member since:
    Nov 2010

    Posted 24 Jun 2011 Link to this post

    Here is an example. If you 3 variables:
    A
    B
    BA

    The you have this expression:
    A*B*BA

    You need to be careful about searching and replacing. You can't just do a search and repace on A. You would have to do the search and replace based on the length of the variable name. The other thing that I am worried about is if the user has variable with the same name as a function. For example:  A  *  BA  *   Max * Max(2,3) . In this case it would be difficult to distinguish between the "Max" variable and "Max" function.

    I guess this is doable I was just wondering if there is already code somewhere that does this that I could leverage.
  5. Stefan Dobrev
    Admin
    Stefan Dobrev avatar
    790 posts

    Posted 27 Jun 2011 Link to this post

    Hi Gerald,

    Although not trivial this can be achieved with RadExpressionEditor and some expression trees magic. I'm sending you a sample project that illustrates one possible implementation of such functionality. You can use it as a reference in your application.

    Hope this help,
    Stefan Dobrev
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  6. Joel Palmer
    Joel Palmer avatar
    164 posts
    Member since:
    May 2009

    Posted 23 Oct 2012 Link to this post

    I have something odd happening with the example you've given.  You have a DynamicObject.cs class that contains a GetValue<T> method:
    protected internal virtual T GetValue<T>(string propertyName)
    {
        object value;
        if (!this.valuesStorage.TryGetValue(propertyName, out value))
        {
            return default(T);
        }
     
        return (T)value;
    }

    When I add a field to the ExpressionEditor's window I see this method get called and it parses the value found in the Dictionary without issue.  This method is also called when I submit this code in your example:
    var evaluatedExpression = (LambdaExpression)
        Evaluator.PartialEval(bindedExpression, this.CanEvalPartially);
    Which calls this Evaluate method:
    private static Expression Evaluate(Expression e)
    {
        if (e.NodeType == ExpressionType.Constant)
        {
            return e;
        }
     
        LambdaExpression lambda = Expression.Lambda(e);
        Delegate fn = lambda.Compile();
     
        return Expression.Constant(fn.DynamicInvoke(null), e.GetType());
    }

    Problem is, when the GetValue method is called from this 2nd set of code I get an exception stating that it can't cast the given value to an integer data type.  It looks tastes and feels like an integer to me.

    Have you ever seen problems with a cast like this?  I can send my example project if you will look at this for me.  I'm using Visual Studio 2012, WPF 2012.2.912.40 on a Windows 7 machine. 

    Thanks for your help,
    Joel.


Back to Top
DevCraft banner