Click here to Skip to main content
15,908,264 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to get the value of a declared variable based on its Name as a "String".

I have a variable name and default value stored in database and want to compare that to the actual value of the variable during runtime and if it has changed save the new value back to the database. I can do this with a select case statement but there are 10 variables per form and over 50 forms.


Example:

database fields are:

VariableName (as string) | Value (as string)
_____________________________________________
"bTest" | "True"
_________________________________________

There is a Local Variable:

VB
Dim bTest as Boolean = false


I would like to do this. (I know there is no "GetVariable")

VB
If DataTable.Rows(i).Item("Value") = GetVariable("DataTable.Rows(i).Item("VarialbeName")).Value Then

End If


A simpler example may be this.

VB
If DataTable.Rows(i).Item("Value") = GetVariable("bTest")).Value Then


I can do it this way but it is a ton of code:

VB
Select Case DataTable.Rows(i).Item("VarialbeName").ToString
 case "bTest"
    If bTest = CType(DataTable.Rows(i).Item("Value"),Boolean)
       'Do some Task
    End if
End Select
Posted

What's the "variable"? You can do it with the member of some class or structure, a field or property, no matter if it is private or not. You will need to know how to use Reflection and have a variable of the class of structure declaring your member, having only a reference to your member is not enough, and not required.

This is the schema:
  • You have an instance Instance of class or structure which declare a member you need. Obtain its type T using GetType(). It will return variable of the type System.Type and assign to T.
  • For a field, call T.GetField(String, BindingFlags) by name. If a field is public, second parameter is not needed, if not, use System.Reflection.BindingFlags | System.Reflection.NonPublic. If the member is static, also OR System.Reflection.Static.
  • For a property, call T.GetProperty(String, BindingFlags) by name. Use the second parameter is needed, same is in the above item.
  • Check up if the value returned by GetField or GetProperty is not Nothing. If it is Nothing, there is no such member, something is wrong. Consider we got field meta-data F of the type FieldIndo or property meta-data P of the type PropertyIndo.
  • For field, call F.GetValue(Instance, Nothing); it will return required object O. For a static field, both parameters are Nothing.
  • For property, call F.GetValue(Instance, Nothing); it will return required object O. For a static field, both parameters are Nothing.
  • You got an object O from GetValue. Cast it to required type. For value types it will cause un-boxing.
  • PROFIT!


See:
http://msdn.microsoft.com/en-us/library/system.type.aspx[^],
http://msdn.microsoft.com/en-us/library/system.reflection.fieldinfo.aspx[^],
http://msdn.microsoft.com/en-us/library/system.reflection.propertyinfo.aspx[^].

I must say, this is a very bad way of using Reflection. Identification of anything by name is bad, because it's not good for support: should you misspell a name, a compiler won't detect the problem. One correct approach is using interfaces implemented in your classes or structures. With Reflection, you find out if requires interface is implemented, obtain a reference to interface instance and use it directly. No use of any string constants is needed.

[EDIT]

Don't get me wrong: Reflection based on type and member names is useful and important for serialization, including its advanced form Data Contract, See http://msdn.microsoft.com/en-us/library/ms733127.aspx[^], http://msdn.microsoft.com/en-us/library/ms730035.aspx[^].

However, every time you hard-code meta-data names in your code as string constants, this is wrong approach.

—SA
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 16-Nov-11 15:37pm    
OP commented:

Tried this but it comes back as nothing

Dim pi As PropertyInfo = Me.GetType().GetProperty("btest").
Sergey Alexandrovich Kryukov 16-Nov-11 15:40pm    
It means the type of Me does not have public property names "btest". Check up everything. First of all, if you use Me, you can use Me.btest (so the whole thing makes no sense, hope this is just for test). Is this the case? The type of Me must be a class or structure.

If still in doubt, create a one-file console test application and post the whole file using "Improve question" above.

--SA
Sergey Alexandrovich Kryukov 16-Nov-11 15:41pm    
Please don't post such things as solution, just add a comment, reply to existing comment of use "Improve question".
--SA
Sergey Alexandrovich Kryukov 16-Nov-11 15:41pm    
OP commented:

Tried this as well and they both come back as nothing

Dim tType As Type = Me.GetType()
Dim pi As PropertyInfo = tType.GetProperty("bTest")
Dim fi As FieldInfo = tType.GetField("bTest")
Sergey Alexandrovich Kryukov 16-Nov-11 15:42pm    
Same as above. What's the type of Me?
--SA
You could use Reflection. This[^] discussion might give you some ideas.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900