הערה
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות להיכנס או לשנות מדריכי כתובות.
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות לשנות מדריכי כתובות.
Question
Sunday, August 25, 2013 9:27 AM
Hello;
I'm used to write :
redim preserve all_clr(intcounter1)
But when I tried to use with two dimensional array it didn't work.
SO how I redim a two or three dimensional array?
All replies (4)
Sunday, August 25, 2013 10:02 AM ✅Answered
Hi,
If you wanna re-dim a two dimensional array, you have to do this:
Module Module1
Sub Output(numbers(,) As Integer)
For y As Integer = 0 To numbers.GetLength(0) - 1
For x As Integer = 0 To numbers.GetLength(1) - 1
Console.Write(numbers(y, x) & " ")
Next
Console.WriteLine()
Next
End Sub
Sub Main()
Dim numbers(1, 1) As Integer
Dim n As Integer = 1
For i As Integer = 0 To 1
For j As Integer = 0 To 1
numbers(i, j) = n
Next
n += 1
Next
'Output
Output(numbers)
'Changed
ReDim Preserve numbers(1, 2)
'Assign values
numbers(0, 2) = 100
numbers(1, 2) = 200
Console.WriteLine("Changed Later=======")
'Output
Output(numbers)
End Sub
End Module
But plz notice that if you shorten the two-dimision array, you should ONLY change the MOST-RIGHT dimision number:
Module Module1
Sub Output(numbers(,) As Integer)
For y As Integer = 0 To numbers.GetLength(0) - 1
For x As Integer = 0 To numbers.GetLength(1) - 1
Console.Write(numbers(y, x) & " ")
Next
Console.WriteLine()
Next
End Sub
Sub Main()
Dim numbers(2, 2) As Integer
For i As Integer = 0 To 2
For j As Integer = 0 To 2
numbers(i, j) = i * j
Next
Next
'Output
Output(numbers)
'Redim Preserve numbers(1,1)——This is wrong! You cannot change the first dimision
ReDim Preserve numbers(2, 1)
Console.WriteLine("Changed Later=======")
'Output
Output(numbers)
End Sub
End Module
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats
Found any spamming-senders? Please report at: Spam Report
Sunday, August 25, 2013 9:46 AM
Hi
From Help.
Resizing with Preserve. If you use Preserve, you can resize only the last dimension of the array, and for every other dimension you must specify the same bound it already has in the existing array.
For example, if your array has only one dimension, you can resize that dimension and still preserve all the contents of the array, because you are changing the last and only dimension. However, if your array has two or more dimensions, you can change the size of only the last dimension if you use Preserve.
Regards Les, Livingston, Scotland
Sunday, August 25, 2013 2:26 PM
C# and VB have parity.
It means that almost all things can be done in both program languages.
C# has the possibility of unsafe code as legacy from C++ which is not in VB; VB has the Redim from older VB versions which is not in C#.
Nobody who knows a little bit more about those regrets it that it is has no parity.
Try to avoid the Redim and go for a class approach as is thousand times written in this forum.
Success
Cor
Thursday, August 29, 2013 9:57 PM
Consider:
Public Class Form1
Dim ar1 As New List(Of Integer)
Dim ar2 As New List(Of Integer)
Dim ar3 As New List(Of String)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
ar3.Add("string 1")
ar1.Add(ar3.Count - 1)
ar3.Add("string 2")
ar2.Add(ar3.Count - 1)
ar3.Add("string 3")
ar1.Add(ar3.Count - 1)
ar3.Add("string 4")
ar2.Add(ar3.Count - 1)
For i = 0 To ar1.Count - 1
Debug.Print("{0} | {1}", ar3(ar1(i)), ar3(ar2(i)))
Next
Stop
End Sub
End Class
Which can be modified according to what you know of the depth of each dimension.