Syntax Question

  • Thread starter Thread starter Robert Brinson
  • Start date Start date
R

Robert Brinson

The company I am working for is trying to work with the Application
Data Blocks that Microsoft released to provide transparent database
connectivity for our .NET applications. I have been tasked with having
a thorough understanding of the code. However, while wading through
the source, I came across a section of code that does not make sense
to me. I was hoping that someone here would be able to enlighten me.
The code in question is in the SqlDbHelper.vb file and is under the
DiscoverSpParameterSet function:

Dim cn As New SqlConnection(connectionString)
Dim cmd As SqlCommand = New SqlCommand(spName, cn)
Dim discoveredParameters() As IDataParameter

Try
cn.Open()
cmd.CommandType = CommandType.StoredProcedure
SqlCommandBuilder.DeriveParameters(cmd)
If Not includeReturnValueParameter Then
cmd.Parameters.RemoveAt(0)
End If

discoveredParameters = New SqlParameter(cmd.Parameters.Count - 1)
{}
cmd.Parameters.CopyTo(discoveredParameters, 0)
Finally
cmd.Dispose()
cn.Dispose()
End Try

The line I am not understanding is:

discoveredParameters = New SqlParameter(cmd.Parameters.Count - 1) {}

I can see that an instance of a SqlParameter object is being created.
However, none of the overloaded constructors match the arguments
given. Though at the end of the statement are an opening and closing
curly brace ({}). If this is removed, I do indeed get the expected
error that I just mentioned. However, with these curly braces,
everything is fine. What is being accomplished here? Thanks in advance
for any insight you can provide.

Robert
 
Robert Brinson said:
The line I am not understanding is:

discoveredParameters = New SqlParameter(cmd.Parameters.Count - 1)
{}

I can see that an instance of a SqlParameter object is being
created. However, none of the overloaded constructors match the
arguments given. Though at the end of the statement are an opening
and closing curly brace ({}). If this is removed, I do indeed get the
expected error that I just mentioned. However, with these curly
braces, everything is fine. What is being accomplished here? Thanks
in advance for any insight you can provide.


I (also) think this is a language inconsistency:

'creates a new instance of <type>, passing <args> to the ocnstructor.
var = New <type>(<args>)

'creates an *array* of <type>, upper bound is <args>, no items are put in
the
array:
var = new <type>(<args>){}

'creates an *array* of <typeY, upper bound is <args>, one item is put in the
array:
var = new <type>(<args>){<Item>}
 
Hi Robert,

I waited on Armin, because he had a long arguing about this empty string
last week.

But if I was you I did put this message also it in the
public.dotnet.framework.adonet newsgroup.

A lot of those who are active in that newsgroup knows especialy much from
the parameters, which are less used by the more VB.language affected people.

Then with the answers from 2 directions I think you have the best response.

Cor
 
Herfried K. Wagner said:
Please point out what's "inconsistent" behavior...

As stated in the previous thread:

Me:
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

You:
In the last line: Why should it call the ctor?

Me:
For the same reason as in the 2nd line.


You didn't reply to my last statement. ;-)
 
* "Armin Zingler said:
As stated in the previous thread:

Me:
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

The last line doesn't call the parameterized ctor. It creates an array
with 11 elements, all pointing to 'Nothing'.
You:
In the last line: Why should it call the ctor?

Me:
For the same reason as in the 2nd line.

LOL, but where to insert the element?
 
Herfried K. Wagner said:
The last line doesn't call the parameterized ctor. It creates an
array with 11 elements, all pointing to 'Nothing'.

Right, in opposite to the 2nd line the 3rd line creates an array. That's the
inconsistence you were asking for.
LOL, but where to insert the element?

Which element? A Form does not need an element.
 
* "Armin Zingler said:
Right, in opposite to the 2nd line the 3rd line creates an array. That's the
inconsistence you were asking for.

That's IMO not inconsistent. "(", ")" have multiple semantics in VB.NET,
the "{}" tells VB.NET to interpret the "(", ")" as array bound specifier
brackets.
Which element? A Form does not need an element.

If the last version listed above would call the ctor too, where would
you expect this object to be inserted in the array?

\\\
Dim f() As Form = New Form(10) {}
///
 
Herfried K. Wagner said:
That's IMO not inconsistent. "(", ")" have multiple semantics in
VB.NET, the "{}" tells VB.NET to interpret the "(", ")" as array
bound specifier brackets.

I consider the ambiguous interpretation of the "()" to be inconsistent. The
"{}" are optional:

new form(10)[array-initializer]

If the last version listed above would call the ctor too, where
would you expect this object to be inserted in the array?

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

As "New Form(10){}" doesn't make sense, the whole line doesn't, so I can't
answer the question.

All IMO.
 
* "Armin Zingler said:
That's IMO not inconsistent. "(", ")" have multiple semantics in
VB.NET, the "{}" tells VB.NET to interpret the "(", ")" as array
bound specifier brackets.

I consider the ambiguous interpretation of the "()" to be inconsistent. The
"{}" are optional:

new form(10)[array-initializer]

That's how _you_ interpret it.
As "New Form(10){}" doesn't make sense, the whole line doesn't, so I can't
answer the question.

You would forbid this syntax?

;->
 
Herfried K. Wagner said:
Right, in opposite to the 2nd line the 3rd line creates an
array. That's the inconsistence you were asking for.

That's IMO not inconsistent. "(", ")" have multiple semantics
in VB.NET, the "{}" tells VB.NET to interpret the "(", ")" as
array bound specifier brackets.

I consider the ambiguous interpretation of the "()" to be
inconsistent. The "{}" are optional:

new form(10)[array-initializer]

That's how _you_ interpret it.

Yes, but if I'd interpret it the way VB does, it is ambiguous. It is
ambiguous because the "()" brackets can be interpreted this or that way.

You would forbid this syntax?

Yes


....and: no, I currently can't offer an alternative. ;-)
 
* "Armin Zingler said:
Right, in opposite to the 2nd line the 3rd line creates an
array. That's the inconsistence you were asking for.

That's IMO not inconsistent. "(", ")" have multiple semantics
in VB.NET, the "{}" tells VB.NET to interpret the "(", ")" as
array bound specifier brackets.

I consider the ambiguous interpretation of the "()" to be
inconsistent. The "{}" are optional:

new form(10)[array-initializer]

That's how _you_ interpret it.

Yes, but if I'd interpret it the way VB does, it is ambiguous. It is
ambiguous because the "()" brackets can be interpreted this or that way.
;-)
You would forbid this syntax?

Yes

...and: no, I currently can't offer an alternative. ;-)

Maybe "[", "]" for arrays?

SCNR
 
Hi Armin and Herfried,

This discussion has been done, I was very short involved, I thought that the
outcoming was something on a suggestion from Jay B. (on a sentence that I
made conform what Armin is saying now) that this code should be possible but
not be prefered.

As you probably know, I like multiple possibilities in code so I did agree
with him, although I find it stupid code.

Cor
 
* "Cor said:
This discussion has been done, I was very short involved, I thought that the
outcoming was something on a suggestion from Jay B. (on a sentence that I
made conform what Armin is saying now) that this code should be possible but
not be prefered.

.... and I don't agree with that.
As you probably know, I like multiple possibilities in code so I did agree
with him, although I find it stupid code.

;-)
 
Back
Top