PC Review


Reply
Thread Tools Rate Thread

Collection in VB.Net

 
 
Anson
Guest
Posts: n/a
 
      25th Oct 2004
Dear All,

I'm a VB6 programmer migrating to VB.Net .

I have a question regarding the usage of Collection in VB.Net. The following
is the code i would use in VB6.

'************* VB6 Code ***************
Dim tempStr(2) as string
Dim tempCollection as new collection
for i = 1 to 10
tempstr(0) = cstr(i)
tempstr(1) = "XXX"
tempstr(2) = "YYY"

tempcollection.add tempstr
next i
'*************************************
This code works without any problem. However, if i use the same code in
VB.Net. The elements in the resultant collection are all the same (10, XXX,
YYY) , all equal to the value of the last element.

Do i somehow have to "new" the array of string? Does .Net take the string
array as an object? If so, how do i new a string?

Thank you in advance for your response.

Best Regards,

Anson


 
Reply With Quote
 
 
 
 
Larry Serflaten
Guest
Posts: n/a
 
      25th Oct 2004

"Anson" <p_o_o_n_a-n-s-o-n@t_NO_SPAM_orontosquare.com> wrote
>
> I have a question regarding the usage of Collection in VB.Net. The following
> is the code i would use in VB6.
>
> '************* VB6 Code ***************
> Dim tempStr(2) as string
> Dim tempCollection as new collection
> for i = 1 to 10
> tempstr(0) = cstr(i)
> tempstr(1) = "XXX"
> tempstr(2) = "YYY"
>
> tempcollection.add tempstr
> next i
> '*************************************
> This code works without any problem. However, if i use the same code in
> VB.Net. The elements in the resultant collection are all the same (10, XXX,
> YYY) , all equal to the value of the last element.
>
> Do i somehow have to "new" the array of string? Does .Net take the string
> array as an object? If so, how do i new a string?


Yes, you need to release the old array and create a new array for the next item
in the collection, something like;

For i = 1 to 10
Dim temp(2) As String
temp(0) = ...
...
tempCollection.Add temp
Next

LFS
 
Reply With Quote
 
Anson
Guest
Posts: n/a
 
      25th Oct 2004
Thank you Larry.


"Larry Serflaten" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
>
> "Anson" <p_o_o_n_a-n-s-o-n@t_NO_SPAM_orontosquare.com> wrote
> >
> > I have a question regarding the usage of Collection in VB.Net. The

following
> > is the code i would use in VB6.
> >
> > '************* VB6 Code ***************
> > Dim tempStr(2) as string
> > Dim tempCollection as new collection
> > for i = 1 to 10
> > tempstr(0) = cstr(i)
> > tempstr(1) = "XXX"
> > tempstr(2) = "YYY"
> >
> > tempcollection.add tempstr
> > next i
> > '*************************************
> > This code works without any problem. However, if i use the same code in
> > VB.Net. The elements in the resultant collection are all the same (10,

XXX,
> > YYY) , all equal to the value of the last element.
> >
> > Do i somehow have to "new" the array of string? Does .Net take the

string
> > array as an object? If so, how do i new a string?

>
> Yes, you need to release the old array and create a new array for the next

item
> in the collection, something like;
>
> For i = 1 to 10
> Dim temp(2) As String
> temp(0) = ...
> ...
> tempCollection.Add temp
> Next
>
> LFS



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Collection problems (create Collection object, add data to collection, bind collection to datagrid) Řyvind Isaksen Microsoft ASP .NET 1 18th May 2007 10:24 AM
Collection problems (create Collection object, add data to collection, bind collection to datagrid) Řyvind Isaksen Microsoft Dot NET 1 18th May 2007 10:24 AM
Can't get collection to save when using collection of custom class as property of control in VS 2005 J.Edwards Microsoft Dot NET Compact Framework 0 10th Jan 2006 04:44 AM
key/value collection that allows key string to be updated and retains collection item entry order dx Microsoft Dot NET Framework 2 25th Sep 2004 05:51 PM
Item Collection Editor doesn't preserve collection Andrés Giraldo Microsoft ASP .NET 2 25th Mar 2004 08:38 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:51 PM.