Eval function in VB .Net and C# 
Author Message
 Eval function in VB .Net and C#

Hi,

I have recently seen many questions about the lack of an equivalent to
the JScript eval() function in C# and VB .Net. This is a good
opportunity to demonstrate the cross-langauge capabilities of .Net

1) Create a file named CustomEval.js (File | New | File | Script) as
follows:

// JScript source code
class CustomEval
{
    static function eval(strExp)
    {
         return eval(strExp);
    }

Quote:
}

2) Compile this source code from the command line:

jsc /t:library CustomEval.js

which should produce CustomEval.dll

3) Reference this assembly from your VB .Net or C# project as well as
the Microsoft.JScript assembly.

4) Use the eval method of CustomEval as follows (since the method is
static, you don't need to instantiate CustomEval.

    result = CustomEval.eval("2+3")

Of course, I'd prefer having this capability implemented in the FCL. Or
did I miss it?

--
Patrick Philippot
MainSoft Consulting Services
www.mainsoft.xx
(replace .xx with .fr when replying by e-mail)



Sun, 11 Dec 2005 21:09:21 GMT  
 Eval function in VB .Net and C#
Patrick,

Could you please disassemble using ildasm.exe your resulting EXE and post
here the generated IL source code ?

That may give us the understanding about how it works from IL level.

many thanks,

Antonio



Sun, 11 Dec 2005 21:16:22 GMT  
 Eval function in VB .Net and C#
Patrick,

    It should be pointed out that this will only evaluate JavaScript code,
it will not evaluate C# and VB.NET code.

    Also, I don't know that this capability ^should^ be in the FCL (or CLR).
I mean, it is a compiled language, and if you look at a basic JS assembly,
there is a lot of work going on in there to allow evaluated expressions.
Personally, I am of the belief that whatever needs to be done can be done
with things such as abstract classes, interfaces, and good design.

--
               - Nicholas Paldino [.NET/C# MVP]


Quote:
> Hi,

> I have recently seen many questions about the lack of an equivalent to
> the JScript eval() function in C# and VB .Net. This is a good
> opportunity to demonstrate the cross-langauge capabilities of .Net

> 1) Create a file named CustomEval.js (File | New | File | Script) as
> follows:

> // JScript source code
> class CustomEval
> {
>     static function eval(strExp)
>     {
>          return eval(strExp);
>     }
> }

> 2) Compile this source code from the command line:

> jsc /t:library CustomEval.js

> which should produce CustomEval.dll

> 3) Reference this assembly from your VB .Net or C# project as well as
> the Microsoft.JScript assembly.

> 4) Use the eval method of CustomEval as follows (since the method is
> static, you don't need to instantiate CustomEval.

>     result = CustomEval.eval("2+3")

> Of course, I'd prefer having this capability implemented in the FCL. Or
> did I miss it?

> --
> Patrick Philippot
> MainSoft Consulting Services
> www.mainsoft.xx
> (replace .xx with .fr when replying by e-mail)



Sun, 11 Dec 2005 21:14:29 GMT  
 Eval function in VB .Net and C#

Quote:

> Could you please disassemble using ildasm.exe
> your resulting EXE and post here the generated
> IL source code ?

Antonio,

This is a lot of stuff to post. Since the project can be re-constructed
within two minutes, this can be avoided. You already have the source
code for the DLL (there's nothing more to add). Here is the code that
you can quickly add to a sample VB .Net program:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
    MsgBox(CustomEval.eval("2+2"))
End Sub

--
Patrick Philippot
MainSoft Consulting Services
www.mainsoft.xx
(replace .xx with .fr when replying by e-mail)



Sun, 11 Dec 2005 21:51:03 GMT  
 Eval function in VB .Net and C#

Quote:
> Patrick,

> It should be pointed out that this will only evaluate JavaScript
> code, it will not evaluate C# and VB.NET code.

Yes, I should have mentioned this. Thanks.

Quote:
> Also, I don't know that this capability ^should^ be in the FCL
> (or CLR). I mean, it is a compiled language, and if you look at a
> basic JS assembly, there is a lot of work going on in there to allow
> evaluated expressions. Personally, I am of the belief that whatever
> needs to be done can be done with things such as abstract classes,
> interfaces, and good design.

Agreed. I intended this as a demonstration of the cross language
capabilities of .Net rather than a demonstration of good programming
practice.

--
Patrick Philippot
MainSoft Consulting Services
www.mainsoft.xx
(replace .xx with .fr when replying by e-mail)



Sun, 11 Dec 2005 21:51:32 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Convert VB.Net to C#, convert C# to VB.Net

2. Eval Function in VB.NET

3. EVAL function in VB.NET - where is it?

4. Does VB.NET has Eval Function?

5. Eval Function in VB.NET

6. how to call C# function in VB.Net.

7. IntelliSense in VS.NET (C# versus VB.NET)

8. Tell me you experience in convert code from VB 6 to VB.NET or C#

9. VB to C# or C# to VB convertor

10. Eval() in VB.NET ??

11. JScript.Eval in VB.NET

12. Eval / Execute in vb.NET

 

 
Powered by phpBB® Forum Software