-
Jan 28th, 2011, 07:33 AM
#1
Thread Starter
Addicted Member
[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
-
Jan 28th, 2011, 07:52 AM
#2
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
-
Jan 28th, 2011, 07:55 AM
#3
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.
-
Jan 28th, 2011, 07:57 AM
#4
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?
-
Jan 28th, 2011, 10:38 PM
#5
Thread Starter
Addicted Member
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?
-
Jan 28th, 2011, 11:09 PM
#6
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.
.
-
Jan 28th, 2011, 11:21 PM
#7
Addicted Member
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
-
Jan 29th, 2011, 02:50 AM
#8
Thread Starter
Addicted Member
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.
-
Feb 29th, 2012, 12:52 AM
#9
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|