Domanda

Does vb.net have a built-in function to check if a multidimensional array of unknown shape from a user passed as a Function parameter is jagged? If so, what is it?

È stato utile?

Soluzione

There are two issues here. Multidimensional arrays (defined like Integer(,)) are never "jagged". You can detect this by checking the array's Rank. The GetLowerBound and GetUpperBound methods can be used to check the length of the individual dimensions as needed.

If you are looking to see if a Jagged array (ie: Integer()()) is fully initialized, and each sub array is the same length (ie: determine whether a jagged array is "logically" rectangular), you could use something like:

Dim initializedToSameLengths As Boolean
If theArray(0) Is Nothing Then
   ' If first element is null, check for all others being null
   initializedToSameLengths = theArray.All(Sub(a) a = Nothing)
Else
   Dim firstLength = theArray(0).Length
   initializedToSameLengths = theArray.All(Sub(a) a <> Nothing AndAlso a.Length = firstLength)
End If

Altri suggerimenti

If the array is multidimensional, it will have more than 1 dimension (so the Array.Rank property will be >1). If the array is jagged, it will be a one-dimensional array with arrays as elements (so its Array.Rank property will be =1).

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top