By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,168 Members | 2,548 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,168 IT Pros & Developers. It's quick & easy.

String variable into integer array

P: n/a
Hi!

I'm wondering how can I get integer type of array easier of the numbers
in string type variable when numbers are separated with comma ",".

I made it like code below, but obviously there is an easier way to do it.

'// Readed string from ConfigurationSettings
Dim strNums As String = "10,20,30,40"

Dim arrStr As String() = strNums.Split(Char.Parse(","))
Dim arrInt(arrStr.Length - 1) As Integer

For i As Integer = 0 To arrStr.Length - 1
arrInt(i) = CType(arrStr(i), Integer)
Next

....code continues

--
Thanks in advance!

Mika
Nov 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
There are slight variations on what you have done, but I dont think there is
a more streamline way code wise to convert arrays of those types.

"Mika M" <ma***************@luukku.com> wrote in message
news:eb**************@TK2MSFTNGP14.phx.gbl...
Hi!

I'm wondering how can I get integer type of array easier of the numbers in
string type variable when numbers are separated with comma ",".

I made it like code below, but obviously there is an easier way to do it.

'// Readed string from ConfigurationSettings
Dim strNums As String = "10,20,30,40"

Dim arrStr As String() = strNums.Split(Char.Parse(","))
Dim arrInt(arrStr.Length - 1) As Integer

For i As Integer = 0 To arrStr.Length - 1
arrInt(i) = CType(arrStr(i), Integer)
Next

...code continues

--
Thanks in advance!

Mika

Nov 21 '05 #2

P: n/a
Hi,

I dont see a better way. Just thought you should know that you can
replace Char.Parse(",") with ","c

Ken
-------------
"Mika M" <ma***************@luukku.com> wrote in message
news:eb**************@TK2MSFTNGP14.phx.gbl...
Hi!

I'm wondering how can I get integer type of array easier of the numbers in
string type variable when numbers are separated with comma ",".

I made it like code below, but obviously there is an easier way to do it.

'// Readed string from ConfigurationSettings
Dim strNums As String = "10,20,30,40"

Dim arrStr As String() = strNums.Split(Char.Parse(","))
Dim arrInt(arrStr.Length - 1) As Integer

For i As Integer = 0 To arrStr.Length - 1
arrInt(i) = CType(arrStr(i), Integer)
Next

...code continues

--
Thanks in advance!

Mika

Nov 21 '05 #3

P: n/a
"Mika M" <ma***************@luukku.com> schrieb:
I'm wondering how can I get integer type of array easier of the numbers in
string type variable when numbers are separated with comma ",".

I made it like code below, but obviously there is an easier way to do it.

'// Readed string from ConfigurationSettings
Dim strNums As String = "10,20,30,40"

Dim arrStr As String() = strNums.Split(Char.Parse(","))
Dim arrInt(arrStr.Length - 1) As Integer

For i As Integer = 0 To arrStr.Length - 1
arrInt(i) = CType(arrStr(i), Integer)
Next


I think the solution is basically OK. However, some suggestions:

* I would use '","c' instead of 'Char.Parse(",")'. Appending the 'c' to
the
one-character string literal will make it a character literal. No need
for
a conversion at runtime.

* Instead of 'CType(arrStr(i), Integer)' you could write 'CInt(arrStr(i))'
which makes code shorter and IMO more readable.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.