Please Help: dynamic statement

  • Thread starter Thread starter A Jafarpour
  • Start date Start date
A

A Jafarpour

Hi everyone, hope someone can tell if there is any way to,
dynamically, build an statement and then call some
functions to execute the statement. I know examples always
help, so here what I am trying to accomplish:

Dim myStatement as String
myStatement = "Dim result as integer result= 2 * 3"

now is there any way to actually let a program to treat
myStatement as a statment and therefore execute whatever
vb code is in it?

Looking forward to hearing from you. TANX
 
A Jafarpour said:
Hi everyone, hope someone can tell if there is any way to,
dynamically, build an statement and then call some
functions to execute the statement. I know examples always
help, so here what I am trying to accomplish:

Dim myStatement as String
myStatement = "Dim result as integer result= 2 * 3"

now is there any way to actually let a program to treat
myStatement as a statment and therefore execute whatever
vb code is in it?

The string itself can not be executed. An application must meet some minimum
requirements in order to be compilable.

You might have a look at CodeDom:

http://msdn.microsoft.com/library/e...gsourcecodedynamicallyinmultiplelanguages.asp
 
Hi Armin,

I think you misunderstood his question - he simply wants to enter a string
into a textbox, for example, and then execute the string as if it were a
macro, not as if it were an executable program. I suspect you or Herfried
do know how to do this; I don't but I'd love to know also.

Bernie Yaeger
 
Bernie Yaeger said:
I think you misunderstood his question - he simply wants to enter a
string into a textbox, for example, and then execute the string as if
it were a macro, not as if it were an executable program.

I understood he wants to execute the entered line. Where is the difference
to an "executable program"? As he wants to execute the line, I think it is a
kind of a short program to be executed.
 
Bernie is right, I didn't mean anythng heavy duty through
CodeDom; just create a string variable whose content is
some vb code and then letting it be treated as a
statement; again the example I provided should says it all
(I hope).

TANX
 
A Jafarpour said:
Bernie is right, I didn't mean anythng heavy duty through
CodeDom; just create a string variable whose content is
some vb code and then letting it be treated as a
statement;


Yes, that's what I understood also.
 
* "A Jafarpour said:
Hi everyone, hope someone can tell if there is any way to,
dynamically, build an statement and then call some
functions to execute the statement. I know examples always
help, so here what I am trying to accomplish:

Dim myStatement as String
myStatement = "Dim result as integer result= 2 * 3"

now is there any way to actually let a program to treat
myStatement as a statment and therefore execute whatever
vb code is in it?

<http://www.devx.com/codemag/Article/10352/0/page/1>

Samples:

<http://www.codeproject.com/useritems/evaluator.asp>
<http://www.codeproject.com/csharp/livecodedotnet.asp>
<http://www.mvps.org/dotnet/dotnet/samples/miscsamples/downloads/DotNetCompiler.zip>

Evaluating mathematical expressions:
<http://www.palmbytes.de/content/dotnet/mathlib.htm>

Using the Script Control (COM):
<http://www.vb2themax.com/Item.asp?PageID=TipBank&ID=535>
 
Herfried K. Wagner said:
You won't win the fight in this group.

IMO, there's no fight to win (because there's still a lot of space in my
killfile). I think it's a pity for the majority of people that simply
download all new messages from time to time and that don't want to process
every single message manually just because of some ignorants. It's even more
a pity because minor changes in their behavior would help everybody.
 
* "Mick Doherty said:
We just don't like rules.

||* "Cor" <[email protected]> scripsit:
||>>> http://learn.to/quote
||>>
||>> You won't win the fight in this group.
||>
||> While it is in German, English and Dutch
||
||Even if people here can read it, they don't understand the content
||*shock*.
||
||--
||Herfried K. Wagner
||MVP · VB Classic, VB.NET
||<http://www.mvps.org/dotnet>

Sorry, couldn't resist that.

It's your choice to take part in the community of people _helping_ or in
the spammer community.

EOT
 
Herfried,
Even if people here can read it, they don't understand the content
*shock*.

Maybe they do, but they don't agree with it, that is the hard thing from
progress and when you want to go in the inovation business, will be
confronted with many times.

And sometimes you will see, that you're the one who took the wrong
conclussion and did learn from it.

This last I said without any connection to the topic now, because in this
topic I don't think there is a right or wrong.

:-)

Cor
 
* "Cor said:
Maybe they do, but they don't agree with it, that is the hard thing from
progress and when you want to go in the inovation business, will be
confronted with many times.

I remember the teacher at school telling us:

1 + 1 = 2

Most people agreed. Those who didn't agree left the school or got a bad
mark. It was their choice to learn or to stay stupid.
And sometimes you will see, that you're the one who took the wrong
conclussion and did learn from it.

"Homo sum; humani nihil a me alienum puto."
(Cicero, Terenz)
This last I said without any connection to the topic now, because in this
topic I don't think there is a right or wrong.

:-)
 
Hi A,

"2 * 3, etc" is simple expression evaluation - relatively easy to do with
a few simple classes. Anything that actually uses VB.NET syntax, and variables
and objects etc will need a heavier framework. This would either mean a script
engine such as the ones that execute JavaScript and VbScript, or creating
source code and compiling and loading it dynamically.

So when you say statements, do you mean arithemetic or general statements?

Using an expression evaluator is easy:

Your example would go from:
Dim myStatement as String
myStatement = "Dim result as integer result= 2 * 3"
to
Dim iResult As Integer = Int (Calc.Evaluate ("2 * 3"))

You could add variables:
Dim dResult As Double = 23
Calc.AddVar ("Foo", dFoo)
Dim dResult As Double = Calc.Evaluate ("Sqrt (Foo)")

Regards,
Fergus
 
Herfried,

You ask for it.
You learned 1 + 1 = 10 at the beginning of your time at school?!

Herfried, wait till you are a little more grown up, you will learn that
there is not only 1 + 1 = 2. When you are in the class where your teacher
learn you multiplying, then you will see that 1+1 is not always 2.

Too there are other language, like the old Latin that you maybe once will
learn, they had other figurs, they wrote I + I = II, we know that it is
difficult for you, but once when you are older, you will see that there is
more in the world than 1+1=2.

Now you have learned that 1+1=2 you think you know everything, but there is
a lot more to learn. I think that Mick maybe can tell you what he did mean
with 1+1 is 10 because I cannot right find the sentences to explain you
that.

Did you been with your daddy all once in that riesenrad or are you still to
young for that?

Cor
 
Back
Top