Loop through each element in an array VB/C#

Post your questions regarding programming in C# in here.
4 posts Page 1 of 1
Contributors
User avatar
MrAksel
C# Coder
C# Coder
Posts: 1758
Joined: Fri Mar 26, 2010 12:27 pm

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!

Image
Image
User avatar
Cheatmasterbw
Coding God
Coding God
Posts: 1506
Joined: Fri Jan 01, 2010 2:30 pm

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
http://www.megaapps.tk/
User avatar
MrAksel
C# Coder
C# Coder
Posts: 1758
Joined: Fri Mar 26, 2010 12:27 pm

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:
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!

Image
Image
User avatar
DreadNought
VIP - Donator
VIP - Donator
Posts: 116
Joined: Fri Jan 08, 2010 12:37 pm

Hmmmmm.

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
4 posts Page 1 of 1
Return to “General coding help”