Syntax Question

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

Robert Brinson

This is a cross post originating in
microsoft.public.dotnet.languages.vb:

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. BTW, I do understand that an array is
being constructed. However, I do not understand how I am able to
instantiate a new SqlParameter without the required number of
arguments.

Robert
 
its the alternate array declaration

dim i(11) as integer 'old way declaring an
array
dim i () as integer = new integer(11) {} 'new way

so

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

sets discoveredParameters to a new array of SqlParameter. you need this
silly syntax because array's and function both use ()'s
 
Back
Top