Odd construct in VB.NET that Im trying to understand

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have found that in order to initiate an array of class objects I am having
to use a construct that I do not understand. The Class is defined as follows:

Public Class Prospects
Public Prospect_ID As String
Public Company_Name As String
Public Cust_Acct_Nbr As String
Public Salesperson_ID As String
Public Close_Probability As String
Public Est_Total_Rev As String
End Class 'Prospects

To make an array of such objects I am doing the following:

Dim ProspectList As Prospects() = New Prospects(0) {}

Then to populate the array I'm doing the following sort of thing:


i = 0
If myReader.Read Then
Do
If i > 0 Then
ReDim Preserve ProspectList(i)
End If
ProspectList(i) = New Prospects
If Not IsDBNull(myReader("Prospect_ID")) Then
ProspectList(i).Prospect_ID =
CStr(myReader("Prospect_ID"))
Else
ProspectList(i).Prospect_ID = "None"
End If
......... etc., et.

Then I make my return as follows:
Return ProspectList

This works. The array of prospect class objects gets returned to the web
service client.

Here is my question. What the heck do the curly brackets in the

Dim ProspectList As Prospects() = New Prospects(0) {}
statement do?

I assume they are a paramaterless constructor but to what do they serve as a
constructor? And why curly brackets? () will not work it has to be {}

Like I say, I've got it working but it drives me crazy when I don't
understand why something like this works.

Any insights would be appreciated.
 
From books online


An array-element initializer consists of a sequence of variable
initializers, enclosed by curly brace tokens and separated by comma tokens.
Each variable initializer is an expression or, in the case of a
multidimensional array, a nested array-element initializer. Array-element
initializers may also be used in array-creation expressions.

The type of expression or statement in which an array-element initializer is
used determines the type of the array being initialized. In an
array-creation expression, the array type immediately precedes the
initializer. In a variable declaration, the array type is the type of the
variable being declared. When an array-element initializer is used in a
variable declaration, such as:

Private a As Integer() = { 0, 2, 4, 6, 8 }it is simply shorthand for an
equivalent array-creation expression:

Private a As Integer() = new Integer() { 0, 2, 4, 6, 8 }In an array-element
initializer, the outermost nesting level corresponds to the leftmost
dimension, and the innermost nesting level corresponds to the rightmost
dimension. The initializer must have the same number levels of nesting as
there are dimensions in the array. All of the elements in the innermost
nesting level must be implicitly convertible to the element type of the
array. The number of elements in each nested array-element initializer must
always be consistent with the size of the other array-element initializers
at the same level.

The following example creates a two-dimensional array with a length of five
for the leftmost dimension and a length of two for the rightmost dimension:

Private b As Integer(,) = { { 0, 1 }, { 2, 3 }, { 4, 5 }, { 6, 7 }, { 8,
9 } }The example is equivalent to the following:

Private b(4, 1) As IntegerThe example then initializes the array instance
with the following values:

b(0, 0) = 0 : b(0, 1) = 1
b(1, 0) = 2 : b(1, 1) = 3
b(2, 0) = 4 : b(2, 1) = 5
b(3, 0) = 6 : b(3, 1) = 7
b(4, 0) = 8 : b(4, 1) = 9If the array-creation expression specifies the
bounds of the dimensions, the number of elements at any particular level
must be the same as the size of the corresponding dimension. If the bounds
are unspecified, the length of each dimension is the number of elements in
the corresponding level of nesting.

Some valid and invalid examples follow:

Private x() As Integer = New Integer(2) {0, 1, 2} ' OK.
Private y() As Integer = New Integer(2) {0, 1, 2, 3} ' Error,
length/initializer mismatch.Here, the initializer for y is in error because
the length and the number of elements in the initializer do not agree.

An empty array-element initializer (that is, one that contains curly braces
but no initializer list) is always valid regardless of the number of
dimensions of the array. If the size of the dimensions of the array being
initialized is known in an array-creation expression, the empty
array-element initializer represents an array instance of the specified size
where all the elements have been initialized to the element type's default
value. If the dimensions of the array being initialized are not known, the
empty array-element initializer represents an array instance in which all
dimensions are size zero.

At run time, the expressions in an array-element initializer are evaluated
in textual order from left to right.

ArrayElementInitializer ::= { [ VariableInitializerList ] }

VariableInitializerList ::=
VariableInitializer |
VariableInitializerList , VariableInitializer

VariableInitializer ::= Expression | ArrayElementInitializer
 
Steve said:
Dim ProspectList As Prospects() = New Prospects(0) {}

The other poster explained what they are. In your case, you don't to
use that syntax though. Since you use redim later, all you really need
is this:

Dim ProspectList As Prospects()
 
On Wed, 2 Nov 2005 06:48:25 -0800, "Steve Wells" <Steve
I have found that in order to initiate an array of class objects I am having
to use a construct that I do not understand. The Class is defined as follows:


<snip>


I see the other posters answered your question, but I just wanted to
ask if you really needed to use an array? Can you not use a
Collection? The Collection gives you some other functionality, like
more dynamic adding & removal, indexing by order or by a string key,
etc.
 
Yeah, I know for some things collections would be better. However, this is
part of a webservice that has to be interoperable with J2EE consumers. So,
I'm constrained to using constructs that will produce strongly typed arrays.
Don't know if collections would have that result or not and I know that what
I'm doing here will result in a wsdl that is consumable by either java or
..net clients.

Otherwise, I'd just use datasets but they produce diffgrams for which there
is no java analog.
 
I agree that that SHOULD work but it doesn't. I tried that. The compiler
barfs all over it in vb.net 2003.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top