This is a migrated thread and some comments may be shown as answers.

Getting the Expression Text with Variable Substitution

4 Answers 195 Views
ExpressionEditor
This is a migrated thread and some comments may be shown as answers.
Gerald
Top achievements
Rank 1
Gerald asked on 23 Jun 2011, 05:13 PM
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

4 Answers, 1 is accepted

Sort by
0
Stefan Dobrev
Telerik team
answered on 24 Jun 2011, 12:05 PM
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
0
Gerald
Top achievements
Rank 1
answered on 24 Jun 2011, 02:38 PM
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.
0
Stefan Dobrev
Telerik team
answered on 27 Jun 2011, 02:21 PM
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
0
Joel Palmer
Top achievements
Rank 2
answered on 23 Oct 2012, 08:37 PM
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.


Tags
ExpressionEditor
Asked by
Gerald
Top achievements
Rank 1
Answers by
Stefan Dobrev
Telerik team
Gerald
Top achievements
Rank 1
Joel Palmer
Top achievements
Rank 2
Share this question
or