Re: evaluating a string as an expression

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: James Moore (jem63_at_del.this.cam.ac.uk)
Date: 01/17/05


Date: Mon, 17 Jan 2005 15:15:01 -0000


"Dan Horne" <dan@taisweb.net> wrote in message
news:103c01c4fa49$6e533540$a401280a@phx.gbl...
> Hello, I am rather new to VB, though I've used other
> languages for years. I am trying to figure out how, in
> VB .NET, to evaluate a string as an expression and I
> can't figure out how to do it.
>
> Example:
>
>
> dim intMyNumber as integer = 1
> dim txtMyString as string
> dim txtSample1 as string = "This is a test"
>
> 'I build a string like so:
> txtMyString = "txtSample" & intMyNumber
>
> which results in txtMyString having a value
> of "txtSample1".
> In ColdFusion, for instance, I could use the
> function 'Evaluate(txtMyString)' to return the value of
> txtSample1. How can I return txtSample1's value in VB?

The problem here is the difference between compile time and runtime. In an
interpreter (which most scripting languages such as ColdFusion, PHP, Perl
etc run under) they are the same thing (IE when you are actually executing
the script you still have things such as the symbol table left over from
compile time so you can use that to look up values).

If compile time and runtime are separate things, as in VB, then you do not
have the comfort of symbol tables to look up names in. It is still possible
to do somthing similar (have a look at reflection) however that will only
give you access to fields on objects and not local variables. If you want to
access the value of a local variable then you will need to do some sort of
switch statement on the value of "txtSample" & intMyNumber.

There may be some way in VB Im not aware of that will allow you to do this
but I cant think of one off the top of my head and the factors above suggest
it is not likley to be easy and the cost of doing it would probably be quite
high.

HTH,

- James



Relevant Pages

  • whats OOPs jargons and complexities?
    ... in advanced languages such as LISP family, it is not uncommon to define ... subroutine f1 ... For example, in Java, a string is a class String. ...
    (comp.lang.perl.misc)
  • whats OOPs jargons and complexities?
    ... in advanced languages such as LISP family, it is not uncommon to define ... subroutine f1 ... For example, in Java, a string is a class String. ...
    (comp.lang.c)
  • whats OOPs jargons and complexities?
    ... in advanced languages such as LISP family, it is not uncommon to define ... subroutine f1 ... For example, in Java, a string is a class String. ...
    (comp.lang.python)
  • whats OOPs jargons and complexities?
    ... in advanced languages such as LISP family, it is not uncommon to define ... subroutine f1 ... For example, in Java, a string is a class String. ...
    (comp.lang.lisp)
  • Re: Java Generics and Erasure foobar
    ... I mean if I mix a String with an Integer this will come out in unit ... So one of the primary features of generics is this: ... Compile time safety is another vital feature. ... out in unit tests," but are you really that confident that every ...
    (comp.lang.java.programmer)