Referencing arrays

adriaan

Member
Joined
Feb 28, 2005
Messages
13
Programming Experience
5-10
Referencing arrays [RESOLVED]

Hi,

I want to reference an array with one of my own control's DataSource property.

When I add an element to my array, I resize it with 'Redim Preserved' and add the new element to it. But when you redim my control looses the reference to the array and does not show the new added element in my control.

I have tried numerous methods of resizing the array with arraylist, creating new
arrays and using Array.Copy but all are losing my reference.

Is there a better way in dealing with this?

Thanks
 
Last edited:
When using the Redim statement all the data contained in the array is lost. If you want to preserve existing data when reinitializing an array then you should use the Preserve keyword which looks like this:
VB.NET:
Dim[/color][/size][size=2] Test() [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]Integer[/color][/size][size=2] = {1, 2, 3} [/size][size=2][color=#008000]'declares an array an initializes it with three members

[/color][/size][size=2][/size][size=2][color=#0000ff]ReDim[/color][/size][size=2] [/size][size=2][color=#0000ff]Preserve[/color][/size][size=2] Test(25) [/size][size=2][color=#008000]'resizes the array and retains the the data in elements 0 to 2

[/color][/size][size=2]Test(3) = 3

Test(4) = 4

MessageBox.Show(Test(3) & ", " & Test(4))

Cheers ;)

Note: I know that you have used Preserve keyword but anyway could you post some code as we can help you out
 
What I am on about is the following:

VB.NET:
[left]Dim Test() As Integer = {1, 2, 3} Dim RefA() As Integer

RefA = Test

ReDim Preserve Test(25)

MessageBox.Show(RefA.Length.ToString) 'returns 3

MessageBox.Show(Test.Length.ToString) 'returns 26

[/left]

 
I know it is the way it works; But my initial question is what would be best to keep that reference, so that RefA would always point to the resized array without going to update the reference after the array has been resized.

Thanks,
 
When you use ReDim it actually assigns a new Array object to the variable. Even if you use "Preserve", the elements are the same but the actual Array object is still different. I'd suggest you use an ArrayList as your DataSource in the first place. You should generally avoid using ReDim if at all possible. If you are having to resize your array more than once then you should be using an ArrayList anyway. If you really must use an Array, you would need to reassign the DataSource after the ReDim or actually ReDim the DataSource itself.

If this is a little confusing, you need to remember that an Array is a class (read: reference type) in .NET. This means that your variable is actually a reference to an object somewhere else in memory. If you create an array and then assign that array to a DataSource property, you have two variables that refer to the one object in memory. When you call ReDim on your array, a new Array object is created in memory and your variable now refers to it, but your DataSource property still refers to the original Array object. You now have two variables that refer to two different objects. Any changes made to one will have no effect on the other.
 
Back
Top