Re: evaluating a string as an expression
From: James Moore (jem63_at_del.this.cam.ac.uk)
Date: 01/17/05
- Next message: Arne Beruldsen: "Error..."Operation must use an updateable query""
- Previous message: Smooga: "How Do I Set Formatting For Specific File Extensions"
- In reply to: Dan Horne: "evaluating a string as an expression"
- Messages sorted by: [ date ] [ thread ]
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
- Next message: Arne Beruldsen: "Error..."Operation must use an updateable query""
- Previous message: Smooga: "How Do I Set Formatting For Specific File Extensions"
- In reply to: Dan Horne: "evaluating a string as an expression"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|