|
-
May 13th, 2003, 11:05 AM
#1
Thread Starter
Fanatic Member
Optional Parameter default value can't be set?
Hi all,
Anyone know how I can do the following:
VB Code:
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.
-
May 13th, 2003, 12:22 PM
#2
Sleep mode
I was trying to do the same couple of days ago but I couldn't . You can't intialize optionl parameters within method signature .
-
May 13th, 2003, 12:34 PM
#3
Thread Starter
Fanatic Member
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).
-
May 13th, 2003, 12:36 PM
#4
Sleep mode
Uh..VS.NET2003 support that ? Can you post little sample for that ! I've tried with VS2002 but didn't work .
-
May 13th, 2003, 12:40 PM
#5
Thread Starter
Fanatic Member
For example, you can do this:
VB Code:
Public Sub MySub(ByVal strString As String, Optional ByVal strOptional As String = "String", _
Optional ByVal intOptional As Integer = 6)
End Sub
I don't know why I couldn't do the array though.
-
May 13th, 2003, 12:51 PM
#6
Sleep mode
This is what I wanted to do :
VB Code:
Public Sub MySub(ByVal strString As String, Optional ByVal Path_ As String = Application.StartupPath & "file.xxx")
End Sub
Did you try make them ByRef ? because arrays are referenced values not like other data types .
-
May 13th, 2003, 12:57 PM
#7
Frenzied Member
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
-
May 13th, 2003, 01:30 PM
#8
Thread Starter
Fanatic Member
Sweet! Thanks. I'll just check for nothing and use a private class byte array instead!
-
May 13th, 2003, 02:03 PM
#9
If you have to test for Nothing then it'd seem better to overload the method:
VB Code:
Public Sub SomeMethod(ByVal strString As String)
Dim bytArray() As Byte = {12, 13, 14}
Me.SomeMethod(strString,bytArray)
End Sub
Public Sub SomeMethod(ByVal strString As String, ByVal bytArray() As Byte)
End Sub
Actually I try to avoid the Optional Argument in favor of Overloading.
-
May 13th, 2003, 02:24 PM
#10
Thread Starter
Fanatic Member
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:
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|