Results 1 to 8 of 8

Thread: what does this statement means? Dim arParms() As SqlParameter = New SqlParameter(3)

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2003
    Posts
    436

    what does this statement means? Dim arParms() As SqlParameter = New SqlParameter(3)

    Dim arParms() As SqlParameter = New SqlParameter(3) {}

  2. #2
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861
    It is declaring an array of 3 SqlParameters to be passed to a stored procedure.
    Being educated does not make you intelligent.

    Need a weekend getaway??? Come Visit

  3. #3
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    4 actually since it is zero based and they are all empty parameters (no name or value).

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2003
    Posts
    436

    Is there a easy way to write this statement?

    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

  5. #5
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Why break it into 2 lines?
    VB Code:
    1. Dim arParms() As SqlParameter
    2.         arParms = New SqlParameter(3) {}

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2003
    Posts
    436

    why should we use ...{} at the end?

    Dim arParms() As SqlParameter
    arParms = New SqlParameter(3) {}


    I didn't understand the '{}' at the end of second line.
    why should we use it?

  7. #7
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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:
    1. Dim arParms(3) As SqlParameter

    If you really wanted to fill the array with 4 actual objects you'd do it like this:
    VB Code:
    1. Dim arParms() As SqlParameter = {New SqlParameter, New SqlParameter, New SqlParameter, New SqlParameter}
    That creates 4 instances of SqlParameters and passes them into the array.

    To better illustrate the difference run this:
    VB Code:
    1. Dim arParms1() As SqlParameter = New SqlParameter(3) {}
    2.         For Each i As SqlParameter In arParms1
    3.             MsgBox(IsNothing(i), , TypeName(i))
    4.         Next
    5.  
    6.         Dim arParms2() As SqlParameter = {New SqlParameter, New SqlParameter, New SqlParameter, New SqlParameter}
    7.         For Each i As SqlParameter In arParms2
    8.             MsgBox(IsNothing(i), , TypeName(i))
    9.         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.
    Last edited by Edneeis; Dec 16th, 2003 at 06:44 PM.

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2003
    Posts
    436

    Awesome explanation

    Ed..
    You are the man..The Guru...

    thanks my friend.

    nath

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width