.net question...getting a variable from a string?

Deeko

Lifer
Jun 16, 2000
30,213
12
81
In .net/C#...

I have an xml configuration file. One of the fields contains the name of a variable. Is there a way to actually referrance that variable, having only this string of the variable name? Maybe through reflection or something?

Thanks.
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
I haven't done any c# coding yet, but one C/C++ way would be to have a brute force function with a table of allowed names, returning a value or pointer.

char* getMeString( const char* stringVarName )
{

char* str ;

if (stricmp( stringVarName, "m_szBunny" ) == 0 )
str = m_szBunny ;

... else if ....

else
str = NULL;

}

You might want something like this even in C# to prevent exploits from the XML pointing to something unsafe or private.
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
I'm not sure exactly what you mean by "One of the fields contains the name of a variable."

Are you saying that you have an XML element and want to get to the value? You can use the DOM to parse an XML file and load the values into objects that can be referenced at runtime.

But I suspect that is not what you're talking about.
 

Deeko

Lifer
Jun 16, 2000
30,213
12
81
well, I could do something like that, that's static though, I want to make it dynamic so if other possible variables are added, I don't have to change as much.
 

Deeko

Lifer
Jun 16, 2000
30,213
12
81
Originally posted by: Markbnj
I'm not sure exactly what you mean by "One of the fields contains the name of a variable."

Are you saying that you have an XML element and want to get to the value? You can use the DOM to parse an XML file and load the values into objects that can be referenced at runtime.

But I suspect that is not what you're talking about.

No, let me explain it a little more.

I have a configuration file....some of the fields have text in them. Say, for example, a line was to display "TOTAL: $10.00", where 10.00 is a variable, named total. In the xml, this is stored as <line>TOTAL: $%total%</line>. Thus, I need to parse out the variable name, which I did, so now I have a string, variable = "total". I now need to take that string, and somehow referance the variable with the same name. Know what I mean?
 

KB

Diamond Member
Nov 8, 1999
5,405
386
126
You could use a HashTable and store the variable name as the key.

Dim hs As New Hashtable
hs.Add("total", 22)

If hs.Contains("total") Then
Dim variable As Object = hs.Item("total")
End If

'variable = 22


HashTable hs = new HashTable();
hs.Add("total", 22)

if(hs.Contains("total")) {
Object variable = hs.Item("total");
}

//variable = 22
 

Apathetic

Platinum Member
Dec 23, 2002
2,587
6
81
Originally posted by: KB
You could use a HashTable and store the variable name as the key.

Dim hs As New Hashtable
hs.Add("total", 22)

If hs.Contains("total") Then
Dim variable As Object = hs.Item("total")
End If

'variable = 22


HashTable hs = new HashTable();
hs.Add("total", 22)

if(hs.Contains("total")) {
Object variable = hs.Item("total");
}

//variable = 22

Dang it... You beat me to it...

Dave
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
Originally posted by: Apathetic
Originally posted by: KB
You could use a HashTable and store the variable name as the key.

Dim hs As New Hashtable
hs.Add("total", 22)

If hs.Contains("total") Then
Dim variable As Object = hs.Item("total")
End If

'variable = 22


HashTable hs = new HashTable();
hs.Add("total", 22)

if(hs.Contains("total")) {
Object variable = hs.Item("total");
}

//variable = 22

Dang it... You beat me to it...

Dave
Looks good to me. Reflection only works if there actually is that variable with that name defined on a real object somewhere. You can't just make stuff up ;) (at least not without bytecode engineering...)
 

UCJefe

Senior member
Jan 27, 2000
302
0
0
Originally posted by: kamper
Originally posted by: Apathetic
Originally posted by: KB
You could use a HashTable and store the variable name as the key.

Dim hs As New Hashtable
hs.Add("total", 22)

If hs.Contains("total") Then
Dim variable As Object = hs.Item("total")
End If

'variable = 22


HashTable hs = new HashTable();
hs.Add("total", 22)

if(hs.Contains("total")) {
Object variable = hs.Item("total");
}

//variable = 22

Dang it... You beat me to it...

Dave
Looks good to me. Reflection only works if there actually is that variable with that name defined on a real object somewhere. You can't just make stuff up ;) (at least not without bytecode engineering...)


Not entirely true. Reflection.Emit namespace can be used to dynamically create variables, methods, aseemblies, etc.
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
Originally posted by: UCJefe
Not entirely true. Reflection.Emit namespace can be used to dynamically create variables, methods, aseemblies, etc.
I'll bet that boils down to automated bytecode engineering (and as such, wouldn't really belong in a reflection class. 'Reflection' by definition is viewing, not modifying). But I have to admit, I'm not a .net programmer...