Home All Groups Group Topic Archive Search About

Build a string for arithmetic operation

Author
14 Nov 2007 1:31 AM
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

Author
13 Nov 2007 2:17 PM
rowe_newsgroups
Show quote Hide quote
On Nov 13, 8:31 pm, "Mike TI" <tanv***@creativepk.com> wrote:
> 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
Author
13 Nov 2007 2:39 PM
Terry
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.

--
Terry


Show quoteHide quote
"rowe_newsgroups" wrote:

> On Nov 13, 8:31 pm, "Mike TI" <tanv***@creativepk.com> wrote:
> > 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
>
>
Author
13 Nov 2007 3:01 PM
rowe_newsgroups
On Nov 13, 9:39 am, Terry <Ter...@nospam.nospam> wrote:
Show quoteHide quote
> 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.
>
> --
> Terry
>
> "rowe_newsgroups" wrote:
> > On Nov 13, 8:31 pm, "Mike TI" <tanv***@creativepk.com> wrote:
> > > 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

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

http://groups.google.com/group/microsoft.public.dotnet.languages.vb/browse_thread/thread/1dcc3718f955806a/08ed83cd46ba1808?hl=en&lnk=gst&q=eval#08ed83cd46ba1808

Thanks,

Seth Rowe
Author
13 Nov 2007 6:38 PM
Cor Ligthert[MVP]
Seth,

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

:-)

Cor
Author
13 Nov 2007 6:47 PM
rowe_newsgroups
On Nov 13, 1:38 pm, "Cor Ligthert[MVP]" <notmyfirstn***@planet.nl>
wrote:
> 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
Author
14 Nov 2007 9:22 AM
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


Show quoteHide quote
"Mike TI" <tanv***@creativepk.com> wrote in message
news:OB62MmfJIHA.588@TK2MSFTNGP05.phx.gbl...
> 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
>
>
Author
14 Nov 2007 11:29 AM
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
Author
14 Nov 2007 12:35 PM
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

Show quoteHide quote
"rowe_newsgroups" <rowe_em***@yahoo.com> wrote in message
news:1195039765.991732.40720@57g2000hsv.googlegroups.com...
>> 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
>
Author
14 Nov 2007 12:55 PM
Armin Zingler
Show quote Hide quote
"Mike TI" <tanv***@creativepk.com> schrieb
> 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
Author
14 Nov 2007 12:30 PM
Armin Zingler
Show quote Hide quote
"Mike TI" <tanv***@creativepk.com> schrieb
> 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
Author
14 Nov 2007 1:04 PM
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.

--
Rory