Build a string for arithmetic operation

R

rowe_newsgroups

Nov 13, 2007

Hi all

Can I build a string for arithmetic operation. (VB NET 2005)

e.g.

I want to build an arithmetic operation from within the program as

mVariable="A1+A2+A3" (where A1=1, A2=5, A3=10) (A1,A2 & A3 are variables
declared as integers)

mTotal=mVariable (mTotal should return 16)

If there is any other way to do what I want I would appreciate if someone
can guide me.

Thank you.
Mike TI

I'm confused - why not just do this?

Dim mVariable As Integer = A1 + A2 + A3

Thanks,

Seth Rowe
 
G

Guest

Think he really wants to do something like ...

mTotal = Eval(MVariable)

.... and I would be interested in something like that. Have an VB6 project I
will need to convert some day that uses an old 'Awk' control for this type of
thing. It fires an event when it needs the value of a 'token' it does not
recognize, allowing you to suibstitue the value for the token. I think it
may be possible using Regx, but I have not explored it yet.
 
R

rowe_newsgroups

Think he really wants to do something like ...

mTotal = Eval(MVariable)

... and I would be interested in something like that. Have an VB6 project I
will need to convert some day that uses an old 'Awk' control for this type of
thing. It fires an event when it needs the value of a 'token' it does not
recognize, allowing you to suibstitue the value for the token. I think it
may be possible using Regx, but I have not explored it yet.

Ahh, for Eval you might take a look at the following thread:

http://groups.google.com/group/micr...6ba1808?hl=en&lnk=gst&q=eval#08ed83cd46ba1808

Thanks,

Seth Rowe
 
R

rowe_newsgroups

Seth,

I think that Tom and know I find that a very good thread.

:)

Cor

lol

I love it when you two do all the work for me! Posting links is *so*
much easier than retyping a solution.

:)

Thanks,

Seth Rowe
 
M

Mike TI

Nov 13, 2007

Hi all

Can I build a string for arithmetic operation. (VB NET 2005)

e.g.

I want to build an arithmetic operation from within the program as

mVariable="A1+A2+A3" (where A1=1, A2=5, A3=10) (A1,A2 & A3 are variables
declared as integers)

mTotal=mVariable (mTotal should return 16)

If there is any other way to do what I want I would appreciate if someone
can guide me.

Thank you.
Mike TI
 
M

Mike TI

Nov 14, 2007

Hi

I have checked links, still apparently they do not answer my question.

I want to build an expression from within the program and then store it in a
variable like

mVariable="A1+A2+A3"

Now I want to put the result of the above into another variable like

mTotal=Eval(mVariable)

The applications should return the value as if I had entered

mTotal=A1+A2+A3

Thank you.

Mike TI
 
R

rowe_newsgroups

I have checked links, still apparently they do not answer my question.

And why not? Could you tell us what exactly didn't work?

Also you still never answered why you are needing this and why you
can't simply do:

mTotal = A1 + A2 + A3

And avoid all the messy Eval stuff.

Thanks,

Seth Rowe
 
A

Armin Zingler

Mike TI said:
Nov 14, 2007

Hi

I have checked links, still apparently they do not answer my
question.

I want to build an expression from within the program and then store
it in a variable like

mVariable="A1+A2+A3"

Now I want to put the result of the above into another variable like

mTotal=Eval(mVariable)

The applications should return the value as if I had entered

mTotal=A1+A2+A3

Thank you.

"A1+A2+A3" is an expression. An expression has a syntax. If you need VB.net
syntax, you need a VB.Net compiler. The compiler compiles to IL code, and
the JIT compiler will compile it to native code when executed.
An executable statement in VB.Net must be part of a procedure/method. A
method is part of a class. The class must belong to a project. You don't
have a project.

So, in order to execute a line you must
1. create a project
2. add a class
3. add a procedure
4. add an executable line
5. compiler the project
6. run the executable

Alternatively, have a look at the System.Reflection.Emit namespace. There
you will also have to create an assembly, a class, a method and executable
code.

What you want to do is not possible because you don't give the line an
environment to live in.


Armin
 
M

Mike TI

Nov 14, 2007

Hellow Seth

I am converting a Payroll application that I did in MS Visual Foxpro. In
this application how an Allowance or Deduction is defined is flexible. Just
to give you a simple example:

For one company the basis can be as below:

Allowances:

ALL01 1,000
ALL02 100
ALL03 500
ALL04 45% of ALL01+ALL02+ALL03
ALL05 10% of ALL01+ALL03 if no Leave
Without Pay

For another company the basis can be as below:

Allowances:

ALL01 1,000
ALL02 100
ALL03 500
ALL04 45% of ALL01+ALL02
ALL05 10% of ALL01+ALL02+ALL03 if no Leave
Without Pay

Now I want to build the calculation for ALL04 & ALL05 from within the
program. There are some other alternates. However I was wondering if I could
do the way I was doing in VFP.

Also I was wondering if I could load a form getting the name stored in a
variable.

Thanks
Mike TI
 
A

Armin Zingler

Mike TI said:
Nov 14, 2007

Hellow Seth

I am converting a Payroll application that I did in MS Visual
Foxpro. In this application how an Allowance or Deduction is defined
is flexible. Just to give you a simple example:

For one company the basis can be as below:

Allowances:

ALL01 1,000
ALL02 100
ALL03 500
ALL04 45% of ALL01+ALL02+ALL03
ALL05 10% of ALL01+ALL03 if no
Leave Without Pay

For another company the basis can be as below:

Allowances:

ALL01 1,000
ALL02 100
ALL03 500
ALL04 45% of ALL01+ALL02
ALL05 10% of ALL01+ALL02+ALL03 if
no Leave Without Pay

Now I want to build the calculation for ALL04 & ALL05 from within
the program. There are some other alternates. However I was
wondering if I could do the way I was doing in VFP.

You must write a parser and interpret the input on you own.
Also I was wondering if I could load a form getting the name stored
in a variable.

Whenever I read this, I wonder how a class name, that is only relevant to
the developer, which means it gets out of interest after the source code has
been compiled, how this class names makes it's way through to run-time as
the content of a variable. The only exception is whenever the class name is
also stored externally. In this case, I recommend writing a Select Case
statment based upon the string and create the corresponding instance. This
enables the compiler to check the class name at compile time and it is more
straight forward than using Reflection (see System.Activator.Creatinstance).


Armin
 
R

Rory Becker

Apoligies if this has been covered. My nesreader lists this as the first
post in this thread (clearly not the case)

I have used a Library called eval3 found @ http://www.codeproject.com/dotnet/eval3.asp
with great success.

If memory serves you get to new up an evaluator of sorts and then add Name/Value
pairs to it in order to define the values of variables which can be evaluated

Pseudocode ( again just the basic Idea, Not actual syntax as I can't remember
it right now)

Dim Ev As New Evaluator
Ev.Add("A1", 10)
Ev.Add("A2", 12)
Ev.Add("A3", 14)
Ev.Add("A4" 16)
Dim Answer as Integer = Ev.Evaluate("A1+A2+A3+A4")
' Answer will now = 52

Is this of any use?

BTW Eval3 is way more powerful than this. It allows you to define your own
functions in .Net Code and Access then directly from the String which Gets
Evaluated.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top