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 17 2393
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]
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
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
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
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
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
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
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
> 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
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
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
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
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
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
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
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
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
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: ensnare |
last post by:
Hi all,
I'm using a database session handler and am looking to loop through
data residing in the sessions table to make a 'Users online' array.
I've found that using urldecode on the data...
|
by: paul |
last post by:
Hi all,
I have a 2D array, which I am trying to access, but I am having
problems doing this:
If, for instance, I use a nested for loop to go through the values,
using something like
echo...
|
by: David |
last post by:
Hello.
I am looking for advice on what is "best practice" regarding looping
through a form to check its checkboxes and associated data fields.
Here is what I am trying to do (Here is the page...
|
by: Stephen |
last post by:
I have an array list alSearchCriteria which contains values which vary each
time its created. It will always have 15 items in the arraylist each time its
created. Some of the values in the array...
|
by: Nicole |
last post by:
Hello! I hope there is someone out there who can shed some light on
this for me. I have a module that is supposed to look at an access
table, pull out each bid record, link to another table to...
|
by: Stephen |
last post by:
I have an array list alSearchCriteria which contains values which vary each
time its created. It will always have 15 items in the arraylist each time its
created. Some of the values in the array...
|
by: motion musso aka: sathia |
last post by:
this is it, how can i get the current index? couldn't figure it out.
thank you
for(i=0;myarray.length<i;i++){
do_something();
//i need the current myarray
}
bye bye
|
by: akselo |
last post by:
Hi folks,
I am working on a routine that will select a sample of parcels from a table. Each parcel belongs to a census tract, and depending on which tract, a certain calculation is applied. The...
|
by: Sweetiecakes |
last post by:
Hello
Is there an example of looping through a dataset filling an array with it?
Thank you
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |