Re: Sting to Int from Array
Dim myNumber As Integer = CInt(sport(1))
Re: Sting to Int from Array
Code:
Dim myNumber As Integer = Convert.ToInt32(sport(1))
Re: Sting to Int from Array
With both methods i get the following error
"Conversion from string "" to type 'Integer' is not valid."
Re: Sting to Int from Array
Perhaps you should use a list(of integer) instead. It'll help avoid issues with blank items.
Re: Sting to Int from Array
You will have to make sure that string actually is an integer. It cant be blank, you can use a different object to store the data to resolve this or,
2 Solutions,
1) when loading the array if the string is blank load a 0
2) Do somthing like this, (My VB is rusty)
Code:
If(sport(1).Trim() != "") then
myNumber = Convert.ToInt32(sport(1))
else
myNumber = 0
End If
Re: Sting to Int from Array
oh does that mean my array has a blank item in it?
Re: Sting to Int from Array
Yes, a string of "" is equivilant to String,Empty.
Re: Sting to Int from Array
Use:
Code:
Dim myNumber As Integer
Integer.TryParse(sport(1), myNumber)
Re: Sting to Int from Array
Code:
If(sport(i).Trim() != "") then
number = Convert.ToInt32(sport(1))
Else
number = 0
End If
Is there any reason why the "=" is showing an error? It says identifier expected. Thanks for your help chaps.
VB Express 2008
Re: Sting to Int from Array
!= should be <>
but use Integer.TryParse
Re: Sting to Int from Array
Quote:
Originally Posted by
ForumAccount
!= should be <>
but use Integer.TryParse
Yep, correct told you my VB was rusty :-S
Re: Sting to Int from Array
Re: Sting to Int from Array
Integer.TryParse is the solution you should go for only if you can not be certain that a certain string contains a valid integer representation, possible scenarios where that applies is if you let the user insert a number by using a textbox.
If you however have the possibility to make sure that the string will always represent an integer, do not use Integer.TryParse but rather Cint(), Integer.Parse or the likes.
Re: Sting to Int from Array
Quote:
Originally Posted by Atheist
Integer.TryParse is the solution you should go for only if you can not be certain that a certain string contains a valid integer representation, possible scenarios where that applies is if you let the user insert a number by using a textbox.
If you however have the possibility to make sure that the string will always represent an integer, do not use Integer.TryParse but rather Cint(), Integer.Parse or the likes.
Not sure if that was to me, but while what you said is valid I suggested Integer.TryParse because he posted this:
Quote:
Originally Posted by albo
With both methods i get the following error
"Conversion from string "" to type 'Integer' is not valid."
So I didn't think he would have consistent Integers passing through.
Re: Sting to Int from Array
It wasnt aimed at anyone in particular but I felt like it was a relevant addition to the topic ;)
Indeed it seems like there are empty strings in the array, so what albo should do at first is to find out why this occurs, since it was mentioned in the first post that the second index should contain a number.
Re: Sting to Int from Array
Another method is using a converter as shown below
Code:
Sub StringToInt()
Dim MyStringArray() As String = _
{"100", "", "200", "23", Nothing}
Dim MyIntegerArray() As Integer = _
Array.ConvertAll(MyStringArray, _
New Converter(Of String, Integer)(AddressOf _ToInteger))
For Each Item In MyIntegerArray
Console.WriteLine(Item)
Next
End Sub
Private Function _ToInteger(ByVal value As String) As Integer
Dim Result As Integer = 0
If Not Integer.TryParse(value, Result) Then
Result = 0
End If
Return Result
End Function