Here's the situation. I have a multidimensional array of different types:
theArray(Integer,String)
(That's clearly not correct syntax. I just want to get the idea across right now.)
The first problem is to find a way to specify a multidimensional array with two different types.
Just as a comparison, this wouldn't even be a question if a collection was being used. For example, this syntax works (but is not necessarily recommended):
Dim theCollection As New Collection
Dim anElement
anElement = "A String"
theCollection.Add(anElement)
anElement = 12345
theCollection.Add(anElement)
Implicit typing of anElement allows it to take on both integer and string types as the code runs.
Creating a structure or a class will also work:
Structure theStruct
Public aString As String
Public anInteger As Integer
End Structure
. . .
Dim theCollection As New Collection
Dim anElement As New theStruct
anElement.aString = "A String"
anElement.anInteger = 12345
theCollection.Add(anElement)
But, in this case, I decided I wanted to use an array. But the use of two different types in the same multidimensional array is a problem. You can't declare the type of the array the normal way:
Dim theArray(,) as <datatype>
Only one datatype is allowed by the syntax.
Fortunately, the same implicit typing introduced by VB.NET 2008/Framework 3.5 makes it possible:
Dim theArray(,) =
{
{12345, "A String"},
{67890, "Another String"}
}
VB.NET figures out that the first element is integer and the second is string by looking at the data assigned. You can even use random types:
Dim theArray(,) =
{
{12345, "A String"},
{67890, "Another String"},
{11111, 22222},
{"More Strings", "Still More"}
}
(I may be wrong, but I think a multidimensional array of different types might not have been possible before implicit typing without using type Object for everything. Let me know if you have information that shows otherwise.)
Now, just to be more contrary, I also want to use a For-Each loop to iterate through the array and only use the first (integer) values. Ordinarily, a For-Each loop would look like this:
For Each anElement In theArray
... <processing code>
Next
A For-Each loop on this array, however, first processes the inner dimension and then the outer one. That means anElement is first assigned an integer, then a string, then the second integer, then the second string, and so forth. My application needed to pass the value of anElement to a subroutine:
Sub theSub(ByVal value As Integer)
... <processing code>
End Sub
If every element is passed, then the code crashes because a string is passed instead of an integer. In my app, it isn't an option to make them all type Object. So ... what to do?
One solution is to simply create a binary flag and use that to avoid passing the strings.
Dim evenOrOdd As Boolean = True
For Each anElement In theArray
If evenOrOdd Then
theSub(anElement)
evenOrOdd = False
Else
evenOrOdd = True
End If
Next
This works. If you step through the code and check the type of anElement, it even changes on each iteration. But it seems slightly hokey to me. So, why not use the type of anElement to make sure that only integers are passed:
For Each anElement In theArray
Dim thetype
thetype = GetType(Integer)
If Type.GetType(anElement ) =
GetType(Integer) Then
theSub(anElement)
End If
Next
This solution still doesn't seem anywhere close to "elegant" to me. If you know a better way, let us know about it. I'll give you credit in this space for the answer.
