Results 1 to 9 of 9

Thread: If EndIf 4 possibilities

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2021
    Posts
    100

    If EndIf 4 possibilities

    Hi, this is my code that doesn't work. I see it only takes TextBox5.Text values to check and do not TextBox4.Text

    Code:
    Dim first2Chars = TextBox4.Text.Substring(0, 2)
    
      If ((Convert.ToInt32(TextBox5.Text)) = 0 And first2Chars.tostring IsNot "ST") Then
                MsgBox("YN")
            ElseIf ((Convert.ToInt32(TextBox5.Text)) = 0 And first2Chars.tostring Is "ST") Then
                MsgBox("YY")
            ElseIf ((Convert.ToInt32(TextBox5.Text)) <> 0 And first2Chars.tostring IsNot "ST") Then
                MsgBox("NN")
            ElseIf ((Convert.ToInt32(TextBox5.Text)) <> 0 And first2Chars.tostring Is "ST") Then
                MsgBox("NY")
      End If
    I have 4 possible states, Y=Yes, N=No. How to make this check actually work as I need it to?

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,754

    Re: If EndIf 4 possibilities

    IsNot and Is will not work here. You should be using = and <>. Side note: because you're converting the TextBox to an Integer, declare a variable for it to be stored in. And there's no need to call ToString on first2Chars since it is already a String. Finally, use AndAlso instead of And, AndAlso will not bother to evaluate the second condition if the first fails.

    Take a look at this example:
    Code:
    Dim first2Chars = TextBox4.Text.Substring(0, 2)
    Dim textbox5Conversion As Integer
    
    If (Not Integer.TryParse(TextBox5.Text, textbox5Conversion)) Then
        MessageBox.Show("TextBox5 is not a valid integer.")
        Return
    End If
    
    If (textbox5Conversion = 0 AndAlso first2Chars <> "ST") Then
        MessageBox.Show("YN")
    ElseIf (textbox5Conversion = 0 AndAlso first2Chars = "ST") Then
        MessageBox.Show("YY")
    ElseIf (textbox5Conversion <> 0 AndAlso first2Chars <> "ST") Then
        MessageBox.Show("NN")
    ElseIf (textbox5Conversion <> 0 AndAlso first2Chars = "ST") Then
        MessageBox.Show("NY")
    End If
    Edit - In free-typing this I missed the end quote on every "ST". I've updated the code to fix that. Also, after looking at it some more, the last ElseIf is unnecessary. You could replace it with an Else.
    Last edited by dday9; Jan 7th, 2022 at 10:09 AM.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: If EndIf 4 possibilities

    'Is' and 'IsNot' aren't meant for checking values (they are basically for checking if two object-variables have the same pointer).

    Use = and <> as you did in the other parts of the code.



    Also note that first2Chars is already a String, so doing first2Chars.ToString is wasteful



    edit: seems my brief distraction took longer than I thought!

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Dec 2021
    Posts
    100

    Re: If EndIf 4 possibilities

    It works, I've tried the code ... Thank You

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Dec 2021
    Posts
    100

    Re: If EndIf 4 possibilities

    Quote Originally Posted by si_the_geek View Post
    'Is' and 'IsNot' aren't meant for checking values (they are basically for checking if two object-variables have the same pointer).

    Use = and <> as you did in the other parts of the code.



    Also note that first2Chars is already a String, so doing first2Chars.ToString is wasteful



    edit: seems my brief distraction took longer than I thought!
    Thanks for the answer. I appreciate it

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,764

    Re: If EndIf 4 possibilities

    Quote Originally Posted by ivansmo View Post
    Thanks for the answer. I appreciate it
    Just an alternative,

    Code:
            Dim first2Chars As String = TextBox4.Text.Substring(0, 2)
            Dim TB5val As Integer
            If Integer.TryParse(TextBox5.Text, TB5val) Then
                Select Case TB5val
                    Case 0
                        If first2Chars <> "ST" Then
                            'YN
                        Else
                            'YY
                        End If
                    Case Else
                        If first2Chars <> "ST" Then
                            'NN
                        Else
                            'NY
                        End If
                End Select
            Else
                MessageBox.Show("TextBox5 is not a valid integer.")
            End If
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,041

    Re: If EndIf 4 possibilities

    You might also want to use String.StartsWith rather than using .SubString for this. Substring returns a string, which means creating a new object, whereas StartsWith returns a Boolean and is likely somewhat more efficient.
    My usual boring signature: Nothing

  8. #8
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,600

    Re: If EndIf 4 possibilities

    Quote Originally Posted by si_the_geek View Post
    (they are basically for checking if two object-variables have the same pointer).
    We have to be very careful with our words in statements like this when talking about the .Net environment. Is and IsNot are used to check for the difference between object references, not pointers. Objects can be moved around in memory during the execution of a .Net application which means their pointers can change while they are still the same objects.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  9. #9
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: If EndIf 4 possibilities

    Well yes, my caveat of "basically" wasn't really good enough... I was just trying to avoid confusing the thread starter.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width