1. Welcome! Please take a few seconds to create your free account to post threads, make some friends, remove a few ads while surfing and much more. ClutchFans has been bringing fans together to talk Houston Sports since 1996. Join us!

Anyone here familiar with Visual Basic? I'm pretty desperate....

Discussion in 'BBS Hangout' started by Swishh, Sep 30, 2014.

  1. Swishh

    Swishh Member

    Joined:
    Mar 4, 2013
    Messages:
    2,009
    Likes Received:
    63
    Taking a beginning Visual Basic course and have been working on this program for the past 3 hours or so, and I'm stuck on the last part of it, which is to convert textbox entries 1-10 into Roman Numerals. Whenever I run the program and enter '1' in the textbox, the Roman Numeral "I" shows up in the listbox below.

    However, after I backspace and try to enter a different value, my program crashes and says "invalidcastexception was unhandled' -- wth is wrong? I don't understand.



    Private Sub txtNumber_TextChanged(sender As Object, e As EventArgs) Handles txtNumber.TextChanged

    'declare variables

    Dim intNumber As Integer
    intNumber = txtNumber.Text

    If intNumber = 1 Then
    lblResult.Text = "I"
    ElseIf intNumber = 2 Then
    lblResult.Text = "I"

    End If
     
  2. Invisible Fan

    Invisible Fan Member

    Joined:
    Dec 5, 2001
    Messages:
    45,954
    Likes Received:
    28,046
  3. Swishh

    Swishh Member

    Joined:
    Mar 4, 2013
    Messages:
    2,009
    Likes Received:
    63
    Believe me, I've went to google before coming on here.

    I don't understand what you mean though. What part of the code is wrong? Seems like what i have so far is pretty straight forward.
     
  4. RedRedemption

    RedRedemption Member

    Joined:
    Jul 21, 2009
    Messages:
    32,542
    Likes Received:
    7,752
    If intNumber = 1 Then
    lblResult.Text = "I"
    ElseIf intNumber = 2 Then
    lblResult.Text = "I"

    Why is 2 resulting in "I"?

    Also wow VBasic's syntax looks horrendous.
     
  5. Invisible Fan

    Invisible Fan Member

    Joined:
    Dec 5, 2001
    Messages:
    45,954
    Likes Received:
    28,046
    Because he couldn't get that far...

    Using the answer with two votes, do a check before you feed your method values.

    1 ::beep:beep:boop:: I
    Backspace ::beep:beep:boop:: null/empty BOOM
    2 ::beep:beep:boop:: II

    So the answer gave an if-else condition where if it fit the condition the programmer wanted, then it would feed that value to the method.

    Try to pretend that computers are really dumb, so when you do a backspace, it thinks you're ready and it'll try to compute the new value (blank/null)
     
  6. Swishh

    Swishh Member

    Joined:
    Mar 4, 2013
    Messages:
    2,009
    Likes Received:
    63
    just a typo, it is written correctly on my code - it is supposed to be lblResult.Text = "II"


    so thats not whats giving me this crash/error.
     
  7. Swishh

    Swishh Member

    Joined:
    Mar 4, 2013
    Messages:
    2,009
    Likes Received:
    63
    no, just a typo. and i'm not exactly sure what the bottom part of your reply is supposed to mean.
     
  8. RedRedemption

    RedRedemption Member

    Joined:
    Jul 21, 2009
    Messages:
    32,542
    Likes Received:
    7,752
    Not familiar with visual basic, but with Java you'd need a loop to handle multiple inputs.

    EDIT: Quick google search shows this is not the problem.

    Instead of backspacing, is there a way to clear the text input without keyboard input?
    Something along the lines of resetting the variables or a .clear() method?
     
    #8 RedRedemption, Sep 30, 2014
    Last edited: Sep 30, 2014
  9. csnerd84

    csnerd84 Member

    Joined:
    Jan 12, 2003
    Messages:
    703
    Likes Received:
    592
    I don't know Visual Basic very well, but I suspect your problem is this line:

    intNumber = txtNumber.Text

    When you hit backspace after entering a number, the value is empty text and this line is not able to convert the empty text to integer.
     
  10. Swishh

    Swishh Member

    Joined:
    Mar 4, 2013
    Messages:
    2,009
    Likes Received:
    63

    Ok, so that does make sense to me - I can see how this is causing the confusion to the program.

    However, how do i fix that - how do i make it so that the backspace doesn't make the program crash? i'm not familiar with blank/null
     
  11. RedRedemption

    RedRedemption Member

    Joined:
    Jul 21, 2009
    Messages:
    32,542
    Likes Received:
    7,752
    Instead of backspacing, is there a way to clear the text input without keyboard input?
    Something along the lines of resetting the variables or a clear() method?
     
    #11 RedRedemption, Sep 30, 2014
    Last edited: Sep 30, 2014
  12. Swishh

    Swishh Member

    Joined:
    Mar 4, 2013
    Messages:
    2,009
    Likes Received:
    63
    well, i could make a clear button - but i'm supposed to have the result pop up simultaneously to the input.
     
  13. Invisible Fan

    Invisible Fan Member

    Joined:
    Dec 5, 2001
    Messages:
    45,954
    Likes Received:
    28,046
    First off, I don't know Visual Basic. I think Ruby or Python would be a better intro to CS.

    https://encrypted.google.com/search?hl=en&q=isNullOrEmpty visual basic

    Learning programming can be like swimming in an ocean. Just think of checks like those as libraries people before you added to make life simpler for playing around certain data types or objects because it's been done again and again.

    Dog.doesRollOver?
    Dog.doesLickBalls?

    In this case...
    String.isNullorEmpty

    Then there's a bunch of libraries or documents to those libraries on how they work.

    Gluck!
     
  14. RedRedemption

    RedRedemption Member

    Joined:
    Jul 21, 2009
    Messages:
    32,542
    Likes Received:
    7,752
    Can you print screen your entire code and upload it here?

    Also, is this your first time programming? Because you may have picked the wrong language to start lol.
     
  15. joepu

    joepu Member

    Joined:
    Nov 15, 2012
    Messages:
    231
    Likes Received:
    6
    Not familiar with VB either but from your code, it seems you have a from with a textbox and you have put your code on the textchanged event. So it is probably what invisible fan is saying, it's trying to process an empty string and throwing an error.

    Maybe try putting in a Msgbox just to test out when the code is triggered.

    Private Sub txtNumber_TextChanged(sender As Object, e As EventArgs) Handles txtNumber.TextChanged

    'declare variables

    Dim intNumber As Integer

    MsgBox(txtNumber.Text)

    intNumber = txtNumber.Text

    If intNumber = 1 Then
    lblResult.Text = "I"
    ElseIf intNumber = 2 Then
    lblResult.Text = "I"

    End If

    If you get a blank message box after doing backspace then that gives you your answer. When you clear the textbox, the code is triggered. In that case you should put in some code to test and see if txtNumber.Text is empty or null before doing the assignment intNumber = txtNumber.Text
     
  16. heypartner

    heypartner Member

    Joined:
    Oct 27, 1999
    Messages:
    63,510
    Likes Received:
    59,001
    I agree with others saying it's cast exception, because your text box can't convert a blank to an integer.

    OP, don't convert the textbox to integer at all. Your program description actually says you are converting textbox entries to Roman Numerals, not integers, anyhow. Try this:

    'no variables needed

    If textNumber.Text = "1" Then
    lblResult.Text = "I"
    ElseIf textNumber.Text ="2" Then
    lblResult.Text = "II"
    ' and so one
    Else
    ' no valid numeral found. Please enter 1-10

    End If

    If for whatever reason, you actually need to use an Integer, you need to check if that textBox can be converted to an integer or not. But, I say, forget that step and just look for string versions of "1", "2" to "10", else invalid input.

    If you want to get check for proper input, you could check if it is a valid text version of a number, then check if it between 1 and 10. like this:

    If Integer.TryParse(textNumber.Text, 0) AndAlso ( textNumber.Text > 0 And textNumber.Text < 11) Then
    ' success, an integer from 1-10
    If textNumber.Text = "1" Then
    lblResult.Text = "I"​
    ElseIf textNumber.Text ="2" Then
    lblResult.Text = "II"​
    ' and so one
    Else
    ' input not a valid Integer, tell user to try again.
    End If
     
    #16 heypartner, Oct 1, 2014
    Last edited: Oct 1, 2014
  17. CometsWin

    CometsWin Breaker Breaker One Nine

    Joined:
    May 15, 2000
    Messages:
    28,028
    Likes Received:
    13,051
  18. fallenphoenix

    fallenphoenix Member

    Joined:
    Jun 20, 2009
    Messages:
    9,821
    Likes Received:
    1,619
    i'm no coding expert. is it crashing if you backspace and put either 1 or 2? if not then your code just doesn't know how to handle anything else other than 1 or 2 and freaks out.
     
  19. Cohete Rojo

    Cohete Rojo Member

    Joined:
    Oct 29, 2009
    Messages:
    10,344
    Likes Received:
    1,203
    It sounds like you are producing an error, so just write some error handling code.
     
  20. cebu

    cebu Member

    Joined:
    Dec 16, 2013
    Messages:
    1,663
    Likes Received:
    587
    <iframe width="420" height="315" src="//www.youtube.com/embed/8To-6VIJZRE" frameborder="0" allowfullscreen></iframe>
     

Share This Page