Math with letters

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Travis

    Math with letters

    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!

  • PJ6

    #2
    Re: Math with letters

    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.Reflecti on)

    Public Shared Function GetFieldValue(O f ReturnType)(ByV al Type As Type,
    ByVal FieldName As String) As ReturnType
    Dim ret As ReturnType = Nothing
    Dim b As BindingFlags = BindingFlags.No nPublic Or BindingFlags.Pu blic Or
    BindingFlags.St atic
    Dim fi As FieldInfo = Type.GetField(F ieldName, b)
    If fi Is Nothing Then Throw New ArgumentExcepti on(String.Forma t("{0} not
    found on type", FieldName))
    If Not fi.FieldType Is GetType(ReturnT ype) Then Throw New
    ArgumentExcepti on(String.Forma t("{0} is not of type {1}", FieldName,
    GetType(ReturnT ype).Name))
    Dim val As Object = fi.GetValue(Not hing)
    If Not val Is Nothing Then ret = DirectCast(val, ReturnType)
    Return ret
    End Function

    Public Shared Function GetFieldValue(O f ReturnType)(ByV al o As Object,
    ByVal FieldName As String) As ReturnType
    Dim ret As ReturnType = Nothing
    Dim b As BindingFlags = BindingFlags.No nPublic Or BindingFlags.Pu blic Or
    BindingFlags.St atic Or BindingFlags.In stance
    Dim fi As FieldInfo = o.GetType.GetFi eld(FieldName, b)
    If fi Is Nothing Then Throw New ArgumentExcepti on(String.Forma t("{0} not
    found on type", FieldName))
    If Not fi.FieldType Is GetType(ReturnT ype) Then Throw New
    ArgumentExcepti on(String.Forma t("{0} is not of type {1}", FieldName,
    GetType(ReturnT ype).Name))
    Dim val As Object = fi.GetValue(o)
    If Not val Is Nothing Then ret = DirectCast(val, ReturnType)
    Return ret
    End Function


    "Travis" <captaint03@com cast.netwrote in message
    news:%23CwdcksN JHA.728@TK2MSFT NGP06.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!

    Comment

    • Jack Jackson

      #3
      Re: Math with letters

      On Sat, 25 Oct 2008 13:42:46 -0400, "Travis" <captaint03@com cast.net>
      wrote:
      >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().To CharArray()
      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().To CharArray()
      If dict.ContainsKe y(chr)
      rslt *= dict(chr)
      Else
      ' Invalid character
      Next

      Comment

      • James Hahn

        #4
        Re: Math with letters

        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(B yVal sender As System.Object, ByVal e As
        System.EventArg s) Handles Button1.Click

        Dim a() = TextBox1.Text.T oCharArray

        Dim result As Integer = 1

        For Each c In a

        result = result * myValues(Array. IndexOf(myLabel s, c))

        Next

        textbox2.text = result.ToString

        End Sub


        "Travis" <captaint03@com cast.netwrote in message
        news:%23CwdcksN JHA.728@TK2MSFT NGP06.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!

        Comment

        Working...