VB.net question

Discussion in 'OT Technology' started by morgue, Jun 5, 2006.

  1. morgue

    morgue New Member

    Joined:
    Jul 10, 2004
    Messages:
    8,278
    Likes Received:
    0
    Can I use "If" with a string?
    I'm trying but it won't work...
    Something like
    If myString = "A" Then

    :dunno:

    It works with = 1
    so I don't know
     
  2. Runescaper

    Runescaper Guest

    I don't see any reason why you couldn't. If possible, can you post the exact code that's giving you problems?
     
  3. morgue

    morgue New Member

    Joined:
    Jul 10, 2004
    Messages:
    8,278
    Likes Received:
    0
    Code:
            TextBox4.Text = ""
            If N = 16 Then
                Dim number16s As String = TextBox2.Text
                For i = 0 To number16s.Length - 1 Step 1
                    current = CInt(Val(number16s.Chars(i)))
                    If current = "A" Then
                        TextBox4.Text += "1010"
                    ElseIf current = 1 Then
                        TextBox4.Text += "0001"
                    ElseIf current = 2 Then
                        TextBox4.Text += "0010"
                    End If
                Next
            End If
    
     
  4. morgue

    morgue New Member

    Joined:
    Jul 10, 2004
    Messages:
    8,278
    Likes Received:
    0
    It's a conversor from hex to binary
     
  5. w0lverine

    w0lverine New Member

    Joined:
    Feb 22, 2005
    Messages:
    47
    Likes Received:
    0
    The Val() of an single character (a-z,A-Z) is zero - I think VB does not recognize it as Hex-digit, therefore this is not going to work.... maybe you could try the simple way :

    Code:
            TextBox2.Text = ""
            If N = 16 Then
                Dim number16s As String = TextBox1.Text
                For i = 0 To number16s.Length - 1 Step 1
                    current = number16s.Chars(i)
                    If current = "A" Then
                        TextBox2.Text += "1010"
                    ElseIf current = "1" Then
                        TextBox2.Text += "0001"
                    ElseIf current = "2" Then
                        TextBox2.Text += "0010"
                    End If
                Next
            End If
    
    :hsd:
     
  6. Frequency

    Frequency New Member

    Joined:
    Dec 30, 2004
    Messages:
    7,504
    Likes Received:
    0
    Location:
    PA
    Code:
    TextBox4.Text = ""
            If N = 16 Then
       Dim number16s As String = TextBox2.Text
                    For i = 0 To number16s.Length - 1 Step 1
                           [COLOR=Red]current [/COLOR]= CInt(Val(number16s.Chars(i)))
          If current = [COLOR=Red]"A"[/COLOR] Then
                                  TextBox4.Text += "1010"
                           ElseIf current = 1 Then
                                  TextBox4.Text += "0001"
                           ElseIf current = 2 Then
                                  TextBox4.Text += "0010"
                           End If
                    Next
            End If
    current is an int you can not compare a string to an in like you could in 6.0 you would need to convert current to a string in your compare using the .ToString method.

    Might i also reconmend a switch statement over all the ifs
     
  7. morgue

    morgue New Member

    Joined:
    Jul 10, 2004
    Messages:
    8,278
    Likes Received:
    0
    This worked number16s.Chars(i), thanks a lot :big grin:
     

Share This Page