|
-
Apr 7th, 2009, 10:03 AM
#1
Thread Starter
Junior Member
Sting to Int from Array
Hi i'm stuck on something so easy, i've googled, tired various methods but cant get it to work. Imagine if you can, i have an array (set as a string type) but the second slot holds a numebr that i want, e.g. 2333.
So to grab it i use the following code, however as my sport array is set to a string it wont allow it. Like i said i have tried a few ways but nothing works. Such a stupid and simple situation. I'm much much better with actionscript surely its just a string to int conversion?
Code:
Dim myNumber As Integer = sport(1)
Last edited by albo; Apr 7th, 2009 at 10:51 AM.
-
Apr 7th, 2009, 10:13 AM
#2
Re: Sting to Int from Array
Dim myNumber As Integer = CInt(sport(1))
-
Apr 7th, 2009, 10:16 AM
#3
Member
Re: Sting to Int from Array
Code:
Dim myNumber As Integer = Convert.ToInt32(sport(1))
In case I forget: I'm using Visual Basic 2008 Express Edition...
Should I, in my odd, bumbling way, actually offer some assistance, feel free to show me some RATE love.
Just Another Laptop Hero
-
Apr 7th, 2009, 10:23 AM
#4
Thread Starter
Junior Member
Re: Sting to Int from Array
With both methods i get the following error
"Conversion from string "" to type 'Integer' is not valid."
-
Apr 7th, 2009, 10:26 AM
#5
Re: Sting to Int from Array
Perhaps you should use a list(of integer) instead. It'll help avoid issues with blank items.
-
Apr 7th, 2009, 10:26 AM
#6
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
-
Apr 7th, 2009, 10:27 AM
#7
Thread Starter
Junior Member
Re: Sting to Int from Array
oh does that mean my array has a blank item in it?
-
Apr 7th, 2009, 10:29 AM
#8
Re: Sting to Int from Array
Yes, a string of "" is equivilant to String,Empty.
-
Apr 7th, 2009, 10:30 AM
#9
Re: Sting to Int from Array
Use:
Code:
Dim myNumber As Integer
Integer.TryParse(sport(1), myNumber)
-
Apr 7th, 2009, 10:32 AM
#10
Thread Starter
Junior Member
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
-
Apr 7th, 2009, 10:34 AM
#11
Re: Sting to Int from Array
!= should be <>
but use Integer.TryParse
-
Apr 7th, 2009, 10:37 AM
#12
Re: Sting to Int from Array
 Originally Posted by ForumAccount
!= should be <>
but use Integer.TryParse
Yep, correct told you my VB was rusty :-S
-
Apr 7th, 2009, 10:53 AM
#13
Thread Starter
Junior Member
Re: Sting to Int from Array
Hehe all is good now
-
Apr 7th, 2009, 11:33 AM
#14
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.
-
Apr 7th, 2009, 11:42 AM
#15
Re: Sting to Int from Array
 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:
 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.
-
Apr 7th, 2009, 11:45 AM
#16
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.
-
Apr 7th, 2009, 01:55 PM
#17
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
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
|