Case Usage

I am pretty much a noob programmer, but I'm getting the hold of visual basic pretty much.

But in one of my programming attempts something is messed up, because as far as I know my code is good...

Take this code:


Answer this question

Case Usage

  • dmbrubac

    wait actually youre right

    i get an error when i enter something that does work

    thats weird, because before i used any case expressions it worked just fine

    so i should take out the integer parse from select case




  • Kevin Dente

    Haha I figured it out

    I should be using Case Is

    how dumb of me...


  • _MMM_

    Try

    Select Case Integer.Parse(TextBox2.Text)
    Case Is < 200
    MessageBox.Show("The browser size is too small. Please revise your setting.")
    Case Is > 760
    MessageBox.Show("The browser size is too big. Please revise your setting.")
    End Select
    Select Case Integer.Parse(TextBox1.Text)
    Case Is < 200
    MessageBox.Show("The browser size is too small. Please revise your setting.")
    Case Is > 1016
    MessageBox.Show("The browser size is too big. Please revise your setting.")
    End Select

    If this doesn't work, make sure that the value typed in textbox2/textbox1 is a value that should give you the message.

    Also, if these are user entry fields you need to check before using Integer.Parse--it will fail if the user leaves the textboxes blank, for example.


  • Corrado Cavalli

    If you use the Catch...Try then in your exception statements, you could set the size value to an acceptable value (for example, default is 500) and then go on as if they had entered that value.
  • jmass17

    thanks but one last question

    If i tried doing a case what would I do (to solve the user entry thing)

    It says relational operator expected


  • Raymundo Chapa94595

    are you sure about that
    because the reason why I did that is because when I entered bad entries it STILL executed the code...


  • K_L

    It doesnt crash the program or anything if the guy puts letters or leaves it blank, but I could do:

    1) make a masked text box
    2) use cases and say that if its alphabet characters or blank then messagebox.show Write an f-ing number!!!

    right (im a noob)


  • Santiagon

    also to fix the user entry problem

    I did

    Select Case Textbox1.Text
    Case Is not integer**

    The starred is what i want to do, what would be the actual expression to use

    Case Is Integer= False i tried
    Case Is Not Integer I tried

    whats the expression i need to use


  • John Portnov

    If you would use a couple of NumericUpDown controls it would solve all of your problems....you would not have to worry about upper and lower bounds because you can set the limits of the control...you would not have to worry about errouneous entries...such as alpha characters etc....You also would not have to worry about your implicit conversions from string to integer....You should always use option strict ON...You also need to understand the select case statement...it finds the first true case and then executes the code within that case and then exits.....in your sample above the third statemnt does not make sense because if the value was greater than 760 or 1016 it would be caught in the second statement so the equivelint of what you are trying to accomplish would be

    Case is <200

    Case Is > 760

    Case Is > 200

    Any value that is greater than 200 and less than 760 will be caught by the third statemnt because if it was greater than 760 it would be caught by the second statement....

    HTH



  • TYTYTY

    You could trap the error--

    Try

    Select Case Integer.Parse(TextBox2.Text)

    Case Is < 200

    MessageBox.Show("The browser size is too small. Please revise your setting.")

    Case TextBox2.Text > 760

    MessageBox.Show("The browser size is too big. Please revise your setting.")

    End Select

    Select Case Integer.Parse(TextBox1.Text)

    Case TextBox1.Text < 200

    MessageBox.Show("The browser size is too small. Please revise your setting.")

    Case TextBox1.Text > 1016

    MessageBox.Show("The browser size is too big. Please revise your setting.")

    End Select

    Catch FormatEx As FormatException

    MsgBox("The value you entered could not be converted to a number.")

    Catch ex As Exception

    MsgBox(ex.ToString)

    End Try

    Or, you could use a numeric up/down or, as you said, a masked box or some other control to force them to enter numbers.


  • SRbake1311

    alright i copied your try thing and combined the entire code for the button
  • Pocketmnky

    im getting some more problems now

    something is screwed up

    i enter something that should be valid (232 by 232) and it says its too big

  • CoffeeAintDoinIt

    The issue now is that without converting this to a number, it is comparing the string value you enter rather than the numeric value.

    One way to avoid a lot of these issues is to:

    1) Give the user a combobox of acceptable values or

    2) Use VAL(Textbox1.Text) and VAL(Textbox2.Text)


  • K007

    the Val thing did the trick

    thanks Matt

    everything is good, thanks for your help both of you...

    the problem was that I just used the string value...I needed Val...


    To Dman:

    Maybe youre talking about Case Else
    originally i didnt use case else, I just stuck it after the select case and end case statements
    after using Case Else it worked

    thanks again..




  • Case Usage