473,378 Members | 1,619 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Eval function vb.net

Hi,

is it possible to enter a string in code in VB.NET 2005 which will be
executed as a codeline during runtime?

i.e. I want to declare a new form depending on the string:
"Dim frm as new frmTest"
Thanx

Feb 20 '06 #1
13 2213
"Maurice" <hm*****@nospam.nospam> schrieb
Hi,

is it possible to enter a string in code in VB.NET 2005 which will
be executed as a codeline during runtime?

i.e. I want to declare a new form depending on the string:
"Dim frm as new frmTest"

This line is to be part of which method, class, namespace, assembly?
Maybe the answers to the 2 questions above help.

Or: (Framework 1.1)
http://msdn.microsoft.com/library/de...assemblies.asp
Armin

Feb 20 '06 #2
"Armin Zingler" <az*******@freenet.de> wrote in
news:uD**************@TK2MSFTNGP12.phx.gbl:
http://msdn.microsoft.com/library/de...y/en-us/cpguid
e/html/cpconemittingdynamicassemblies.asp


Hi Armin,

I have 2 different forms inside my project:
frmTest
frmCompany

I can declare a new form by using the code:
dim frm as new frmTest
OR
dim frm as new frmCompany

What I want is that when I have a string with the form name (strFormName) I
can use this string to declare the new form.

strEval = "Dim frm as new " & strFormName

Something like this. So the string above (strEval) will be executed as a
line of code.


Feb 20 '06 #3
"Maurice" <hm*****@nospam.nospam> schrieb
Hi Armin,

I have 2 different forms inside my project:
frmTest
frmCompany

I can declare a new form by using the code:
dim frm as new frmTest
OR
dim frm as new frmCompany

What I want is that when I have a string with the form name
(strFormName) I can use this string to declare the new form.

strEval = "Dim frm as new " & strFormName

Something like this. So the string above (strEval) will be executed
as a line of code.

Creating an object by class name has been asked three times in a row, that's
why I pointed to the previous two questions and answers.

Why do you have the class name in a string? Often this is not necessary:

Dim frm as form

select case value
case This
frm = new frmTest
case That
frm = new frmCompany
end select

frm.show
Armin

Feb 20 '06 #4
"Armin Zingler" <az*******@freenet.de> wrote in news:OrGv#YjNGHA.428
@tk2msftngp13.phx.gbl:
Creating an object by class name


Hi Armin,

sorry, indeed creating an object by class name.

It's possible that in future some names may change and I don;t want to
change this function then. That's why I don't want a select case statement
to declare a new form.

But isn't there something like the Eval function in VBScript that you can
use?

i.e.:
Dim intA as integer, intB as integer, intC as integer

intA = 5
intB = 10
Eval("intC = intA * intB")

The string value "intC = intA * intB" would then be executed as code and
intC would have the value 50.

Feb 20 '06 #5
CMM
Look into Activator.CreateInstance(). This has been asked before. Do a
search for more detailed examples.
--
-C. Moya
www.cmoya.com
"Maurice" <hm*****@nospam.nospam> wrote in message
news:Xn****************************@194.109.133.24 2...
"Armin Zingler" <az*******@freenet.de> wrote in
news:uD**************@TK2MSFTNGP12.phx.gbl:
http://msdn.microsoft.com/library/de...y/en-us/cpguid
e/html/cpconemittingdynamicassemblies.asp


Hi Armin,

I have 2 different forms inside my project:
frmTest
frmCompany

I can declare a new form by using the code:
dim frm as new frmTest
OR
dim frm as new frmCompany

What I want is that when I have a string with the form name (strFormName)
I
can use this string to declare the new form.

strEval = "Dim frm as new " & strFormName

Something like this. So the string above (strEval) will be executed as a
line of code.


Feb 20 '06 #6
"Maurice" <hm*****@nospam.nospam> schrieb
"Armin Zingler" <az*******@freenet.de> wrote in news:OrGv#YjNGHA.428
@tk2msftngp13.phx.gbl:
Creating an object by class name
Hi Armin,

sorry, indeed creating an object by class name.

It's possible that in future some names may change and I don;t want
to change this function then. That's why I don't want a select case
statement to declare a new form.


A class name usually doesn't change, and if it changes we have to change the
code. If you change a variable name you also have to change the code
accessing the variable. Reflection is not a replacement for this.
But isn't there something like the Eval function in VBScript that
you can use?


No.

VB.Net is not an interpreter. Source code is compiled into an executable
(IL-Code), then to native assembler code by the JIT-Compiler. Source code
has a structure. Executable commands are part of procedures. Procedures are
members of classes, and classes are contained in assemblies. That's the
structure of an application. You can not place a string into nowhere without
any context.

If you dynamically want to create an application, you must supply this
structure. That's why I provided the link in my first message, but I was
actually looking for this one:
http://msdn.microsoft.com/library/en...elanguages.asp
Armin

Feb 20 '06 #7
"Maurice" <hm*****@nospam.nospam> schrieb:
is it possible to enter a string in code in VB.NET 2005 which will be
executed as a codeline during runtime?

i.e. I want to declare a new form depending on the string:
"Dim frm as new frmTest"


Build a Custom .NET "EVAL" Provider
<URL:http://www.eggheadcafe.com/articles/20030908.asp>

Runtime Compilation (A .NET eval statement)
<URL:http://www.codeproject.com/dotnet/evaluator.asp>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Feb 20 '06 #8
Hi Maurice

Here is something I use

<code>
Public NotInheritable Class Evaluator

Private Shared WithEvents m_ScriptControl As
MSScriptControl.ScriptControlClass

Shared Sub New()

m_ScriptControl = New MSScriptControl.ScriptControlClass

m_ScriptControl.Language = "VBScript"
m_ScriptControl.AllowUI = False

End Sub

Public Shared Function Evaluate(ByVal s As String, ByVal decimalPlaces
As Integer) As Double

Return Decimal.Round(Convert.ToDecimal(Evaluate(s)), decimalPlaces)

End Function

Public Shared Function Evaluate(ByVal s As String) As Double

Dim r As Double

m_ScriptControl.Reset()

' Example of executing code
''m_ScriptControl.ExecuteStatement("Dim m")
''m_ScriptControl.ExecuteStatement("m = 5")

' Example of manipulating a form
''m_ScriptControl.AddObject("DotnetForm", Me)
''m_ScriptControl.ExecuteStatement("dotnetform.wid th =
dotnetform.width + 10")

Try
r = CDbl(m_ScriptControl.Eval(s))

Catch ex As Exception
Dim ee As EvaluationException

ee = New EvaluationException(ex.Message)

ee.Description = m_ScriptControl.Error.Description
ee.Column = m_ScriptControl.Error.Column
ee.Number = m_ScriptControl.Error.Number
ee.Source = m_ScriptControl.Error.Source
ee.Text = m_ScriptControl.Error.Text

Throw ee

End Try

Return r

End Function

End Class
</code>

I call Evaluator.Evaluate(...) with a string that I want to evaluate,
optionally with a number of decimal places. EvaluationException is my custom
exception class, but you could make up your own.

HTH

Charles
"Maurice" <hm*****@nospam.nospam> wrote in message
news:Xn****************************@194.109.133.24 2...
"Armin Zingler" <az*******@freenet.de> wrote in news:OrGv#YjNGHA.428
@tk2msftngp13.phx.gbl:
Creating an object by class name


Hi Armin,

sorry, indeed creating an object by class name.

It's possible that in future some names may change and I don;t want to
change this function then. That's why I don't want a select case statement
to declare a new form.

But isn't there something like the Eval function in VBScript that you can
use?

i.e.:
Dim intA as integer, intB as integer, intC as integer

intA = 5
intB = 10
Eval("intC = intA * intB")

The string value "intC = intA * intB" would then be executed as code and
intC would have the value 50.

Feb 22 '06 #9
Charles,

Do you mind if I test your class and than set it on our (VB-Tips) website,
this was in my opinion as well the way to go and I was intended to try it
some day.

(With telling who it made of course)

Cor

"Charles Law" <bl***@nowhere.com> schreef in bericht
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hi Maurice

Here is something I use

<code>
Public NotInheritable Class Evaluator

Private Shared WithEvents m_ScriptControl As
MSScriptControl.ScriptControlClass

Shared Sub New()

m_ScriptControl = New MSScriptControl.ScriptControlClass

m_ScriptControl.Language = "VBScript"
m_ScriptControl.AllowUI = False

End Sub

Public Shared Function Evaluate(ByVal s As String, ByVal decimalPlaces
As Integer) As Double

Return Decimal.Round(Convert.ToDecimal(Evaluate(s)), decimalPlaces)

End Function

Public Shared Function Evaluate(ByVal s As String) As Double

Dim r As Double

m_ScriptControl.Reset()

' Example of executing code
''m_ScriptControl.ExecuteStatement("Dim m")
''m_ScriptControl.ExecuteStatement("m = 5")

' Example of manipulating a form
''m_ScriptControl.AddObject("DotnetForm", Me)
''m_ScriptControl.ExecuteStatement("dotnetform.wid th =
dotnetform.width + 10")

Try
r = CDbl(m_ScriptControl.Eval(s))

Catch ex As Exception
Dim ee As EvaluationException

ee = New EvaluationException(ex.Message)

ee.Description = m_ScriptControl.Error.Description
ee.Column = m_ScriptControl.Error.Column
ee.Number = m_ScriptControl.Error.Number
ee.Source = m_ScriptControl.Error.Source
ee.Text = m_ScriptControl.Error.Text

Throw ee

End Try

Return r

End Function

End Class
</code>

I call Evaluator.Evaluate(...) with a string that I want to evaluate,
optionally with a number of decimal places. EvaluationException is my
custom exception class, but you could make up your own.

HTH

Charles
"Maurice" <hm*****@nospam.nospam> wrote in message
news:Xn****************************@194.109.133.24 2...
"Armin Zingler" <az*******@freenet.de> wrote in news:OrGv#YjNGHA.428
@tk2msftngp13.phx.gbl:
Creating an object by class name


Hi Armin,

sorry, indeed creating an object by class name.

It's possible that in future some names may change and I don;t want to
change this function then. That's why I don't want a select case
statement
to declare a new form.

But isn't there something like the Eval function in VBScript that you can
use?

i.e.:
Dim intA as integer, intB as integer, intC as integer

intA = 5
intB = 10
Eval("intC = intA * intB")

The string value "intC = intA * intB" would then be executed as code and
intC would have the value 50.


Feb 22 '06 #10
Hi Cor

Please feel free. I'd be honoured :-)

Charles
"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:ui**************@TK2MSFTNGP10.phx.gbl...
Charles,

Do you mind if I test your class and than set it on our (VB-Tips) website,
this was in my opinion as well the way to go and I was intended to try it
some day.

(With telling who it made of course)

Cor

"Charles Law" <bl***@nowhere.com> schreef in bericht
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hi Maurice

Here is something I use

<code>
Public NotInheritable Class Evaluator

Private Shared WithEvents m_ScriptControl As
MSScriptControl.ScriptControlClass

Shared Sub New()

m_ScriptControl = New MSScriptControl.ScriptControlClass

m_ScriptControl.Language = "VBScript"
m_ScriptControl.AllowUI = False

End Sub

Public Shared Function Evaluate(ByVal s As String, ByVal decimalPlaces
As Integer) As Double

Return Decimal.Round(Convert.ToDecimal(Evaluate(s)),
decimalPlaces)

End Function

Public Shared Function Evaluate(ByVal s As String) As Double

Dim r As Double

m_ScriptControl.Reset()

' Example of executing code
''m_ScriptControl.ExecuteStatement("Dim m")
''m_ScriptControl.ExecuteStatement("m = 5")

' Example of manipulating a form
''m_ScriptControl.AddObject("DotnetForm", Me)
''m_ScriptControl.ExecuteStatement("dotnetform.wid th =
dotnetform.width + 10")

Try
r = CDbl(m_ScriptControl.Eval(s))

Catch ex As Exception
Dim ee As EvaluationException

ee = New EvaluationException(ex.Message)

ee.Description = m_ScriptControl.Error.Description
ee.Column = m_ScriptControl.Error.Column
ee.Number = m_ScriptControl.Error.Number
ee.Source = m_ScriptControl.Error.Source
ee.Text = m_ScriptControl.Error.Text

Throw ee

End Try

Return r

End Function

End Class
</code>

I call Evaluator.Evaluate(...) with a string that I want to evaluate,
optionally with a number of decimal places. EvaluationException is my
custom exception class, but you could make up your own.

HTH

Charles
"Maurice" <hm*****@nospam.nospam> wrote in message
news:Xn****************************@194.109.133.24 2...
"Armin Zingler" <az*******@freenet.de> wrote in news:OrGv#YjNGHA.428
@tk2msftngp13.phx.gbl:

Creating an object by class name

Hi Armin,

sorry, indeed creating an object by class name.

It's possible that in future some names may change and I don;t want to
change this function then. That's why I don't want a select case
statement
to declare a new form.

But isn't there something like the Eval function in VBScript that you
can
use?

i.e.:
Dim intA as integer, intB as integer, intC as integer

intA = 5
intB = 10
Eval("intC = intA * intB")

The string value "intC = intA * intB" would then be executed as code and
intC would have the value 50.



Feb 22 '06 #11
Charles,

I made it a little bit more simple as sample. Do you agree with this?

http://www.vb-tips.com/default.aspx?...1-2b03e1a439ae
Cor
Feb 22 '06 #12
Hi Cor

Yes, indeed. It is neat enough to demonstrate the idea, and people can add
frills as they wish.

Charles
"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:ua**************@TK2MSFTNGP14.phx.gbl...
Charles,

I made it a little bit more simple as sample. Do you agree with this?

http://www.vb-tips.com/default.aspx?...1-2b03e1a439ae
Cor

Feb 22 '06 #13
"Cor Ligthert [MVP]" <no************@planet.nl> wrote in
news:ua**************@TK2MSFTNGP14.phx.gbl:
Charles,

I made it a little bit more simple as sample. Do you agree with this?

http://www.vb-tips.com/default.aspx?...-86d1-2b03e1a4
39ae
Cor


Hi Charles, Cor,

thanks for the good support!

I will try this one out.
Maurice
Feb 23 '06 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jean-Sébastien Bolduc | last post by:
Hello, I would like to associate a local namespace with a lambda function. To be more specific, here is exactly what I would want: def foo(): a = 1 f = lambda x : a*x return f
9
by: HikksNotAtHome | last post by:
This is a very simplified example of an Intranet application. But, is there an easier (or more efficient) way of getting the decimal equivalent of a fraction? The actual function gets the select...
12
by: knocte | last post by:
Hello. I have always thought that the eval() function was very flexible and useful. If I use it, I can define functions at runtime!! However, I have found a case where eval() does not work...
18
by: Joe Fallon | last post by:
I have some complex logic which is fairly simply to build up into a string. I needed a way to Eval this string and return a Boolean result. This code works fine to achieve that goal. My...
3
by: Pauljh | last post by:
Hi All, I'm running some javascript over a server side generated web page and have multiple generated empty select statements, that I want to populate when the page is loaded. As HTML doesn't do...
7
by: Darko | last post by:
Hello, I have this particular problem with eval() when using Microsoft Internet Explorer, when trying to define an event handler. This is the code: function BigObject() { this.items = new...
6
by: vasudevram | last post by:
Hi group, Question: Do eval() and exec not accept a function definition? (like 'def foo: pass) ? I wrote a function to generate other functions using something like eval("def foo: ....") but...
2
by: Florian Loitsch | last post by:
hi, What should be the output of the following code-snippet? === var x = "global"; function f() { var x = 0; eval("function x() { return false; }"); delete x; alert(x); }
6
by: RandomElle | last post by:
Hi there I'm hoping someone can help me out with the use of the Eval function. I am using Access2003 under WinXP Pro. I can successfully use the Eval function and get it to call any function with...
10
by: Gordon | last post by:
I have a script that creates new objects based on the value of a form field. Basically, the code looks like this. eval ('new ' + objType.value + '(val1, val2, val3'); objType is a select with...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.