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

Looping for string array

P: n/a
Hi,

I have a small function that uses a For...Next loop. I am trying to place
the results of the loop in a string array. My code is as follows:

Dim i as Integer
Dim str() as String
For i = 0 to products.Items(0).Item.Length - 1
str() = New String() {products.Items(0).Item(i).Name}
Next

What I'm trying to do with the loop is this:

Dim str() as String = New String() {products.Items(0).Item(0).Name,
products.Items(0).Item(1).Name, etc.}
For some reason I cannot get the For...Next loop to work correctly. What am
I doing wrong? Any help will be appreciated.

Thanks,
Roshawn
Nov 21 '05 #1
Share this Question
Share on Google+
17 Replies


P: n/a
On Tue, 24 Aug 2004 22:38:41 -0500, Roshawn wrote:
Hi,

I have a small function that uses a For...Next loop. I am trying to place
the results of the loop in a string array. My code is as follows:

Dim i as Integer
Dim str() as String
For i = 0 to products.Items(0).Item.Length - 1
str() = New String() {products.Items(0).Item(i).Name}
Next

What I'm trying to do with the loop is this:

Dim str() as String = New String() {products.Items(0).Item(0).Name,
products.Items(0).Item(1).Name, etc.}
For some reason I cannot get the For...Next loop to work correctly. What am
I doing wrong? Any help will be appreciated.

Thanks,
Roshawn


Your array is a null reference (it doesn't point to anything). You would
need to declare your array with bounds or use ReDim/ReDim Preserve to
enlarge the array dynamically. Though, I believe you might find
System.Collections.Specialized.StringCollection to be simpler...

Imports System.Collections.Specialized

....
Dim strings As New StringCollection()

For i As Integer = 0 To products.Items(0).Item.Length - 1
strings.Add(products.Items(0).Item(0).Name)
Next i
--
Tom Shelton [MVP]
Nov 21 '05 #2

P: n/a
Tom,.

How did you find this one, I never saw it?
Can be usefull in my opinion instead of the arraylist for this because
probably you do not have to unbox.

Cor
Nov 21 '05 #3

P: n/a
Roshawn,

As an alternative for Tom's nice answer because you know in advance the
length of your array.

Dim i as Integer
Dim str(products.Items(0).Item.Length) as String
For i = 0 to products.Items(0).Item.Length - 1
str() = New String() {products.Items(0).Item(i).Name}
Next

Cor

Nov 21 '05 #4

P: n/a
Cor,
Strings are a reference type, there is NO boxing going on!

Hope this helps
Jay

"Cor Ligthert" <no**********@planet.nl> wrote in message
news:e1**************@TK2MSFTNGP11.phx.gbl...
Tom,.

How did you find this one, I never saw it?
Can be usefull in my opinion instead of the arraylist for this because
probably you do not have to unbox.

Cor

Nov 21 '05 #5

P: n/a
Roshawn,
It appears that you are attempting to create a single element array for each
string in the list.

As the others suggested you need to create the array first, then you need to
set each element.

Something like:

Dim i As Integer
Dim str(products.Items(0).Item.Length - 1) As String
For i = 0 To products.Items(0).Item.Length - 1
str(i) = products.Items(0).Item(i).Name
Next

Notice that I create the correctly sized array first (the array is defined
with upper bound, not size). Then we set the respective element of the array
to the respective element of your list.

Hope this helps
Jay
"Roshawn" <ud****@bellsouth.net> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Hi,

I have a small function that uses a For...Next loop. I am trying to place
the results of the loop in a string array. My code is as follows:

Dim i as Integer
Dim str() as String
For i = 0 to products.Items(0).Item.Length - 1
str() = New String() {products.Items(0).Item(i).Name}
Next

What I'm trying to do with the loop is this:

Dim str() as String = New String() {products.Items(0).Item(0).Name,
products.Items(0).Item(1).Name, etc.}
For some reason I cannot get the For...Next loop to work correctly. What am I doing wrong? Any help will be appreciated.

Thanks,
Roshawn

Nov 21 '05 #6

P: n/a
Jay,
Can be usefull in my opinion instead of the arraylist for this because
probably you do not have to unbox.

Strings are a reference type, there is NO boxing going on!


How do I than get the string out of the arraylist in the last routine
without using "ToString"

\\\
Dim mss() As String = {"Jay", "Herfried", "Cor"}
Dim mss2(mss.Length - 1) As String
Dim arl As New ArrayList
For Each msrow As String In mss
arl.Add(msrow)
Next
For i As Integer = 0 To arl.Count - 1
mss2(i) = arl(i).ToString
Next
///

Cor
Nov 21 '05 #7

P: n/a
Roshaw

I was looking what I did wrong, I see it now, I changed one thing to less.
Dim i as Integer
Dim str(products.Items(0).Item.Length) as String
For i = 0 to products.Items(0).Item.Length - 1 str = products.Items(0).Item(i).Name Next


Cor
Nov 21 '05 #8

P: n/a
JD
This is not boxing. Boxing is taking a value type and wrapping into an
object reference type. Since a string is already a reference there is no
boxing. Matter of fact look at the IL created by your example and there is
not one box operation.

In your example the string has been downcasted to an object type, and all
you need to do is CType it back to a string. Tostring mthod works also
because its a virtual method in the object base class that is overriden by
the string class for its specific implementation. In your example when you
call object.tostring it gets diverted to the string.tostring.

JD
"Cor Ligthert" <no**********@planet.nl> wrote in message
news:e5*************@TK2MSFTNGP12.phx.gbl...
Jay,
Can be usefull in my opinion instead of the arraylist for this because
probably you do not have to unbox.

Strings are a reference type, there is NO boxing going on!


How do I than get the string out of the arraylist in the last routine
without using "ToString"

\\\
Dim mss() As String = {"Jay", "Herfried", "Cor"}
Dim mss2(mss.Length - 1) As String
Dim arl As New ArrayList
For Each msrow As String In mss
arl.Add(msrow)
Next
For i As Integer = 0 To arl.Count - 1
mss2(i) = arl(i).ToString
Next
///

Cor

Nov 21 '05 #9

P: n/a
JD
> str = products.Items(0).Item(i).Name

Aren't you assigning a string to a string array here?
"Cor Ligthert" <no**********@planet.nl> wrote in message
news:uR**************@TK2MSFTNGP09.phx.gbl...
Roshaw

I was looking what I did wrong, I see it now, I changed one thing to less.
Dim i as Integer
Dim str(products.Items(0).Item.Length) as String
For i = 0 to products.Items(0).Item.Length - 1

str = products.Items(0).Item(i).Name
Next


Cor

Nov 21 '05 #10

P: n/a
Hi JD,
This is not boxing. Boxing is taking a value type and wrapping into an
object reference type.


Than I have seen this wrong, what you name this?

dim myObject as new Object
myObject = "MyString"

Because that is what happens in my opinion here in my sample with the
arraylist.

Cor

Nov 21 '05 #11

P: n/a
Yes,

str(i) = products.Items(0).Item(i).Name

However the essential part was the construction of the String array, and
with this was the law of Murphy active I think.

:-)

Cor
Nov 21 '05 #12

P: n/a
JD
Hi Cor
dim myObject as new Object
myObject = "MyString"
This is an implicit conversion from a string to an object.

The IL for the above statement is:

IL_0020: ldstr "MyString"
IL_0025: stloc.1 //location 1 being -> [1] object
myObject. So it stores the string directly to the object.

Now take this code:
Dim arl As ArrayList = New ArrayList
arl.Add(42)

The IL for this code is:
IL_0008: ldc.i4.s 42
IL_000a: box [mscorlib]System.Int32
IL_000f: callvirt instance int32
[mscorlib]System.Collections.ArrayList::Add(object)

JD
"Cor Ligthert" <no**********@planet.nl> wrote in message
news:Or*************@TK2MSFTNGP11.phx.gbl... Hi JD,
This is not boxing. Boxing is taking a value type and wrapping into an
object reference type.


Than I have seen this wrong, what you name this?

dim myObject as new Object
myObject = "MyString"

Because that is what happens in my opinion here in my sample with the
arraylist.

Cor

Nov 21 '05 #13

P: n/a
JD,

Thanks for explaining this for me.

I used boxing for everything that is wraped implictly or explititly in an
object.

Next time I will better use my words in this.

Thank you for taking the time for me.

Cor
Nov 21 '05 #14

P: n/a
JD
Its my pleasure Cor.

You do fine job helping people in these newsgroups, and I can't seem to find
the time to help enough.

JD
"Cor Ligthert" <no**********@planet.nl> wrote in message
news:er**************@TK2MSFTNGP10.phx.gbl...
JD,

Thanks for explaining this for me.

I used boxing for everything that is wraped implictly or explititly in an
object.

Next time I will better use my words in this.

Thank you for taking the time for me.

Cor

Nov 21 '05 #15

P: n/a
Hi everyone,

Let me say thanks to all who responded to my question. Never have I
submitted a question that generated so many answers. It's very nice to know
that there are individuals out there in the .NET universe willing to help us
newbies.

Just to let you know, things worked perfectly!!!

Thanks again,
Roshawn
Nov 21 '05 #16

P: n/a
Cor,
You really should know the answer to this one! :-|

You can use DirectCast which does a Cast from the object reference type to
the String reference.
mss2(i) = DirectCast(arl(i), String)
Because String is a reference type there is no unboxing going on.

However if your ArrayList has Integers in it, because Integer is a value
type, then DirectCast will unbox when it did the Cast.

Hope this helps
Jay

"Cor Ligthert" <no**********@planet.nl> wrote in message
news:e5*************@TK2MSFTNGP12.phx.gbl... Jay,
Can be usefull in my opinion instead of the arraylist for this because
probably you do not have to unbox.

Strings are a reference type, there is NO boxing going on!


How do I than get the string out of the arraylist in the last routine
without using "ToString"

\\\
Dim mss() As String = {"Jay", "Herfried", "Cor"}
Dim mss2(mss.Length - 1) As String
Dim arl As New ArrayList
For Each msrow As String In mss
arl.Add(msrow)
Next
For i As Integer = 0 To arl.Count - 1
mss2(i) = arl(i).ToString
Next
///

Cor

Nov 21 '05 #17

P: n/a
Jay,

It became clear to me, I use boxing for every whatever that is put in an
object.

JD and now you made it clear to me I was using the wrong word in the wrong
place.

You know that I (with you) look more at the correct readability than to any
small result in time and am therefore mostly not interested if it is boxed
or/and unboxed.

However I should not use here Boxing because that can be confusing for
others and I will take some time how the object is build in the ILS.

Thank you as well for explaining it to me.

Cor
Cor,
You really should know the answer to this one! :-|

You can use DirectCast which does a Cast from the object reference type to
the String reference.
mss2(i) = DirectCast(arl(i), String)


Because String is a reference type there is no unboxing going on.

However if your ArrayList has Integers in it, because Integer is a value
type, then DirectCast will unbox when it did the Cast.

Hope this helps
Jay

Nov 21 '05 #18

This discussion thread is closed

Replies have been disabled for this discussion.