473,326 Members | 2,012 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Really????

Is there no easier way to convert a string into the variable that the
string represents?

Nov 21 '05 #1
12 1050
"Alex Pierson" <ar******@charter.net> schrieb:
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/>
Nov 21 '05 #2
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:
"Alex Pierson" <ar******@charter.net> schrieb:
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/>


Nov 21 '05 #3
Sounds like you might need a HashTable.

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

"Alex Pierson" <ar******@charter.net> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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:
"Alex Pierson" <ar******@charter.net> schrieb:
> 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/>

Nov 21 '05 #4
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?

Nov 21 '05 #5
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?
Nov 21 '05 #6
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?

Nov 21 '05 #7
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 Pierson" <ar******@charter.net> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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?

Nov 21 '05 #8
Alex Pierson schrieb:
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
Nov 21 '05 #9
Alex Pierson wrote:
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 :)
--
Rinze van Huizen
C-Services Holland b.v.
Nov 21 '05 #10
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

"Alex Pierson" <ar******@charter.net> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
| 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:
| > "Alex Pierson" <ar******@charter.net> schrieb:
| > > 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/>
|
Nov 21 '05 #11
Jay,

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> schrieb:
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/>

Nov 21 '05 #12
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

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:ev**************@tk2msftngp13.phx.gbl...
| Jay,
|
| "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> schrieb:
| > 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/>
|
Nov 21 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Simon | last post by:
Hi. I don't have a problem per se, I was just wondering if anyone can offer some opinions about the best way to go about creating and disposing of a Singleton class. I have a class (handling...
21
by: Hattuari | last post by:
I'm learning C++ after having spent several years in the computer industry doing both system administration and engineering. I've written code in Perl, Bash, Pascal, Ada, C, Mathematica (hundreds...
3
by: Studio P.M. | last post by:
Dear colleagues, I kindly ask the C# developers community to help me to REALLY understand the Page_Load event, so to REALLY have it under control. Reason for this question is that I cannot...
59
by: Alan Silver | last post by:
Hello, This is NOT a troll, it's a genuine question. Please read right through to see why. I have been using Vusual Basic and Classic ASP for some years, and have now started looking at...
131
by: pemo | last post by:
Is C really portable? And, apologies, but this is possibly a little OT? In c.l.c we often see 'not portable' comments, but I wonder just how portable C apps really are. I don't write...
16
by: CMM | last post by:
Is it me or has anyone noticed that F1 is really dumb in VS2005. Since VB3 I have been able to click F1 on an ambiguous method in code and the IDE automatically determines the type based on the...
3
Chrisjc
by: Chrisjc | last post by:
I am not good at VB and I am taking a class for it at ITT... I am doing a program chanllenge and really really need help it is due today... and I am not good at this at all... could some one please...
0
by: P Pulkkinen | last post by:
Dear all, sorry, i know this code is far little too long to debug here, but there is really annoying logical error. If someone debugs this, I really offer warm virtual handshake. What this...
8
by: Vincent RICHOMME | last post by:
Hi, first I would like to apologize about my question because usually I hate specific implementation but in this case I really would like an answer. Besides my example is an example of what's...
0
by: raylopez99 | last post by:
I ran afoul of this Compiler error CS1612 recently, when trying to modify a Point, which I had made have a property. It's pointless to do this (initially it will compile, but you'll run into...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.