Redim Preserve in C#?
Use ArrayLists or Generics instead
How to convert ReDim?
The closest thing available in C# is Array.Resize
, which is standard .NET and thus available in VB too. You might like to try using it in the VB code to see whether you get the same result first, then the translation to C# will be easier.
In your code, these lines:
ReDim Preserve bytIV(sTemp.Length - 1)
ReDim Preserve bytIV(maxSize / 8 - 1)
would become this:
Array.Resize(bytIV, sTemp.Length)
Array.Resize(bytIV, maxSize / 8)
Note that Array.Resize
takes a new length rather than a new upper bound. Converting those lines to C# would consist of appending a semicolon.
I should also point out that, with Option Strict On
(as it should pretty much always be), that VB code would have to use integer division:
ReDim Preserve bytIV(maxSize \ 8 - 1)
Array.Resize(bytIV, maxSize \ 8)
In C#, integer division is automatic when dividing one int
by another, so the operator looks the same as regular division.
EDIT: prompted by Mary's comment above, I realised that converting the Array.Resize
calls from VB to C# would require the addition of the ref
keyword too, as the first parameter is passed by reference.
Array.Resize(ref bytIV, sTemp.Length);
Array.Resize(ref bytIV, maxSize / 8);
Converting VB code to C# using array and redim preserver keyword
Right; I think the real issue here is that you have a 2-dimensional array; RefStrLinks
is not a string[]
, but rather is a string[,]
, with dimension 2 on the first axis. Array.Resize
only works with vectors (a "vector" is a one-dimensional array with base index 0, i.e. string[]
).
Frankly, I would replace all of this (re-dimming an array or using Array.Resize
per element is absurdly expensive) with something like:
List<SomeBasicType> list = ...
...
// where "foo" and "bar" are the two values that you intend to store per item
var item = new SomeBasicType(foo, bar);
list.Add(item);
where perhaps SomeBasicType
is an immutable struct that takes two strings. Or more simply, in C# "current": value-type tuples:
// declare the list (change the names to something meaningful for your code)
var list = new List<(string name, string url)>();
// ... add values efficiently
string name = "whatever"; // your per-item code goes here
string url = "some value"; // and here
list.Add((name, url));
// ... show that we have the data
foreach(var item in list)
{
Console.WriteLine($"{item.name} / {item.url}");
}
Resize array in C# later in the program
You cannot resize an array; you must declare a new array of the desired size and copy the contents of the original array into the new array.
Update: I don't like Array.Resize
- it doesn't resize the array (as the method name would suggest), it creates a new array and replaces the reference:
static void Main(string[] args)
{
int[] a1 = new int[] { 1, 2, 3 };
int[] a2 = a1;
Console.WriteLine("A1 Length: {0}", a1.Length); // will be 3
Console.WriteLine("A2 Length: {0}", a2.Length); // will be 3
Array.Resize(ref a1, 10);
Console.WriteLine("A1 Length: {0}", a1.Length); // will be 10
Console.WriteLine("A2 Length: {0}", a2.Length); // will be 3 - not good
Console.ReadLine();
}
What is the equivalent of ReDimStatement in c#?
ReDim Item.Text(0)
in VB is the same as
Item.Text = new String[1];
in C#. But you might want to take a step back here and ask what the program is actually doing, and then write a more idiomatic C# program. In particular, should Text be a List<string>
instead of an array?
How to use Redim Preserve in vba
Using ReDim Preserve
- You need to declare the variable using parentheses to be able to 'apply' the first
ReDim Preserve
on it: Dim V() As Variant
- Note that this can be written to be more efficient i.e. by creating a reference to the row range and using its number of columns to
ReDim
the array. In the end, only one or none ReDim Preserve
is needed.
Option Explicit
Sub WriteToZeroBasedArray()
Dim V() As Variant ' Note the parentheses!
Dim Index As Long
Dim cCount As Long: cCount = 10 ' e.g.
Dim c As Long
For c = 7 To cCount
If 0 < Cells(9, c).Value And Cells(9, c).Value < 100 Then
ReDim Preserve V(Index)
' A safer way (Option Base related):
'ReDim Preserve V(0 To Index)
V(Index) = Cells(9, c).Value
Index = Index + 1
End If
Next c
If Index = 0 Then Exit Sub
Debug.Print Join(V, vbLf)
End Sub
Sub WriteToOneBasedArray()
Dim V() As Variant ' Note the parentheses!
Dim Index As Long
Dim cCount As Long: cCount = 10 ' e.g.
Dim c As Long
For c = 7 To cCount
If 0 < Cells(9, c).Value And Cells(9, c).Value < 100 Then
Index = Index + 1
ReDim Preserve V(1 To Index)
V(Index) = Cells(9, c).Value
End If
Next c
If Index = 0 Then Exit Sub
Debug.Print Join(V, vbLf)
End Sub
Related Topics
Use of "This" Keyword in Formal Parameters for Static Methods in C#
Winforms: Application.Exit VS Environment.Exit VS Form.Close
Compare Two Lists for Differences
Owin Security - How to Implement Oauth2 Refresh Tokens
Is There Any JSON Web Token (Jwt) Example in C#
Advantages to Using Private Static Methods
Token Based Authentication in ASP.NET Core (Refreshed)
Get List of Zero Reference Codes in Visual Studio
Practical Applications of Bitwise Operations
Binding Objects Defined in Code-Behind
Unity Create UI Control from Script
ReDim Preserve
on it: Dim V() As Variant
ReDim
the array. In the end, only one or none ReDim Preserve
is needed.Option Explicit
Sub WriteToZeroBasedArray()
Dim V() As Variant ' Note the parentheses!
Dim Index As Long
Dim cCount As Long: cCount = 10 ' e.g.
Dim c As Long
For c = 7 To cCount
If 0 < Cells(9, c).Value And Cells(9, c).Value < 100 Then
ReDim Preserve V(Index)
' A safer way (Option Base related):
'ReDim Preserve V(0 To Index)
V(Index) = Cells(9, c).Value
Index = Index + 1
End If
Next c
If Index = 0 Then Exit Sub
Debug.Print Join(V, vbLf)
End Sub
Sub WriteToOneBasedArray()
Dim V() As Variant ' Note the parentheses!
Dim Index As Long
Dim cCount As Long: cCount = 10 ' e.g.
Dim c As Long
For c = 7 To cCount
If 0 < Cells(9, c).Value And Cells(9, c).Value < 100 Then
Index = Index + 1
ReDim Preserve V(1 To Index)
V(Index) = Cells(9, c).Value
End If
Next c
If Index = 0 Then Exit Sub
Debug.Print Join(V, vbLf)
End Sub
Use of "This" Keyword in Formal Parameters for Static Methods in C#
Winforms: Application.Exit VS Environment.Exit VS Form.Close
Compare Two Lists for Differences
Owin Security - How to Implement Oauth2 Refresh Tokens
Is There Any JSON Web Token (Jwt) Example in C#
Advantages to Using Private Static Methods
Token Based Authentication in ASP.NET Core (Refreshed)
Get List of Zero Reference Codes in Visual Studio
Practical Applications of Bitwise Operations
Binding Objects Defined in Code-Behind
Unity Create UI Control from Script