Collection in VB.Net

A

Anson

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
 
L

Larry Serflaten

Anson said:
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
 
A

Anson

Thank you Larry.


Larry Serflaten said:
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
 

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