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. Air Langhi

    Air Langhi Contributing Member

    Joined:
    Aug 26, 2000
    Messages:
    21,937
    Likes Received:
    6,686
    I am not VB expert, but I think your error is due to setting an int variable to a text string: invalidcastexception


    Dim intNumber As Integer
    intNumber = txtNumber.Text

    intNumber can take a value 1,2,3,etc

    txtNumber.Text

    is text so it takes on values like "1","2","3", etc. "1" not equal 1

    so what you need to do is parse the string

    change this line:

    intNumber = txtNumber.Text

    to

    If Not Integer.TryParse(txtNumber.Text, intNumber ) Then
    intNumber = -1
    End If
     
  2. professorjay

    professorjay Member

    Joined:
    Oct 20, 2006
    Messages:
    9,676
    Likes Received:
    388
    It's been awhile since I've coded, but I think this along the lines of what cohete and heypartner mentioned. You should check what the input is first.

    if not integer then

    don't do anything (or go the extra step and create a message like 'please enter an integer between 1-10)

    else

    code to convert integer to roman numerals here

    It goes with the territory that error handling is a part of programming user interfaces.
     
  3. Cohete Rojo

    Cohete Rojo Member

    Joined:
    Oct 29, 2009
    Messages:
    10,344
    Likes Received:
    1,203
    I concur with Air Langhi said. Since this is VB you can probably find some help MSDN.
     
  4. H-TOWNSFINEST

    H-TOWNSFINEST Member

    Joined:
    Apr 14, 2008
    Messages:
    714
    Likes Received:
    106
    w3schools.com got me through my programming classes in college.
     
  5. SwoLy-D

    SwoLy-D Member

    Joined:
    Jul 20, 2001
    Messages:
    37,618
    Likes Received:
    1,456
    I am not a VB expert, haven't coded in any Windows-based language in a while, but I would agree with most peeps here that you need to declare ALL your variables and their type before using them or even assigning a value to them in memory.

    Lastly, have an "if anything else I haven't already covered" option just in case none of the explicitly suggested ones are not chosen.

    For example:

    if something,
    do this
    else if something else
    do that
    else if something else
    do something more
    for everything else
    do this
    end

    ^ Something like that should cover you, even if the conditions above aren't met. :eek: It's just covering your butt.
     
  6. StupidMoniker

    StupidMoniker I lost a bet

    Joined:
    Jul 18, 2001
    Messages:
    16,120
    Likes Received:
    2,812
    Sweatiest owner in NBA history?
     
  7. HR Dept

    HR Dept Member

    Joined:
    May 31, 2012
    Messages:
    6,792
    Likes Received:
    1,223
    I was a CIS major for three years and took two courses on VB. I can't tell you exactly how to fix it, but is there something that you can put into the IF statement that commands what to do if there's no input? IDK, but that's the logic that I would use to try to figure it out.

    Either that or ditch the integer conversion as suggested. Why are you converting to integers to begin with?
     
  8. Dr of Dunk

    Dr of Dunk Clutch Crew

    Joined:
    Aug 27, 1999
    Messages:
    46,615
    Likes Received:
    33,599
    Like others have said, it's because you're attempting to convert an empty string into an int. Air Langhi's approach should work. Another approach would be to only execute the TextChanged event code if txtNumber.Text <> "", but I like Air Langhi's better since it checks to see if the number is specifically an integer.
     

Share This Page