 |
 |

02-16-2005, 07:12 AM
|
|
Freshman
|
|
Join Date: Dec 2004
Posts: 41
|
|
Dynamic array as optional parameter
|
How is it possible to create a Sub that accepts dynamic array as optional parameter.
I'm trying to use this:
Public Sub MySub(ByVal SomeBool As Boolean, Optional ByVal SomeArr() As Integer = {1})
But this doesn't work. Should I use object type instead?
|
Last edited by Slow-Mo; 02-16-2005 at 07:19 AM.
|

02-16-2005, 07:24 AM
|
 |
Contributor
|
|
Join Date: Dec 2003
Location: Mi
Posts: 654
|
|
You could specify it as an array.You could use an arraylist instead as the parameter since you want it dynamic.
Code:
Optional ByVal myArray As Array = Nothing
|
|

02-16-2005, 07:34 AM
|
|
Freshman
|
|
Join Date: Dec 2004
Posts: 41
|
|
Can I use:
Code:
Optional ByVal myArray() As Integer = Nothing
[/QUOTE]
I think I will have to check if myArray is nothing in code.
But anyway, how can I initialize the array?
|
|

02-16-2005, 07:47 AM
|
 |
Contributor
|
|
Join Date: Dec 2003
Location: Mi
Posts: 654
|
|
Well, if you want it dynamic you could use the arraylist. Then when you pass the arraylist to the function. So back in your main form declare it as an arraylist instead of an array of integers. You said you wanted it dynamic right?
Meaning resizable?
Then in your function to add the arraylist you would just use
Code:
Optional ByRef myArray as ArrayList=nothing
myArray.add(itemToAdd)
Keep in mind that if you wanted to retain new values that you would have to pass ByRef instead of ByVal so you pass the arraylist in, rather then a copy.
|
|

02-16-2005, 12:00 PM
|
 |
MetaCenturion
Retired Moderator * Guru *
|
|
Join Date: Aug 2001
Location: California, USA
Posts: 16,583
|
|
You could just opt for overloading the function that you want to call:
Public Sub MySub(SomeBool As Boolean)
Public Sub MySub(SomeBool As Boolean, ByVal SomeArr() As Integer )

|
|

02-16-2005, 12:03 PM
|
 |
Obsessive OPtimizer
Administrator * Guru *
|
|
Join Date: Jun 2002
Location: Debug Window
Posts: 13,685
|
|
Quote:
|
Originally Posted by Slow-Mo
How is it possible to create a Sub that accepts dynamic array as optional parameter.
I'm trying to use this:
Public Sub MySub(ByVal SomeBool As Boolean, Optional ByVal SomeArr() As Integer = {1})
But this doesn't work. Should I use object type instead?
|
It doesn't work because arrays must be passed ByRef, not ByVal.
Code:
Private Sub foo(ByVal i As Integer, Optional ByRef a() As Integer = Nothing)
|
__________________
Quis custodiet ipsos custodues.
|

02-16-2005, 01:11 PM
|
|
Promising Talent
Retired Moderator * Guru *
|
|
Join Date: May 2002
Location: Brussels
Posts: 3,601
|
|
Quote:
|
Originally Posted by OnErr0r
It doesn't work because arrays must be passed ByRef, not ByVal.
|
I don't think that's true in VB.NET. Arrays are reference variables, so you're passing a pointer anyway, but that you can pass with ByVal.
|
|

02-16-2005, 01:14 PM
|
 |
Microsoft Excel MVP
Forum Leader * Guru *
|
|
Join Date: Jul 2003
Location: New York, NY, USA
Posts: 7,848
|
|
Well in .Net I would think that the 'ByVal' restriction under VB6 would no longer apply, right? [Edit: Heh, Deadalus and I think alike... I think this must be beacause we read the same books. ]
I think the real problem is that: is not really a constant. It's an implicit call to creating a New Array(), and is really equivalent to this:
Code:
= New Integer() {1}
I think this is right?
Anyway, Slow-Mo, I would go with IcePlug's solution:
Code:
Public Sub MySub(someBool As Boolean)
Call MySub(someBool, New Integer() {1})
End Sub
Public Sub MySub(someBool As Boolean, ByVal someArr() As Integer)
' Your Code Goes Here
' Your Code Goes Here
' Your Code Goes Here
End Sub
-- Mike
|
|

02-16-2005, 01:25 PM
|
 |
Obsessive OPtimizer
Administrator * Guru *
|
|
Join Date: Jun 2002
Location: Debug Window
Posts: 13,685
|
|
Quote:
|
Originally Posted by Deadalus
I don't think that's true in VB.NET. Arrays are reference variables, so you're passing a pointer anyway, but that you can pass with ByVal.
|
Ah, true. I hadn't tried that in .net yet. And I would never think of trying it in legacy VB.
And in that case, to answer the original posters question in post #3: You could redim the array to a given size, if it was initially declared dynamically. If you didn't want an arraylist for some reason.
|
__________________
Quis custodiet ipsos custodues.
|

02-16-2005, 01:28 PM
|
 |
Microsoft Excel MVP
Forum Leader * Guru *
|
|
Join Date: Jul 2003
Location: New York, NY, USA
Posts: 7,848
|
|
Yes, Post #3 plus ReDim works.  Slow-mo, it's your choice, posts 5 and 8 avoid 'Optional' in favor of Overloading, which is preferred in .Net.
|
|

02-16-2005, 01:42 PM
|
 |
Obsessive OPtimizer
Administrator * Guru *
|
|
Join Date: Jun 2002
Location: Debug Window
Posts: 13,685
|
|
|
I can see the point of using overloads instead of multiple optional parameters. Due to the complexity of checking whether anything was passed. However, if there is a single optional parameter, a simple if statement might suffice. This could also lead to less code duplication over time and a smaller executable.
|
__________________
Quis custodiet ipsos custodues.
|

02-16-2005, 02:25 PM
|
 |
Microsoft Excel MVP
Forum Leader * Guru *
|
|
Join Date: Jul 2003
Location: New York, NY, USA
Posts: 7,848
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|
|
|
 |
|