Loop through each element in an array VB/C#
Post your questions regarding programming in C# in here.
4 posts
Page 1 of 1
So I have this function that has one parameter of type Array. Not Int32[] or String[] but Array. This Array might just as well be multidimensional, jagged, or just single dimensional. How would I loop through each element in the array? No matter if the code is in C# or VB, I just need to know how I can get each element in the array.
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;
Over 30 projects with source code!
Please give reputation to helpful members!
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;
Over 30 projects with source code!
Please give reputation to helpful members!
I don't know if there is a way to use a For Each loop with multidimensional array, but you can use loops within loops:
Code: Select all
Dim arr(10, 10) As String
For a = 0 To arr.GetLength(0) - 1
For b = 0 To arr.GetLength(1) - 1
Next
Next
I couldn't use your way, because then I have to hardcode exactly as many loops as there are dimensions. The problem is, the amount of dimensions wont be the same each time.
I think I might use this though:
I think I might use this though:
Code: Select all
public void SaveArray(Stream s, Array Value)
{
foreach (object element in Value)
{
Type t = element.GetType();
if (t.IsArray || element is Array)
{
SaveArray(s, (Array)element);
continue;
}
SaveObject(s, element, t);
}
}
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;
Over 30 projects with source code!
Please give reputation to helpful members!
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;
Over 30 projects with source code!
Please give reputation to helpful members!
Hmmmmm.
I'll just give you my first initial thoughts
I'll just give you my first initial thoughts
Code: Select all
foreach (object c in xArray)
{
if (c is Array)
{
do whatever you want.
}
}
Bound and boom tech,
The Future Of Coding
The Future Of Coding
4 posts
Page 1 of 1
Copyright Information
Copyright © Codenstuff.com 2020 - 2023