-2

Hi I would like to ask a question guys I am doing an application in vb.net and my if else is acting weird instead of stating the correct caption in the label it shows the else statement instead which is "Do you have anything you want to say other than that?"... Thanks in advance guys :) here is my code sirs:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    If TextBox1.Text = "What is your name?" Then
        Label1.Text = "Hi there kid! I will tell you later"
    Else
        Label1.Text = "Do you have anything you want to say other than that?"
        If TextBox1.Text = "Hi" Then
            Label1.Text = "Hi there also!"

        Else
            Label1.Text = "Do you have anything you want to say other than that?"
        End If
    End If
End Sub 
chris_techno25
  • 2,401
  • 5
  • 20
  • 32
TheNewbie
  • 112
  • 1
  • 6
  • 15
  • You should look at `String.Compare` or `String.Equals`. `If String.Equals( Label1.Text, "What is your name?" ) Then ...`. http://stackoverflow.com/questions/900927/comparing-strings-in-vb – clcto Jan 31 '14 at 00:19
  • 1
    Make sure there aren't any spaces and your string is completely the same as the one entered in the textbox, and that includes upper case or lower case letters. – chris_techno25 Jan 31 '14 at 00:21
  • @chris_techno25 yes there were no spaces as you can see on my code and it is exactly the same as the other one that is why it bothers me why it acts weird :S – TheNewbie Jan 31 '14 at 00:23
  • What did you enter in the text box? – logixologist Jan 31 '14 at 00:24
  • @logixologist What is your name? that is what i entered sir – TheNewbie Jan 31 '14 at 00:25
  • It works fine on my side. Here's what you'll have to do... Instead of TextBox1.Text, make it TextBox1.Text.Trim()... Tell me if it works or not... I have another fix for that... – chris_techno25 Jan 31 '14 at 00:27
  • Why is your end if all the way down their... it looks as if you are nesting the IF's but I dont think thats what you are intending. Your structure should be IF/END IF or IF/ELSE/END IF – logixologist Jan 31 '14 at 00:28
  • Its setting the textbox correctly first. Then you are asking the second question where it is correctly setting the ELSE – logixologist Jan 31 '14 at 00:29
  • @chris_techno25 still the same sir :( – TheNewbie Jan 31 '14 at 00:29
  • @logixologist before it was if/else/end if the only reason that i put two else their because awhile ago when i tried if/else/end if the result is still the same... – TheNewbie Jan 31 '14 at 00:30
  • If you put a breakpoint in your code and you step through it... you will see its doing it exactly the way you coded it. You are saying If the text box says: "What is your name" then do something. If its not then do something else. Then you say if the text box is "Hi" do something, if not do something else. It is doing both things. Does that make more sense? – logixologist Jan 31 '14 at 00:33
  • 1
    Access your project folder, head to the debug folder and delete everything there that can be deleted, ignore anything that can't be deleted because it's loaded in the memory. I don't see anything wrong with the code, if your code is acting funny, Visual Studio might have not replaced the one in the debug folder with the new one. This is a common problem in Visual Studio... – chris_techno25 Jan 31 '14 at 00:34
  • @chris_techno25 okay sir i will try that and i will tell you what is the result. – TheNewbie Jan 31 '14 at 00:35
  • @chris_techno25, its a logical error... has nothing to do with Visual Studio. – logixologist Jan 31 '14 at 00:35
  • @chris_techno25 problem SOLVED!. thanks a lot from the bottom of my heart :) – TheNewbie Jan 31 '14 at 00:37
  • @logixologist The code is working on my side. There's nothing wrong with the code if it's what he wants. The code is acting funny, there's a possibility that the app that's running is the previously created one, not this new one...SEE? Problem solved! – chris_techno25 Jan 31 '14 at 00:37
  • @logixologist and chris_techno25 thanks for your time and effort in answering my query..again many thanks you saved my life :) – TheNewbie Jan 31 '14 at 00:41
  • @logixologist It doesn't always happen but when it does, it leaves everyone believing there's something wrong with the code... but there isn't. When I encountered this problem, I went crazy checking my code over and over again :) – chris_techno25 Jan 31 '14 at 00:42
  • No Problem.... im still puzzled at the logic in the code. Did you end up also changing it to my new code and then did the deleting of the files? – logixologist Jan 31 '14 at 00:42
  • @TheNewbie No problem man, logixologist and I are happy to help :) – chris_techno25 Jan 31 '14 at 00:45
  • @logixologist actually i changed my code a little bit because I added more codes and now it brings up another problem :p I will post it in a minute sir..Thanks again! – TheNewbie Jan 31 '14 at 00:52
  • @TheNewbie If it's not any trouble, I posted an answer, please do mark my post as answered and I change the title of your problem so other people can find this helpful. :) – chris_techno25 Jan 31 '14 at 00:57

2 Answers2

2

Access your project folder, head to the debug folder and delete everything there that can be deleted, ignore anything that can't be deleted because it's loaded in the memory. I don't see anything wrong with the code, if your code is acting funny, Visual Studio might have not replaced the one in the debug folder with the newly compiled one, which means the running app is the last one compiled before the error happened. This is a common bug in Visual Studio.

chris_techno25
  • 2,401
  • 5
  • 20
  • 32
0

I believe you intended to do this:

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If TextBox1.Text = "What is your name?" Then
    Label1.Text = "Hi there kid! I will tell you later"
Else
    Label1.Text = "Do you have anything you want to say other than that?"
End If


 If TextBox1.Text = "Hi" Then
 Label1.Text = "Hi there also!"
 End If
End Sub 
logixologist
  • 3,694
  • 4
  • 28
  • 46
  • still the same sir i already tried that awhile ago before i seek help here :( – TheNewbie Jan 31 '14 at 00:34
  • thanks sir problem now solved my deleting the files in the debug folder it is a bug from visual studio as what chris_techno25 has said..thanks again for your help :) – TheNewbie Jan 31 '14 at 00:38