9 Replies - 1212 Views - Last Post: 22 August 2014 - 03:20 PM Rate Topic: -----

#1 dschwartz   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 26-March 12

If ... AndAlso statements not working as expected

Posted 22 August 2014 - 11:04 AM

I'm not sure why this is not working. I see no reason for it. My code is below. It checks a textbox for the required input and changes a button's color based on whether or not it is there. Basically it makes sure that the proper service packs are checked for the respective OS's. The code for Vista works but the one for 7 does not. It turns the button red even when "Windows 7 Pro, SP1" is entered into the textbox. Any idea why?

If TextBox10.Text.Contains("7") AndAlso TextBox10.Text.Contains("SP1") Then

            Button2.BackColor = Color.LimeGreen

        Else : Button2.BackColor = Color.Red

        End If

 

If TextBox10.Text.Contains("Vista") AndAlso TextBox10.Text.Contains("SP2") Then

            Button2.BackColor = Color.LimeGreen

        Else : Button2.BackColor = Color.Red

        End If



Is This A Good Question/Topic? 0
  • +

Replies To: If ... AndAlso statements not working as expected

#2 BetaWar   User is offline

  • #include "soul.h"
  • member icon

Reputation: 1522
  • View blog
  • Posts: 8,301
  • Joined: 07-September 06

Re: If ... AndAlso statements not working as expected

Posted 22 August 2014 - 11:06 AM

Moved to VB.net since it isn't a tutorial.
Was This Post Helpful? 0
  • +
  • -

#3 dschwartz   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 26-March 12

Re: If ... AndAlso statements not working as expected

Posted 22 August 2014 - 11:06 AM

I'm not sure why this is not working. I see no reason for it. My code is below. It checks a textbox for the required input and changes a button's color based on whether or not it is there. Basically it makes sure that the proper service packs are checked for the respective OS's. The code for Vista works but the one for 7 does not. It turns the button red even when "Windows 7 Pro, SP1" is entered into the textbox. Any idea why?

If TextBox10.Text.Contains("7") AndAlso TextBox10.Text.Contains("SP1") Then

            Button2.BackColor = Color.LimeGreen

        Else : Button2.BackColor = Color.Red

        End If

 

If TextBox10.Text.Contains("Vista") AndAlso TextBox10.Text.Contains("SP2") Then

            Button2.BackColor = Color.LimeGreen

        Else : Button2.BackColor = Color.Red

        End If


Was This Post Helpful? 0
  • +
  • -

#4 dschwartz   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 26-March 12

Re: If ... AndAlso statements not working as expected

Posted 22 August 2014 - 11:42 AM

Okay, so I've fixed it by adding another instance of the "7" if statement underneath the "Vista" statement, but this seems highly inefficient. Any ideas as to why this doesn't work as expected?
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw   User is online

  • So Smart! S-M-R-T!
  • member icon

Reputation: 6578
  • View blog
  • Posts: 26,715
  • Joined: 12-December 12

Re: If ... AndAlso statements not working as expected

Posted 22 August 2014 - 11:46 AM

Work it through. If the text is "Windows 7 Pro, SP1" then, yes, it will turn the button-text LimeGreen, but it will also carry-on through the next if-statement. It won't paint it LimeGreen again, but it will paint it Red.

There is a debugging tutorial linked in my signature below. If you take this then you could step through your code.

You need an If..ElseIf block.

This post has been edited by andrewsw: 22 August 2014 - 11:47 AM

Was This Post Helpful? 0
  • +
  • -

#6 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1505
  • View blog
  • Posts: 3,810
  • Joined: 01-February 13

Re: If ... AndAlso statements not working as expected

Posted 22 August 2014 - 02:42 PM

Hi,

As andrewsw has stated, you need to use an If ElseIf block instead of separate If Then statements.

I also wanted to add that you may want to use the String.ToLower method to convert the text from the textboxes to lower case letters and then see if they contain the lower case "vista" and "sp2". That will make it so the check is not case sensitive. If the user types "Vista sp2" or "VISTA SP2" then it will still detect the words "vista" and "sp2" no mater what case the user types the words in.

One last thing i wanted to mention is that you don`t really need to use the "AndAlso" for your comparisons, you can just use "And". The "AndAlso" is for special cases like where you want to see if something exists before you try to check one of its properties in the same If statement. Kind of like this
        If TextBox1 IsNot Nothing AndAlso TextBox1.Text = "" Then

        End If


If you tried using the "And" for that If statement and the textbox is nothing then it would throw an exception trying to see if the text was equal to "" in the second part because, the textbox is nothing and does not exist.

Using the "AndAlso" in it makes it so if the TextBox is nothing then it will not check the second part of the If statement. That stops it from throwing an exception.

Anyways, this is an example of doing what i have mentioned.
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim txt As String = TextBox10.Text.ToLower

        If txt.Contains("7") And txt.Contains("sp1") Then
            Button1.BackColor = Color.LimeGreen
        ElseIf txt.Contains("vista") And txt.Contains("sp2") Then
            Button1.BackColor = Color.LimeGreen
        Else
            Button1.BackColor = Color.Red
        End If

    End Sub


This post has been edited by IronRazer: 22 August 2014 - 02:46 PM

Was This Post Helpful? 0
  • +
  • -

#7 andrewsw   User is online

  • So Smart! S-M-R-T!
  • member icon

Reputation: 6578
  • View blog
  • Posts: 26,715
  • Joined: 12-December 12

Re: If ... AndAlso statements not working as expected

Posted 22 August 2014 - 02:56 PM

This is a very minor point but I would prefer to check "vista" before "7".
Was This Post Helpful? 0
  • +
  • -

#8 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1505
  • View blog
  • Posts: 3,810
  • Joined: 01-February 13

Re: If ... AndAlso statements not working as expected

Posted 22 August 2014 - 03:09 PM

Quote

This is a very minor point but I would prefer to check "vista" before "7".


Just out of curiosity, is that because you don`t like windows 7 ? -.-
Was This Post Helpful? 0
  • +
  • -

#9 andrewsw   User is online

  • So Smart! S-M-R-T!
  • member icon

Reputation: 6578
  • View blog
  • Posts: 26,715
  • Joined: 12-December 12

Re: If ... AndAlso statements not working as expected

Posted 22 August 2014 - 03:13 PM

No (it's better than 8!).

I'm just extra-cautious. The value "7" might creep into the string at some point, perhaps as part of a version or serial number. Putting "vista" first removes this possibility.

But you probably knew what I meant ;)

This post has been edited by andrewsw: 22 August 2014 - 03:13 PM

Was This Post Helpful? 0
  • +
  • -

#10 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1505
  • View blog
  • Posts: 3,810
  • Joined: 01-February 13

Re: If ... AndAlso statements not working as expected

Posted 22 August 2014 - 03:20 PM

Quote

But you probably knew what I meant ;)

Yea, kind of. I was thinking we would be in trouble if sp7 or sp8 came out next week but, yes i had a feeling that was the idea of your suggestion. :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1