Looping for string array

R

Roshawn

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
 
T

Tom Shelton

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
 
C

Cor Ligthert

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
 
C

Cor Ligthert

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
 
J

Jay B. Harlow [MVP - Outlook]

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

Hope this helps
Jay
 
J

Jay B. Harlow [MVP - Outlook]

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
 
C

Cor Ligthert

Jay,
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
 
C

Cor Ligthert

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

Cor
 
J

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
 
C

Cor Ligthert

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
 
C

Cor Ligthert

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
 
J

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
 
C

Cor Ligthert

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
 
J

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
 
R

Roshawn

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
 
J

Jay B. Harlow [MVP - Outlook]

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
 
C

Cor Ligthert

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top