Results 1 to 10 of 10

Thread: Optional Parameter default value can't be set?

  1. #1

    Thread Starter
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681

    Optional Parameter default value can't be set?

    Hi all,

    Anyone know how I can do the following:
    VB Code:
    1. Public Sub SomeMethod(ByVal strString As String, Optional ByVal bytArray() As Byte = {12, 13, 14})

    I get a squigly under the first { telling me "Expression Expected".

    Any ideas? I would rather not overload the method t get around this if I don't have to.

    Thanks.

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    I was trying to do the same couple of days ago but I couldn't . You can't intialize optionl parameters within method signature .

  3. #3

    Thread Starter
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681
    I can do it for strings and integers and the like, but not for the byte array for some reason. (I am using VS.NET 2003).

  4. #4
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Uh..VS.NET2003 support that ? Can you post little sample for that ! I've tried with VS2002 but didn't work .

  5. #5

    Thread Starter
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681
    For example, you can do this:
    VB Code:
    1. Public Sub MySub(ByVal strString As String, Optional ByVal strOptional As String = "String", _
    2.         Optional ByVal intOptional As Integer = 6)
    3. End Sub

    I don't know why I couldn't do the array though.

  6. #6
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    This is what I wanted to do :
    VB Code:
    1. Public Sub MySub(ByVal strString As String, Optional ByVal Path_ As String = Application.StartupPath & "file.xxx")
    2.  
    3. End Sub
    Did you try make them ByRef ? because arrays are referenced values not like other data types .

  7. #7
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    From MSDN:
    defaultvalue
    Required for Optional arguments. Any constant or constant expression that evaluates to the data type of the argument. If the type is Object, or a class, interface, array, or structure, the default value can only be Nothing.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  8. #8

    Thread Starter
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681
    Sweet! Thanks. I'll just check for nothing and use a private class byte array instead!

  9. #9
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    If you have to test for Nothing then it'd seem better to overload the method:
    VB Code:
    1. Public Sub SomeMethod(ByVal strString As String)
    2.    Dim bytArray() As Byte = {12, 13, 14}
    3.    Me.SomeMethod(strString,bytArray)
    4. End Sub
    5.  
    6. Public Sub SomeMethod(ByVal strString As String, ByVal bytArray() As Byte)
    7.  
    8. End Sub

    Actually I try to avoid the Optional Argument in favor of Overloading.

  10. #10

    Thread Starter
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681
    If I overloaded, then the method that takes doesn't take the array would simply call the one that did with the array defined in the class.

    I think in my case it makes more sense just to use the optional parameter and then have one line:
    VB Code:
    1. If arr Is Nothing Then arr = myClassArray

    From what I have heard, optional parameters are converted into overloaded methods when compiled. Not sure if this is true, but it would make sense.

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