Home > Back-end >  Object Array, Array class and need for a full array declaration
Object Array, Array class and need for a full array declaration

Time:07-12

Pls note that it may seem that I am asking many questions but they are all related. Its just that I am unable to frame a crisp question due to lack of understanding of the underlying concepts and therefore tried to give scenarios to highlight my confusion.

Dim myArray As Array
myArray = New Integer() {1, 2, 3, 4}

Versus

Dim mynewArray() As Integer = New Integer() {4, 5, 6, 7}

Can someone help me understand the following points w.r.t the above:

  1. My understanding is that ‘myArray’ is not an array. Rather it is a field of the array class that can point to any array that we create? Is this the right understanding?
  2. If that is so then why to write something like Dim myArray() As Integer = New Integer() {4, 5, 6, 7}. Is there any advantage to declare it such a manner? This point becomes more relevant because all the following declarations work:

• Dim mynewArray() = New Integer() {4, 5, 6, 7}

• Dim mynewArray = New Integer() {4, 5, 6, 7}

• So even when we omit the “As Integer” part and even the parenthesis ‘()‘ the declarations seem to work. So can someone tell mewhat is happening here and what is the advantage of the full definition: Dim mynewArray() As Integer = New Integer() {4, 5, 6, 7}

  1. What exactly is the role of “mynewArray”? I believe it is the field that is used to store the reference to the array being created? Is my understanding correct?

Also, a related question is what would be the difference between ‘myArray’ which is of Array type and an object array? My confusion stems from the following:

  1. myArray can point to any array (irrespective of the type of elements it holds)
  2. an object array, like ‘myObjArray’ as in Dim myObjArray() As Object = {26, 27, 28, 29, 30} can accept elements of any type

So are these (i.e. myArray and myObjArray) not essentially doing the same thing, that is giving us full flexibility with respect to the type of elements that the array they are pointing to can hold??

CodePudding user response:

In a comment, the question notes some additional context, which is that this is prompted by an article in the VB documentation on iteration. Specifically, the article uses as an example iteration over the values in an enumeration. System.Enum.GetValues dates from .NET 1.0 (before generics) and returns an array which is typed at runtime as the enum type whose values are returned. Because a) it's before generics and b) the type is provided at runtime rather than compile time, System.Enum.GetValues returns an Array.

This is the specific use case in which declaring something as Array makes some sense. You wouldn't be likely to see something like that in more modern code---if I were writing an equivalent of System.Enum.GetValues today, I would write something like this:

Public Function GetValues(Of TEnum As Structure)() As IEnumerable(Of TEnum)

Or this:

Public Function GetValues(Of TEnum As Structure)() As TEnum()

But neither option was available in .NET 1.0 because generics didn't exist yet.

Arrays work a little like generics, but as I noted before, in this case, you can only get the contained type at runtime.

Note that System.Array supports IEnumerable, so you can use Linq functions if you first use Cast(Of T) to convert to a sequence of a specified type.

CodePudding user response:

It's very rare that you should ever need to use the Array type for a variable. Just don't do it. If you need an array then use a specific type. For a field, declare the type explicitly, e.g.

Private myArray1 As Integer()
Private myArray2 As Integer() = {1, 2, 3}

For a local, let the type be inferred in you're initialising it, e.g.

Dim myArray1 As Integer()
Dim myArray2 = {1, 2, 3}

The only reason you should specify the type when initialising a local is if you want a type other than that of the elements, e.g.

Dim myArray As Object() = {1, 2, 3}

That's really all you need to know. Anything else is complicating things unnecessarily.

Note that, in each of these case, myArray and the like are variables. They behave just like any other reference type variables, i.e. they are Nothing by default and they can refer to an object of the appropriate type. Arrays are class instances so array variables behave like variables of any other class type. Again, don't try to complicate things. If you know how variables of type Form, DataTable, String, etc, work then you know how array variables work.

  • Related