How to use DateTime functions in the Expression

May 11, 2009 at 1:52 PM

Hi,

first of all thanks for such a wonderful application.

I want to use some functions from math, string,Datetime and DateAndTime this classes.

I have added them in the following manner.

context.Imports.AddType(typeof(Math));
context.Imports.AddType(typeof(bool));
context.Imports.AddType(typeof(DateTime));
context.Imports.AddType(typeof(String));
context.Imports.AddType(typeof(DateAndTime));

I had expression like

if(SomeDate<Now.AddDays(-30),cast(0.250*SomeEvaluatedExpression,double),0.0)

But I cant able to access Now.AddDays Method.

Please Help me to come out of this problem.

Thanx in Advance

regards

Amol

India

 

 

 

 

 

Coordinator
May 15, 2009 at 2:32 AM

I can compile and run the following expression:

        Dim context As New ExpressionContext()
        context.Imports.AddType(GetType(DateTime))
        context.Imports.AddType(GetType(Boolean))
        context.Imports.AddType(GetType(Math))
        context.Imports.AddType(GetType(String))

        context.Variables.Add("somedate", DateTime.Now)


        Dim e As IDynamicExpression = context.CompileDynamic("if(SomeDate>Now.AddDays(-30),cast(0.250*100,double),0.0)")       
        Dim result As Object = e.Evaluate()

You'll have to give me the error/exception details before I can narrow down the problem.

May 15, 2009 at 5:53 AM

Hi,

The Problem is solved. It was because the context.import statement was getting called more than once so the DateTime.Now property was becoming ambigous in the parser.