Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
953 views
in Technique[技术] by (71.8m points)

vb.net - When do I need to use a `New` keyword when creating an array?

I was just looking at the help page for arrays in VB and apparently all of these are acceptable syntax for creating arrays:

Dim numbers(4) As Integer 

inquiriesByYearMonthDay = New Byte(20)()() {}

Dim sales()() As Double = New Double(11)() {}

Note that the first one doesn't even have a New keyword, the second one only has a New keyword, and the third one uses both the Dim and New keywords. Why is there so much inconsistency and which one are you supposed to use? I'm most curious about the first one since the array object is never even being created

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Actually, only the first array is created and ready for use. The others are merely declared as arrays (or partially created).

Dim numbers(4) As Integer
Dim sales()() As Double = New Double(11)() {}

As Intellisense shows, numbers is ready to use. For the jagged array sales - an array of arrays- the code has so far just told it how many arrays it will hold. Each of those is Nothing because the code has not created or assigned one to any of the slots:

enter image description here

The line declaring sales is actually doing two things which can be confusing. It could have been declared like this:

' at some point I will want an array of array of doubles named sales:
Dim sales()() As Double

' Ok, I need it to hold 12 double arrays:
sales =  New Double(11)() {}

Step one declares the variable and Type. Step 2 defines the "container array" by creating a temp array and assigning it. (The link tries to illustrate this with inquiriesByYearMonthDay - it is declared in the preceding block.)

Sales still isnt ready to use, it just a bunch of Nothing.

' Ok, I need the first array to hold 5 elements
sales(0) = New Double(4) {}       ' 0 based, so 4 == 5 items
sales(0)(0) = 12.67               ' FINALLY we can use it

Now, sales(0)(0 thru 4) can store data. sales(1)() and the rest have not been initialized.

The New isnt really acting on sales directly. It is creating a new, temp array (on the right) which is immediately assigned to a slot in sales() on the left.

The key is: arrays are ready to use when given a size. So, these are functionally identical:

' create int array with 5 slots:
Dim numbers(4) As Integer
' declare array; assign empty 4 slot int array to it:
Dim numbers() As Int32 = New Int32(4) {}

The first declaration includes the size; the second assigns a (new) temp array of a specified size to it. That said, VB can infer the size:

Dim fishes As String() = {"cod", "salmon", "tuna", "perch", "barracuda"}

The {...} is another temp array, which VB assigns to 'fishes'.


But arrays are just clunky. What will sales(6,3) represent? To make them readable you need constants everywhere: foo = sales(SALES_MARCH, SALES_ZIGGY). Personally, I think the only good array is a static array with known, fixed values:

Dim Days As String() = {"sun", "mon", "tues"....}

Otherwise, List(Of T), Dictionary(TK, TV), Collection(Of T) and many many others are easier to use, create, manage and work with (they size themselves!)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...