Really????

  • Thread starter Thread starter Alex Pierson
  • Start date Start date
A

Alex Pierson

Is there no easier way to convert a string into the variable that the
string represents?
 
Sorry that was meant to be a reply to another thread, but my question
is this.

I need to convert a string into a value that is represented by a
variable that has the same name as the string.

EG.

Dim str as string = "var1"
dim var1 as integer = 5

I want to convert str in this case to 5.

Thanks.
 
Sounds like you might need a HashTable.

Dim ht As New HashTable
ht.Add ("var1", 5)
Dim var1 As Integer = ht("var1")

Alex Pierson said:
Sorry that was meant to be a reply to another thread, but my question
is this.

I need to convert a string into a value that is represented by a
variable that has the same name as the string.

EG.

Dim str as string = "var1"
dim var1 as integer = 5

I want to convert str in this case to 5.

Thanks.
 
I don't think thats what I'm looking for.

Let me explain it a little better.

I set several variables:

x = 0
y = 1
z = 2 etc

An outside source, eg. a database, tells me which variable to use in a
particular instance.
So the external source says use variable x in the equation.

The equation is something like:
Ans = 50 + [the applicable variable], or in this case 50 + x

Since the external source is sending a string indicating which variable
to use, how can I convert that string into the value represented by the
current variable?

Make sense?
 
As I said, the immediate solution that comes to mind is store them in a
HashTable.
x = 0
y = 1
z = 2 etc

ht.Add ("x", 0)
ht.Add ("y", 1)
ht.Add ("z", 1)
etc
An outside source, eg. a database, tells me which variable to use in a
particular instance.
So the external source says use variable x in the equation.

The equation is something like:
Ans = 50 + [the applicable variable], or in this case 50 + x

Ans = 50 + ht ("x")

-or-

Dim CurrentVariable As String = "x"
Ans = 50 + ht (CurrentVariable)

This is a method of assigning names to variables and storing values for
them. This does exactly what you asked...

So does the "external source" tell you "which variable to use in a
particular instance" or does the "external source" pass you the "entire
mathematical expression" which you want to now parse? E.g., did you *mean*
to ask "How do I parse mathematical expressions?" If so then you should
visit http://www.codeproject.com/cpp/MathieuMathParser.asp or Google "math
expression parser".

Make sense?
 
OK I think I got it, and thanks.

The external source does not pass the the entire expression, although
that is essentially what I am trying to do.

A further question, and if the answer is: "google math expression
parser", just let me know -- I just don't know if thats what I'm
looking for:

taking a string of "a = b"
and associated integer variables a =1 and b = 2
how do I convert the string to an mathematical equation yielding an
integer?
 
The math parser I pointed you to does, I believe, have some functionality
built in to assign values to variables. Now, given the simple expressions
you've given, you can split the string on the "=" sign and use CInt() on the
right half. To further use the HashTable example:

' Assumes we defined HashTable ht from previous example
Dim Expression As String = "x = 100"
Dim Factors() As String = Expression.Split("=")
ht(Factors(0).Trim()) = CInt(Factors(1).Trim())
Dim CurrentVariable As String = "x"
Dim Ans As Integer = 50 + ht("x")

This only works for simple expressions (i.e., "x = 100") and won't work for
more complex expressions (i.e., "x = 100 + ( 10 * 67)"). For the complex
expressions you need to either roll your own math parser, or use one that's
already written; parse the right hand side, then assign to the variable.
 
Alex said:
Sorry that was meant to be a reply to another thread, but my question
is this.

I need to convert a string into a value that is represented by a
variable that has the same name as the string.

EG.

Dim str as string = "var1"
dim var1 as integer = 5

I want to convert str in this case to 5.

Variable names are only used at compile time for the compiler to reference
memory locations in code. They are there for simplification, otherwise you
would have to deal with memory addresses which doesn't make life easier.
Variable names don't matter at runtime at all. If you want to have a
"key/value" association, you can use a Hashtable.

Referring to variable names is part of the *source code* of an application.
At run time, you can not refer to something that does not exist at this
point. The only thing available at run time is assembler code executed by
the CPU. Source code must have a certain structure for a compiler to be able
to compile it. There are some minimum requirements that must be met. If you
want to compile, for example, an Exe, you must at least have a class with a
sub main, and executable code must be part of a procedure. In your code
above, you don't have a compilable project. See my link in the other thread
how to compile code at run time.

Armin
 
Alex said:
Sorry that was meant to be a reply to another thread, but my question
is this.

I need to convert a string into a value that is represented by a
variable that has the same name as the string.

EG.

Dim str as string = "var1"
dim var1 as integer = 5

I want to convert str in this case to 5.

Thanks.

Hmm sounds like you've been programming in Clipper (Xbase) for some time :)
 
Alex,
In addition to the other comments.

Is the variable a local variable to a function or is the "variable" a
field/property of a class.

If its a field/property of a class you can use Reflection to get or set the
field/property of the class.

Something like:

Dim aValue As Integer = 100
Dim aFieldName As String = "x"

Dim aSomething As New Something

Dim fields() As System.Reflection.FieldInfo =
aSomething.GetType().GetFields()

For Each field As System.Reflection.FieldInfo In fields
If field.Name = aFieldName Then
field.SetValue(aSomething, aValue)
End If
Next


Public Class Something
Public x As Integer = 0
Public y As Integer = 1
Public z As Integer = 2
End Class

The "problem" is that GetFields return an array of FieldInfo objects, you
need to search for the field with the name you want. In this case I will
consider using System.ComponentModel.PropertyDescriptor instead as
System.ComponentModel.PropertyDescriptorCollection allows indexing by name.

Hope this helps
Jay

| Sorry that was meant to be a reply to another thread, but my question
| is this.
|
| I need to convert a string into a value that is represented by a
| variable that has the same name as the string.
|
| EG.
|
| Dim str as string = "var1"
| dim var1 as integer = 5
|
| I want to convert str in this case to 5.
|
| Thanks.
|
| Herfried K. Wagner [MVP] wrote:
| > > Is there no easier way to convert a string into the variable that the
| > > string represents?
| >
| > ???
| >
| > --
| > M S Herfried K. Wagner
| > M V P <URL:http://dotnet.mvps.org/>
| > V B <URL:http://classicvb.org/petition/>
|
 
Jay,

Jay B. Harlow said:
Dim aFieldName As String = "x"

Dim aSomething As New Something

Dim fields() As System.Reflection.FieldInfo =
aSomething.GetType().GetFields()

For Each field As System.Reflection.FieldInfo In fields
If field.Name = aFieldName Then
field.SetValue(aSomething, aValue)
End If
Next


Public Class Something
Public x As Integer = 0
Public y As Integer = 1
Public z As Integer = 2
End Class

The "problem" is that GetFields return an array of FieldInfo objects, you
need to search for the field with the name you want. In this case I will
consider using System.ComponentModel.PropertyDescriptor instead as
System.ComponentModel.PropertyDescriptorCollection allows indexing by
name.

I am curious why you don't use 'Type.GetField' instead of 'Type.GetFields':

\\\
Dim s As New Something()
s.GetType().GetField("y").SetValue(s, 100)
MsgBox(CStr(s.y))
///
 
Herfried,
| I am curious why you don't use 'Type.GetField' instead of
'Type.GetFields':
|
Doh! ;-)

Cause I'm normally work with the entire list of fields/properties, rather
then individual fields or properties. Truth be told, I actually use
PropertyDescriptor more then I use FieldInfo or PropertyInfo.

Thanks for reminding me about GetField...

Jay

| Jay,
|
| > Dim aFieldName As String = "x"
| >
| > Dim aSomething As New Something
| >
| > Dim fields() As System.Reflection.FieldInfo =
| > aSomething.GetType().GetFields()
| >
| > For Each field As System.Reflection.FieldInfo In fields
| > If field.Name = aFieldName Then
| > field.SetValue(aSomething, aValue)
| > End If
| > Next
| >
| >
| > Public Class Something
| > Public x As Integer = 0
| > Public y As Integer = 1
| > Public z As Integer = 2
| > End Class
| >
| > The "problem" is that GetFields return an array of FieldInfo objects,
you
| > need to search for the field with the name you want. In this case I will
| > consider using System.ComponentModel.PropertyDescriptor instead as
| > System.ComponentModel.PropertyDescriptorCollection allows indexing by
| > name.
|
| I am curious why you don't use 'Type.GetField' instead of
'Type.GetFields':
|
| \\\
| Dim s As New Something()
| s.GetType().GetField("y").SetValue(s, 100)
| MsgBox(CStr(s.y))
| ///
|
| --
| M S Herfried K. Wagner
| M V P <URL:http://dotnet.mvps.org/>
| V B <URL:http://classicvb.org/petition/>
|
 

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

Back
Top