Results 1 to 9 of 9

Thread: [RESOLVED] Conversion from string "" to type 'Integer' is not valid.

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2009
    Posts
    253

    Resolved [RESOLVED] Conversion from string "" to type 'Integer' is not valid.

    How can I convert this to integer?
    Code:
    ArrayCBSA(Index) = UnsortedListBox.Items(Val(Index))

    Theres a line indicating that UnsortedListBox.Items has error. How can I fix it?
    Last edited by cary1234; Jan 28th, 2011 at 07:51 AM. Reason: forgot something

  2. #2
    Fanatic Member stlaural's Avatar
    Join Date
    Sep 2007
    Location
    Quebec, Canada
    Posts
    683

    Re: Conversion from string "" to type 'Integer' is not valid.

    What exactly are you trying to convert to an int, the code line you show us doesn't give us much information to help you. And what is the error you are getting exactly? This kind of information will help us help you a lot faster and more efficiently.

    Based on the title of your thread, you are trying to convert an empty string "" to Integer. In order to convert your should have a value, if your string is Null or empty, converting will fail.
    Alex
    .NET developer
    "No. Not even in the face of Armageddon. Never compromise." (Walter Kovacs/Rorschach)

    Things to consider before posting.
    Don't forget to rate the posts if they helped and mark thread as resolved when they are.


    .Net Regex Syntax (Scripting) | .Net Regex Language Element | .Net Regex Class | DateTime format | Framework 4.0: what's new
    My fresh new blog : writingthecode, even if I don't post much.

    System: Intel i7 920, Kingston SSDNow V100 64gig, HDD WD Caviar Black 1TB, External WD "My Book" 500GB, XFX Radeon 4890 XT 1GB, 12 GBs Tri-Channel RAM, 1x27" and 1x23" LCDs, Windows 10 x64, ]VS2015, Framework 3.5 and 4.0

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Conversion from string "" to type 'Integer' is not valid.

    I would suggest a full and clear explanation, including exactly what you're trying to achieve and why, what and how you put into the ListBox, etc.

  4. #4
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Conversion from string "" to type 'Integer' is not valid.

    What type is "Index"? If it is integer (or any numeric type), you don't need the Val() around it.
    What type is ArrayCBSA?
    What type of data does your UnsortedListBox items hold?
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Oct 2009
    Posts
    253

    Re: Conversion from string "" to type 'Integer' is not valid.

    Code:
    Dim finish As Integer = TextBox1.Text
            Dim Total As Integer = TextBox1.Text
            Dim CC As Integer
            Dim array(finish) As Integer
            Dim TT As Integer
            Dim WT As Integer
            Dim TempAveTT As Double
            Dim TempAveWT As Double
            Dim AveTT As Double
            Dim AveWT As Double
    
     If finish = 0 Then
    
            Else
    
                For start As Integer = 1 To finish
                    CC = InputBox("Please enter CPU Cycles: ")
                    UnsortedListBox.Items.Add(CC)
                Next
    
    
            End If
    
            Dim Index As Integer = 0
            Dim ArrayCBSA(0 To UnsortedListBox.Items.Count - 1) As Integer
    
    
            Do While Index <= UnsortedListBox.Items.Count - 1
                ArrayCBSA(Index) = UnsortedListBox.Items(Index)
                Index = Index + 1
            Loop
    
            Index = 0
            ClassicBubbleSortAscending(ArrayCBSA)
    
            Do While Index <= UnsortedListBox.Items.Count - 1
                TT = ArrayCBSA(Index) + TT
                TempAveTT = TempAveTT + TT
                TTListBox.Items.Add(TT)
    
                WT = TT - ArrayCBSA(Index)
                TempAveWT = TempAveWT + WT
                WTListBox.Items.Add(WT)
    
                CCListBox.Items.Add(ArrayCBSA(Index))
                Index = Index + 1
            Loop
    
            AveTT = TempAveTT / Total
            AveWT = TempAveWT / Total
    
            AveTTListBox.Text = Format(AveTT, "Standard")
            AveWTListBox.Text = Format(AveWT, "Standard")
    
            TextBox1.Clear()
        End Sub
    This is the code, but the problem is if I press cancel on the inputbox that will pop if I press the button It gives the error (Conversion from string "" to type 'Integer' is not valid.) what should I do?

  6. #6
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Re: Conversion from string "" to type 'Integer' is not valid.

    The problem in your code is that you are trying to convert an empty string into an integer, inside the Do .. While loop. This empty string is a result of you pressing Cancel in the inputbox.

    There are two ways to work around this. Immediately after the inputbox display, place a check to see if the value of CC is "", which means the user either clicked Cancel or clicked OK without entering anything in the inputbox. If the CC is "", make it "0". This way if you cancel out of the inputbox, the list will now show a zero for that value. The remaining code should now work fine.

    The alternate approach is to modify the code inside the Do..While. Place a check here to see if the value you are reading from the listbox is "". If it is, skip it. If not, then add it to the array.

    Here's how the modified code would look like:

    Code:
    'Approach 1
                For start As Integer = 1 To finish
                    CC = InputBox("Please enter CPU Cycles: ")
                    If CC="" Then CC="0"
                    UnsortedListBox.Items.Add(CC)
                Next
    Code:
    'Approach 2
            Do While Index <= UnsortedListBox.Items.Count - 1
                If UnsortedListBox.Items(Index) <> "" Then
                    ArrayCBSA(Index) = UnsortedListBox.Items(Index)
                End If
                Index = Index + 1
            Loop
    Any one of these approaches should fix your code for now.

    However, on a general note, it is a bad idea to assume the user will always enter what you expect, specially when you have no validations to check if the user is only entering numbers and nothing else. So a word of caution: Always ensure the user input is validated so you can avoid such problems in future.

    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  7. #7
    Addicted Member vb_ftw's Avatar
    Join Date
    Dec 2010
    Posts
    139

    Re: Conversion from string "" to type 'Integer' is not valid.

    well, i made this while honeybee posted...i might have missed a few things but i sure it wont hurt to just post it:


    if u want to continue using the same code as is it i would suggest u enter a numeric value in TextBox1 before pressing the button or validate the TextBox1 and any other input fields to check if the user has typed anything or an integer value before moving forward.

    Conversion from string "" to type 'Integer' is not valid.

    this error is on these lines:

    Dim finish As Integer = TextBox1.Text
    Dim Total As Integer = TextBox1.Text
    CC = InputBox("Please enter CPU Cycles: ")

    if the user enters a string such as "abc" or nothing at all "" in textbox1 or inputbox the conversion from string "abc" or "" to the declared data type of each of those variables which is integer fails.
    if an integer value is entered the code works fine.

    bl: u need validation
    hope this helps

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Oct 2009
    Posts
    253

    Re: Conversion from string "" to type 'Integer' is not valid.

    honeybee, very well said. thanks! IT WORKS! This forum is really awesome!
    caution: Always ensure the user input is validated so you can avoid such problems in future.
    Thanks for this tip, I will remember that.


    vb ftw, thank you! I understand it now. Your post helps me.

  9. #9
    New Member
    Join Date
    Feb 2012
    Posts
    1

    Re: [RESOLVED] Conversion from string "" to type 'Integer' is not valid.

    Private Sub CarEditor_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim vehicle As Car = New Car
    Dim StreamCars As FileStream
    Dim FormatterCars As BinaryFormatter = New BinaryFormatter

    REM This is the file that holds the list of items
    Dim Filename As String = "C:\WOOSH Car Rental 1.1\Cars.crs"
    lblPictureName.Text = "."

    If File.Exists(Filename) Then
    StreamCars = New FileStream(Filename,
    FileMode.Open,
    FileAccess.Read,
    FileShare.Read)
    Try
    REM Retrieve the list of cars
    ListOfCars = CType(FormatterCars.Deserialize(StreamCars),
    Dictionary(Of String, Car))

    Finally
    StreamCars.Close()
    End Try
    Else
    ListOfCars = New Dictionary(Of String, Car)
    End If
    End Sub





    im having the same problem with the code where i highlighted in blue.
    can anyone help me?thx so much.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width