Math with letters

  • Thread starter Thread starter Travis
  • Start date Start date
T

Travis

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!
 
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
 
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!

If the text letters are only A - Z and have the values 0 - 25, you can
convert the text to an integer:

Dim xx As String = "ABC"
Dim rslt As Integer = 0

For Each chr As Char In xx.ToUpper().ToCharArray()
rslt *= Asc(chr) - Asc("A"c)
Next

You would also need to add checking for invalid characters.

If the set of legal characters includes other characters, or if the
values do not map linearly with the letters, you might want to build a
table of legal values:

Dim dict as New Dictionary(Of Char, Integer)

With dict
.Add("A"c, 0)
.Add("B"c, 1)
...
.Add("%"c, 99)
End With

For Each chr As Char In xx.ToUpper().ToCharArray()
If dict.ContainsKey(chr)
rslt *= dict(chr)
Else
' Invalid character
Next
 
Is there any reason to use variable names to find the required values to be
multiplied?

If the association of values to letters is arbitrary (for instance, not
based on the letter's ASCII code) the following example shows one way that
it can be done without using variable names:
Public Class Form1

Dim myLabels() As Char = {"A"c, "B"c, "C"c, "D"c, "E"c, "F"c} 'etc.

Dim myValues() As Integer = {0, 1, 2, 3, 4, 5} 'etc.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim a() = TextBox1.Text.ToCharArray

Dim result As Integer = 1

For Each c In a

result = result * myValues(Array.IndexOf(myLabels, c))

Next

textbox2.text = result.ToString

End Sub
 
Back
Top