Connecting Tech Pros Worldwide Help | Site Map

Eval function vb.net

 
LinkBack Thread Tools Search this Thread
  #1  
Old February 20th, 2006, 02:05 PM
Maurice
Guest
 
Posts: n/a
Default 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


  #2  
Old February 20th, 2006, 02:15 PM
Armin Zingler
Guest
 
Posts: n/a
Default Re: Eval function vb.net

"Maurice" <hmoviat@nospam.nospam> schrieb[color=blue]
> 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"[/color]


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

  #3  
Old February 20th, 2006, 02:25 PM
Maurice
Guest
 
Posts: n/a
Default Re: Eval function vb.net

"Armin Zingler" <az.nospam@freenet.de> wrote in
news:uDJYz8iNGHA.3856@TK2MSFTNGP12.phx.gbl:
[color=blue]
> http://msdn.microsoft.com/library/de...y/en-us/cpguid
> e/html/cpconemittingdynamicassemblies.asp[/color]

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.






  #4  
Old February 20th, 2006, 03:05 PM
Armin Zingler
Guest
 
Posts: n/a
Default Re: Eval function vb.net

"Maurice" <hmoviat@nospam.nospam> schrieb[color=blue]
> 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.[/color]


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

  #5  
Old February 20th, 2006, 03:45 PM
Maurice
Guest
 
Posts: n/a
Default Re: Eval function vb.net

"Armin Zingler" <az.nospam@freenet.de> wrote in news:OrGv#YjNGHA.428
@tk2msftngp13.phx.gbl:
[color=blue]
> Creating an object by class name[/color]

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.



  #6  
Old February 20th, 2006, 04:35 PM
CMM
Guest
 
Posts: n/a
Default Re: Eval function vb.net

Look into Activator.CreateInstance(). This has been asked before. Do a
search for more detailed examples.


--
-C. Moya
www.cmoya.com
"Maurice" <hmoviat@nospam.nospam> wrote in message
news:Xns9770A5BB9E1ACmauricemertens@194.109.133.24 2...[color=blue]
> "Armin Zingler" <az.nospam@freenet.de> wrote in
> news:uDJYz8iNGHA.3856@TK2MSFTNGP12.phx.gbl:
>[color=green]
>> http://msdn.microsoft.com/library/de...y/en-us/cpguid
>> e/html/cpconemittingdynamicassemblies.asp[/color]
>
> 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.
>
>
>
>
>
>[/color]


  #7  
Old February 20th, 2006, 07:45 PM
Herfried K. Wagner [MVP]
Guest
 
Posts: n/a
Default Re: Eval function vb.net

"Maurice" <hmoviat@nospam.nospam> schrieb:[color=blue]
> 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"[/color]

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/>
  #8  
Old February 22nd, 2006, 08:45 AM
Charles Law
Guest
 
Posts: n/a
Default Re: Eval function vb.net

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" <hmoviat@nospam.nospam> wrote in message
news:Xns9770B2725FE78mauricemertens@194.109.133.24 2...[color=blue]
> "Armin Zingler" <az.nospam@freenet.de> wrote in news:OrGv#YjNGHA.428
> @tk2msftngp13.phx.gbl:
>[color=green]
>> Creating an object by class name[/color]
>
> 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.
>
>
>[/color]


  #9  
Old February 22nd, 2006, 09:25 AM
Cor Ligthert [MVP]
Guest
 
Posts: n/a
Default Re: Eval function vb.net

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" <blank@nowhere.com> schreef in bericht
news:%23mviJK5NGHA.3196@TK2MSFTNGP09.phx.gbl...[color=blue]
> 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" <hmoviat@nospam.nospam> wrote in message
> news:Xns9770B2725FE78mauricemertens@194.109.133.24 2...[color=green]
>> "Armin Zingler" <az.nospam@freenet.de> wrote in news:OrGv#YjNGHA.428
>> @tk2msftngp13.phx.gbl:
>>[color=darkred]
>>> Creating an object by class name[/color]
>>
>> 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.
>>
>>
>>[/color]
>
>[/color]


  #10  
Old February 22nd, 2006, 10:55 AM
Charles Law
Guest
 
Posts: n/a
Default Re: Eval function vb.net

Hi Cor

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

Charles


"Cor Ligthert [MVP]" <notmyfirstname@planet.nl> wrote in message
news:uiIv2k5NGHA.3732@TK2MSFTNGP10.phx.gbl...[color=blue]
> 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" <blank@nowhere.com> schreef in bericht
> news:%23mviJK5NGHA.3196@TK2MSFTNGP09.phx.gbl...[color=green]
>> 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" <hmoviat@nospam.nospam> wrote in message
>> news:Xns9770B2725FE78mauricemertens@194.109.133.24 2...[color=darkred]
>>> "Armin Zingler" <az.nospam@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.
>>>
>>>
>>>[/color]
>>
>>[/color]
>
>[/color]


  #11  
Old February 22nd, 2006, 01:55 PM
Cor Ligthert [MVP]
Guest
 
Posts: n/a
Default Re: Eval function vb.net

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


  #12  
Old February 22nd, 2006, 02:45 PM
Charles Law
Guest
 
Posts: n/a
Default Re: Eval function vb.net

Hi Cor

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

Charles


"Cor Ligthert [MVP]" <notmyfirstname@planet.nl> wrote in message
news:ua$Ue27NGHA.3284@TK2MSFTNGP14.phx.gbl...[color=blue]
> 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
>[/color]


  #13  
Old February 23rd, 2006, 02:25 PM
Maurice
Guest
 
Posts: n/a
Default Re: Eval function vb.net

"Cor Ligthert [MVP]" <notmyfirstname@planet.nl> wrote in
news:ua$Ue27NGHA.3284@TK2MSFTNGP14.phx.gbl:
[color=blue]
> 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
>
>[/color]

Hi Charles, Cor,

thanks for the good support!

I will try this one out.


Maurice
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.