Dim arParms() As SqlParameter = New SqlParameter(3) {}
Dim arParms() As SqlParameter = New SqlParameter(3) {}
It is declaring an array of 3 SqlParameters to be passed to a stored procedure.
4 actually since it is zero based and they are all empty parameters (no name or value).
Instead of having only one line...
Dim arParms() As SqlParameter = New SqlParameter(3) {}
can we break it into 2 lines?
I tried writing and it doesn't work..
dim arparms() as sqlparameter
arparms(0 = new sqlparameter(3)
any ideas please...
thanks
Why break it into 2 lines?
VB Code:
Dim arParms() As SqlParameter arParms = New SqlParameter(3) {}
Dim arParms() As SqlParameter
arParms = New SqlParameter(3) {}
I didn't understand the '{}' at the end of second line.
why should we use it?
The {} declare the contents of the array at declaration. Without them it will think the 3 is being passed as a parameter to the SQLParameter objects contructor. Usually you'd pass the elements of the array in the {}.
On closer inspection your code actually fills the arParms with 4 sqlparameter objects that are all set to nothing. This is because nothing is passed in the {}. So it is the same as declaring this:
VB Code:
Dim arParms(3) As SqlParameter
If you really wanted to fill the array with 4 actual objects you'd do it like this:
That creates 4 instances of SqlParameters and passes them into the array.VB Code:
Dim arParms() As SqlParameter = {New SqlParameter, New SqlParameter, New SqlParameter, New SqlParameter}
To better illustrate the difference run this:
VB Code:
Dim arParms1() As SqlParameter = New SqlParameter(3) {} For Each i As SqlParameter In arParms1 MsgBox(IsNothing(i), , TypeName(i)) Next Dim arParms2() As SqlParameter = {New SqlParameter, New SqlParameter, New SqlParameter, New SqlParameter} For Each i As SqlParameter In arParms2 MsgBox(IsNothing(i), , TypeName(i)) Next
You'll notice that the first shows each item in the array is set to nothing and actually of nothing type. Where as the second one shows they are not set to nothing and are of type SqlParameter. I'm not sure which one you need for you application but that should explain the difference.
Ed..
You are the man..The Guru...
thanks my friend.
nath