The functions below will do what you want, but you may want to consider
doing something like this instead -
Public Shared Readonly A As New KeyValuePair(Of String, Integer)("A", 0)
Public Shared Readonly B As New KeyValuePair(Of String, Integer)("B", 1)
Public Shared Readonly C As New KeyValuePair(Of String, Integer)("C", 2)
Paul
-----
(Imports System.Reflection)
Public Shared Function GetFieldValue(Of ReturnType)(ByVal Type As Type,
ByVal FieldName As String) As ReturnType
Dim ret As ReturnType = Nothing
Dim b As BindingFlags = BindingFlags.NonPublic Or BindingFlags.Public Or
BindingFlags.Static
Dim fi As FieldInfo = Type.GetField(FieldName, b)
If fi Is Nothing Then Throw New ArgumentException(String.Format("{0} not
found on type", FieldName))
If Not fi.FieldType Is GetType(ReturnType) Then Throw New
ArgumentException(String.Format("{0} is not of type {1}", FieldName,
GetType(ReturnType).Name))
Dim val As Object = fi.GetValue(Nothing)
If Not val Is Nothing Then ret = DirectCast(val, ReturnType)
Return ret
End Function
Public Shared Function GetFieldValue(Of ReturnType)(ByVal o As Object,
ByVal FieldName As String) As ReturnType
Dim ret As ReturnType = Nothing
Dim b As BindingFlags = BindingFlags.NonPublic Or BindingFlags.Public Or
BindingFlags.Static Or BindingFlags.Instance
Dim fi As FieldInfo = o.GetType.GetField(FieldName, b)
If fi Is Nothing Then Throw New ArgumentException(String.Format("{0} not
found on type", FieldName))
If Not fi.FieldType Is GetType(ReturnType) Then Throw New
ArgumentException(String.Format("{0} is not of type {1}", FieldName,
GetType(ReturnType).Name))
Dim val As Object = fi.GetValue(o)
If Not val Is Nothing Then ret = DirectCast(val, ReturnType)
Return ret
End Function
"Travis" <ca********@comcast.netwrote in message
news:%2***************@TK2MSFTNGP06.phx.gbl...
>I have a question about how to get this to work:
I have the following:
Const A As Integer = 0
Const B As Integer = 1
Const C As Integer = 2
etc...
I have a textbox (txtInput) that people can type in. Say they type in "AB"
and hit the button to calculate. I want it to be able to take the string
(strText) (which contains the letters AB) and convert that text into a
form where it recognizes the letters as constants and not a string. AB = 0
(0*1), but I can't get there because it only sees the text "AB." Its like
writing "class" and class. The first is a string, the other is a keyword.
How do I take the string and get it to see that it's a bunch of constants,
not a string?
Do you get what I'm trying to ask? If they enter in ABC it to do the math
of 0*1*2 and output that, not output the text ABC again.
There must be a simple way of doing this that I'm not thinking of?
Thanks!