Resizing a two dimensional Array - vb.net

How can I resize the two dimensional array size without affecting its value?

Use ReDim with the Preserve modifier. VB.NET will make sure the original values are untouched. Didn't read the documentation right. ReDim Preserve will only allow you to change the length of the last dimension of the array.
You need to allocate a new array (with the correct size) and manually copy the elements from the first array to the second.

As Adam said, you can't resize 2D arrays dynamically. You can easily copy the existing array into a bigger one like so:
Dim smaller(1, 1) As Byte
Dim bigger(2, 2) As Byte
Array.Copy(smaller, bigger, smaller.length)

Try using array.resize if you are on .net 2 framework or above.
For example:
Dim MyArray() as string
Array.Resize(myarray,12)

Related

Does REDIM PRESERVE for more elements set all new elements to NOTHING?

I have an array with data.
I do REDIM PRESERVE and specify a larger boundlist.
All old elements are preserved.
Will all new elements be NOTHING?
REDIM PRESERVE my_array( my_array.LENGTH + 10 )
Will the new 10 elements all be NOTHING?
Yes, additional elements are Nothing. Be aware, though, ReDim Preserve does not just extend an array in-place. It allocates a completely new array and copies the existing elements, and then leaves the original for the garbage collector.
Almost all cases where you find yourself using ReDim Preserve should be changed to use a generic List(Of T) instead.
It depends on what type your array is. Strings, integers and other 'known' numeric types get initialised with value of 0 ("" for strings).

For loop only way to copy array content to another array?

I have some code that has heavy use of arrays. It all works but there are many for loops where I iterate and copy contents from several arrays to a new array with more dimensions to fit all content.
As I understand it, there is no way to do this without a for loop (to copy array content to another array). Is this correct?
There is the method Array.Copy(arSource,arTarget,length). But it does not work with more dimensions arrays. The new one is that jagged arrays()arrays of arrays are more perfomant than 2D ARRAYS,if you move along the vectors.

How can I transfer the values of a 4D jagged array to a 2D standard array?

I have EuroCPCrap mapped as (j,0)(i,0) and want to put this into an array EuroCPConsol mapped as (j,i). I tried:
For j = 0 to CPIndex 'CPIndex is a global count of variables in matrix j references
For i = 0 to UBound (EuroCPCrap,3) 'i in the (now known to be) jagged
EuroCPConsol(j+1,i+1)= EuroCPCrap(j,0,i,0) 'add one since I'm base 1 but function that produced this matrix outputted base zero
Next i
Next j
I get a subscript error on the UBound statement, and I realised it's because there is no 3rd dimension in the referenced array.
First of all, I don't understand why this EuroCPCrap(j,0,i,0) would work if it's a jagged array as you describe in your first sentence. On the looks of it, it should be EuroCPCrap(j,0)(i,0).
You have a parent two-dimensional array of children two-dimensional arrays. The "third" dimension you're looking for is actually the first dimension of each child array. So something like this should work:
For i = 0 to UBound(EuroCPCrap(j,0),1)
Actually, iterating from LBound to UBound is even better practice to ensure that the entire array is traversed regardless of your Option Base or how the array is "Dimmed":
For i = LBound(EuroCPCrap(j,0),1) to UBound(EuroCPCrap(j,0),1)
Does EuroCPCrap really need to be jagged? Why not make it a 4-dimensional array? EuroCPConsol is not jagged... Is it dimensioned correctly to accept the contents of the largest of the children array? These are things to think about...

Visual Basic 6 Dynamic Variables

I am doing a program using vb6 which is stored datas from mouseclick in term of coordinates. I succeeded doing the first stage which is displaying the clicked coordinates. My problem now is I need to save the coordinate in term of variables so i can call them back to use for another purpose for example to find distance between two points.
if its just two coordinates its easier to find the distance. but when it comes to many coordinates I am stuck. I tried to do an array to stored the data inside loop
1. InputX(ListNum, 0) = Int(x)
2. InputY(ListNum, 1) = Int(y)
3. ListNum=ListNum+1
when I try to call for InputX(2,0) = Text1.Text or Text1.Text=InputX(2,0) none of them working. It seems that the data will be erased after I do a mouseclick
Is there any way which I can set the dynamic variables which stored each my clicked coordinates such as Input1,Input2,Input3 ...InputN
I do this in VB6.
The problem you're having is that you're using a two dimensional array there. A two dimensional array looks like a table. That's not what you want though. You want a list of pairs of points. So, create a structure with two integers in it, x and y, and make an array of those structures:
'Right underneath your Class Form1 declaration:
Structure Point
Dim x As Integer
Dim y As Integer
End Structure
Dim length As Integer = 10
Dim Points(length) As Point
'When you want to start using your points put this in the method:
Points(0).x = 10
Points(0).y = 10
Points(1).x = 20
Points(1).y = 40
Dynamic variables in VB6
First you declare the variable without giving size:
Dim InputX() As String
Then you give for the first time size to your array using ReDim:
ReDim InputX(5)
If you want to preserve whatever data is already in your array you use ReDim Preserve:
ReDim Preserve InputX(10)
I hope this is what you need.
it appears that the first method
Text1.Text=InputX(2,0)
is working. I just need to declare x and y As Single

Total Nos. of items in array variable

I have an array variable (string type). It contains certain no. of items, that I donot know how many they are.
I need to run a loop for that many nos. that the array contains. I tried LBound and UBound loop but it says my array is not a system array.
How can I know how many items my array contains?
Thanks
Furqan
You can use the Length property of the array object.
From MSDN (Array.Length Property):
Gets a 32-bit integer that represents the total number of elements in all the dimensions of the Array.
Read about arrays in VB.NET and the Array class for better understanding of arrays in VB.NET and the .NET framework.
Update:
However, for looping over an array you should simply use a For Each loop (as an array is treated like any other collection in .NET) - this way you will not make any silly mistakes with array bounds and off by ones:
For Each item As arrayItemType in MyArray
' do stuff with item
Next
See the example on this page.
You look at the length:
To get the number of items in the first dimension: arrayName.GetLength(0)
If you need the index, use GetUpperBound(0)
Some helpful examples here.
Like Oded said, you can use the Length-propery of the Array. This would look something like this:
Dim data As String() = {"one", "two", "three", "four"}
For i = 0 To data.Length - 1
Console.WriteLine(data(i))
Next
If you just want to loop all strings in your array, you can use For Each as well:
For Each s As String In data
Console.WriteLine(s)
Next
If the compiler is telling you that your variable is not a system array, then chances are, it's not an array. If it's not an array, you won't be able to get its bounds through any means.
Inspect the variable in the locals window and verify that your variable is of the type that you think it is. It's probably not an array after all.