Initializing System.Array?

Why is the Visual Basic compiler complaining?

Dim finalArray As Array = New Array
+3
source share
6 answers

An array is an abstract class ( MustInherit in terms of VB). You cannot create an abstract class.

+4
source

EDIT: (following a comment by Joe Zhong)

msdn:

MustInherit , New MustInherit. MustInherit, , MustInherit.

+2

, . , . :

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

, . :

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

, , , .

+2

does it do for me which version of Visual Studio are you using?

 Error  1   'New' cannot be used on a class that is declared 'MustInherit'. C:\Documents and Settings\---\My Documents\Visual Studio 2008\Projects\---\Default.vb   171 39  ---
+1
source

Why not

Dim finalArray as New ArrayList()

Indeed, if you only save a specific type of object, you should use generics.

Dim finalArray as New List(Of Integer)
Dim finalArray as New List(Of String)
Dim finalArray as New List(Of YourFavoriteObject)

(And don't be a sloppy VB6 programmer ... add those perens for constructors and calls to other methods.)

+1
source

You must specify the type and size of the array:
Example of creating an array String, size 5:

Dim finalArray As Array = Array.CreateInstance(GetType(String), 5)
0
source

Source: https://habr.com/ru/post/1715707/


All Articles