{} again

H

Hei

Hi All

in the book of example :

Public MustInherit Class Broker

Private const m_MAXRESERVATION As Integer =10
Protected m_reservations() as Reservation

Public Sub New()
m_reservations = New Reservation(m_MAXRESERVATION ) {}

End Sub

......

what mean of {} in above case?

thx
Hei
 
K

Klaus H. Probst

The code is creating an array. Look in the VB language reference, near the
end of the Dim topic there's an explanation for that.
 
A

Armin Zingler

Hei said:
Hi All

in the book of example :

Public MustInherit Class Broker

Private const m_MAXRESERVATION As Integer =10
Protected m_reservations() as Reservation

Public Sub New()
m_reservations = New Reservation(m_MAXRESERVATION ) {}

End Sub

.....

what mean of {} in above case?

As it's already been answered in the other thread: It specifies the content
of the array. If there is nothing between the curly braces, the array has 0
items.
 
H

Herfried K. Wagner [MVP]

* "Hei said:
in the book of example :

Public MustInherit Class Broker

Private const m_MAXRESERVATION As Integer =10
Protected m_reservations() as Reservation

Public Sub New()
m_reservations = New Reservation(m_MAXRESERVATION ) {}

End Sub

.....

what mean of {} in above case?

A new array if 'Reservation' with 'm_MAXRESERVATION' + 1 elements is
created.
 
A

Armin Zingler

Herfried K. Wagner said:
A new array if 'Reservation' with 'm_MAXRESERVATION' + 1 elements
is created.


I didn't know this syntax! I thought you can _not_ create an array using
"New" unless you fill it by the values in curly braces. After reading your
post, I read the question again and thought the code can not work at all.
The syntax above is very confusing: Does it assign an array containing zero
items because there are no items between the braces, or does it create an
array containing m_MAXRESERVATION items? Contradictive IMO.
 
H

Herfried K. Wagner [MVP]

* "Armin Zingler said:
I didn't know this syntax! I thought you can _not_ create an array using
"New" unless you fill it by the values in curly braces. After reading your
post, I read the question again and thought the code can not work at all.
The syntax above is very confusing: Does it assign an array containing zero
items because there are no items between the braces, or does it create an
array containing m_MAXRESERVATION items? Contradictive IMO.

It will work. I have tested it with this code:

\\\
Dim x() As Form = New Form(10) {}
///

The code will create an array of 'Form' with 11 elements pointing to
'Nothing'.
 
A

Armin Zingler

Herfried K. Wagner said:
It will work. I have tested it with this code:

\\\
Dim x() As Form = New Form(10) {}
///

The code will create an array of 'Form' with 11 elements pointing
to 'Nothing'.



Well, I did understand it and I know what it does, but I tried to say that
the syntax is ambiguous. I could also expect it must create an _empty_ array
because there are no items between the braces.
 
C

Cor

Hi Armin and Herfried,

I think we should not support this kind of code in the newsgroup.

It give no information to the ones who need it, brings someone only on the
wrong route., when he/she would read this.

Just an opinion.


Cor
 
H

Herfried K. Wagner [MVP]

* "Cor said:
I think we should not support this kind of code in the newsgroup.


It give no information to the ones who need it, brings someone only on the
wrong route., when he/she would read this.

IMO there is no ambiguity...
 
J

Jay B. Harlow [MVP - Outlook]

Armin,
I agree its confusing. However

The 10 is giving the number of elements (off by one rule) instead of the 10
being a parameter to the constructor.

Hope this helps
Jay
 
J

Jay B. Harlow [MVP - Outlook]

Cor,
It is valid VB.NET! Confusing yes, but valid.

Why would we not recommend using it & supporting it?

How would it be any different if I insisted you cannot under any
circumstance use any of the functions in Microsoft.VisualBasic namespace.
Period! Although I largely prefer to use the 'native .NET methods', I will
not say you cannot use Microsoft.VisualBasic.Strings.Mid over
System.String.SubString, instead I will caution people that the arguments to
Mid are base one, where as the arguments to SubString are base zero.

I feel its better to cover "all" the options so others have a choice on
which syntax they want to use...

Just a thought
Jay
 
A

Armin Zingler

Jay B. Harlow said:
Armin,
I agree its confusing. However

The 10 is giving the number of elements (off by one rule) instead of
the 10 being a parameter to the constructor.

Right, that's why I thought the syntax it is _not_ possible.


Dim f As Form()

f = new form(10) 'error
f = new form(10) {} 'no error

As the second creates a Form array and assigns 0 items, I expect the first
one to create a Form array without assigning an item. So, both are equal.

In other words, I do know that the first creates a new Form and passes 10 to
the constructor, but as it is true, the second should not work because the
curly braces don't make sense when creating a single instance.
 
C

Cor

Hi Jay,

If I am wrong tell it me, from the example from Herfried I understand that
this is the same

Dim myform() As form = New form(10) {}
Dim myform(10) As form

For what you wrote, I tell it always exact in the way you did write it.
Using the Visualbasic functions, describes the things often better than the
..Net methods.
(I try to avoid them but that is my own decission).

If my example above is right, than is this a way of creating a bad readable
program.

Some people like that, but I think those don't have to use VB but C# and
than a lot of documentation with it.

Just a thought

Cor
 
J

Jay B. Harlow [MVP - Outlook]

Armin,
the constructor, but as it is true, the second should not work because the
curly braces don't make sense when creating a single instance.
Think of the second as saying "New Array of Form with bounds of 10", only
backwards & mixed up. ;-)
f = new form(10) {} 'no error

The "{}" is the "array of" part & the "(10)" is "with bounds of 10", The
"{}" as "array of" is what caught the OP off guard.

The syntax is documented at:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcn7/html/vaconDeclaringArrays.asp

Under the "Initializing Arrays" section.

<quote>
- Assign an array object to the variable, using the New clause. When you
use a New clause, you must follow it with braces ({}), even if they are
empty.

Dim BA(2) As Byte ' Currently contains Nothing (no object).
Dim BA(2) As Byte = New Byte() ' INVALID (New after length specified).
Dim BA() As Byte = New Byte() {} ' Empty Byte array object.
Dim BA() As Byte = New Byte() ' INVALID (missing braces).
Dim BA() As Byte = New Byte(2) ' INVALID (missing braces).
Dim BA() As Byte = New Byte() {0,1,2} ' (0) through (2).
Dim BA() As Byte = New Byte(2) {0,1,2} ' (0) through (2).
</quote>

I would have expected one more example in there.

Dim BA() As Byte = New Byte(2) {}

Hope this helps
Jay
 
A

Armin Zingler

Jay B. Harlow said:
Armin,
Think of the second as saying "New Array of Form with bounds of 10",
only backwards & mixed up. ;-)
:)


The "{}" is the "array of" part & the "(10)" is "with bounds of 10",
The "{}" as "array of" is what caught the OP off guard.

The syntax is documented at:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcn7/html/vaconDeclaringArrays.asp

Under the "Initializing Arrays" section.

<quote>
- Assign an array object to the variable, using the New clause. When
you use a New clause, you must follow it with braces ({}), even if
they are empty.

Dim BA(2) As Byte ' Currently contains Nothing (no object).
Dim BA(2) As Byte = New Byte() ' INVALID (New after length
specified). Dim BA() As Byte = New Byte() {} ' Empty Byte array
object.
Dim BA() As Byte = New Byte() ' INVALID (missing braces).
Dim BA() As Byte = New Byte(2) ' INVALID (missing braces).
Dim BA() As Byte = New Byte() {0,1,2} ' (0) through (2).
Dim BA() As Byte = New Byte(2) {0,1,2} ' (0) through (2).
</quote>


Yes, it's documented, but I'd see it this way:


New Form 'create new form
New Form(10) 'create new form + call param. ctor
New Form(10){} 'create new form + call param. ctor + assign empty array


Do you see why I'm confused?

I would have expected one more example in there.

Dim BA() As Byte = New Byte(2) {}

The missing example shows that they forgot to disallow this version. ;-)
 
J

Jay B. Harlow [MVP - Outlook]

Cor,
Dim myform() As form = New form(10) {}
Dim myform(10) As form
Yes these two are the same as are these two:

myform() = New form(10) {}
ReDim myform(10)
For what you wrote, I tell it always exact in the way you did write it.
Using the Visualbasic functions, describes the things often better than the
.Net methods.
(I try to avoid them but that is my own decission).
O.K. maybe the Visual Basic functions were not the best example. Would
Overloading a method verses using Optional parameters have been a better
example? Or using "While - End While" instead of "Do While - Loop"? I am
trying to demostrate that there are a number of alternative ways to do the
exact same thing, sometimes the syntax on the alternative may catch us off
guard, but it is still valid VB.NET.

Also it may be "bad" today, but after looking at the new syntax & using it,
tomorrow it may be "good". ;-)
If my example above is right, than is this a way of creating a bad readable
program.
I don't like the terms bad & better, as its a matter of perspective (is the
glass half empty or half full). At the same time I am not disagreeing with
you one maybe "bad" and the other may be "better", as I find the Visual
Basic functions make things easier.

However for beginner developers I will agree it can be confusing. Which is
where I prefer to point out it is valid, try to explain how it works.

Hope this helps
Jay
 
H

Herfried K. Wagner [MVP]

* "Armin Zingler said:
Right, that's why I thought the syntax it is _not_ possible.


Dim f As Form()

f = new form(10) 'error

Only error if there is not a parameterized ctor defined in 'form'.
f = new form(10) {} 'no error

As the second creates a Form array and assigns 0 items, I expect the first
one to create a Form array without assigning an item. So, both are equal.

The curly braces are only used to aviod ambiguity (ctor call vs. array
declaration).
In other words, I do know that the first creates a new Form and passes 10 to
the constructor, but as it is true, the second should not work because the
curly braces don't make sense when creating a single instance.

Why not? Where do you create a "single instance"?
 
H

Herfried K. Wagner [MVP]

* "Cor said:
If I am wrong tell it me, from the example from Herfried I understand that
this is the same

Dim myform() As form = New form(10) {}
Dim myform(10) As form

Yes, they are the same. But you will need the fist one when reassigning
an array of 'form' later in the code, not in the declaration directly.
For what you wrote, I tell it always exact in the way you did write it.
Using the Visualbasic functions, describes the things often better than the
.Net methods.
(I try to avoid them but that is my own decission).

The "new" way of declaring arrays is IMO not the best one:

\\\
Dim afrm As MyForm()
///

*really ugly*
If my example above is right, than is this a way of creating a bad readable
program.

This depends on the skills of the reader.
 
A

Armin Zingler

Herfried K. Wagner said:
Only error if there is not a parameterized ctor defined in 'form'.

No, also error if a parameterized ctor is definied because f is declared as
an array.
The curly braces are only used to aviod ambiguity (ctor call vs.
array declaration).


Why not? Where do you create a "single instance"?

New Form 'create new form
New Form(10) 'create new form + call param. ctor
New Form(10){} 'create new form + call param. ctor + assign empty array
 
J

Jay B. Harlow [MVP - Outlook]

Armin,
Do you see why I'm confused?

I knew why you confused from the start, as I was a little startled/taken
back when I first saw the syntax, then it was: O.K. I can do that.
The missing example shows that they forgot to disallow this version. ;-)

<rant>I have a bigger problem with the "off by one" syntax in VB.NET arrays.
In that VB.NET arrays use upper bound as oppose to number of elements. To me
that is just asking for trouble as I suspect most beginners expect the low
bound to be one, not zero. Which means they have this 'hidden' element
floating around.

For example:

<beginner mode>
Dim list(2) As String ' define a two element array
list(1) = "Hello"
list(2) = "World"
Dim str As String
str = String.Join(" ", list)

Why does my str have a leading space on it?

</beginner mode>
</rant>

Just a thought,
Jay
 

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

Top