4 Replies - 586 Views - Last Post: 17 February 2013 - 11:02 PM Rate Topic: -----

#1 AlexOde  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 17-February 13

Can't get If-then-else to work

Posted 17 February 2013 - 04:30 PM

Hi all

I know that you are not to do my homework for me, but I've tried everything, EVERYTHING!
I am new to programming, and now I've started using the if-then-else function, but trying to make a little code just won't work.
Here is the code:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim age As Integer

        age = txtAge.Text

        If age <= 17 Then
            MsgBox("Young.")
        ElseIf age <= 66 Then
            MsgBox("Adult.")
        ElseIf age <= 149 Then
            MsgBox("Old.")
            MsgBox("And retired.")
        Else
            MsgBox("You can only write numbers in the text-field.")
        End If
    End Sub
End Class



I've tried to change the Dim age to string, boolean and integer, but it's still the same, I still get the same error message, as it won't get past the first test - in this case, the [If age <= 17 Then] - it crashes.
I've tried to set the Else-message on the top, but I still can't get it to work. I've used several hours to make it work, but I just can't...

It's only a tiny part of my homework, but I've re-read my lecture several times, and checked on other sites, and it's supposed to work!
Please help!!

This post has been edited by Martyr2: 17 February 2013 - 04:52 PM
Reason for edit:: Fixed code tags, Remember it is [code]code here[/code] :)


Is This A Good Question/Topic? 0
  • +

Replies To: Can't get If-then-else to work

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4399
  • View blog
  • Posts: 12,255
  • Joined: 18-April 07

Re: Can't get If-then-else to work

Posted 17 February 2013 - 04:59 PM

Well first of all the code should be working. Other than the fact you are trying to dump a text string into "age" it should still work if you don't have Option Explicit On.

Can you tell us what error messages you are getting and how it is crashing?

As a side note, this appears to be VB.NET code you are doing, not VB6 code. So I will move it to the appropriate forum. In VB.NET we do not use MsgBox anymore and it is MessageBox.Show() instead. If you are using VB6, then that could be part of your problem.

Either way, if you can tell us more about the error you are getting it will help. :)
Was This Post Helpful? 0
  • +
  • -

#3 IronRazer  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 342
  • View blog
  • Posts: 1,016
  • Joined: 01-February 13

Re: Can't get If-then-else to work

Posted 17 February 2013 - 05:54 PM

Does it give you an error before the program even starts because, I am using VB.Net and this code works fine for me. What visual basic version are you using?
Was This Post Helpful? 0
  • +
  • -

#4 AlexOde  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 17-February 13

Re: Can't get If-then-else to work

Posted 17 February 2013 - 09:23 PM

Marty, sorry, didn't understand the coding (pun intended) :P

Anyway, it's VB.NET code I'm using in Visual Studio 2012
The program starts without any error, and it gives me the correct message-boxen when I input numbers, but when I don't write anything or write letter, it gives me:
Conversion from string "" to type 'Integer' is not valid.
NOTE: This is when Dim age as Integer is used.

When changing the Dim age to Dim age as String, I get the following error message:
Conversion from string "" to type 'Double' is not valid.

We have been informed by our teacher to use MsgBox(), and I have not had any issues with this previously.
This is really starting to grind my gears...

If any more information is required, please don't hesitate to tell me so :)
Was This Post Helpful? 0
  • +
  • -

#5 IronRazer  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 342
  • View blog
  • Posts: 1,016
  • Joined: 01-February 13

Re: Can't get If-then-else to work

Posted 17 February 2013 - 11:02 PM

Sorry for the delay. I understand your problem now. The txtAge.text should be converted to an integer before the value is assigned to the age variable. This can be done with the val() command. For example if you have an integer called X you can use X = Val(String). String could be a string variable or the text from a textbox. The way you are doing it is trying to put a string from the textbox into an integer that is ment for number values. I know this is homework and i don`t want to just give you the code but, from what i just told you you should be able to figure it out. If not i will try to explain it again until you understand.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1