Dynamic array as optional parameter

Slow-Mo
02-16-2005, 07:12 AM
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?

AFterlife
02-16-2005, 07:24 AM
You could specify it as an array.You could use an arraylist instead as the parameter since you want it dynamic.


Optional ByVal myArray As Array = Nothing

Slow-Mo
02-16-2005, 07:34 AM
Can I use:

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?

AFterlife
02-16-2005, 07:47 AM
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


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.

Iceplug
02-16-2005, 12:00 PM
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 )
:)

OnErr0r
02-16-2005, 12:03 PM
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.


Private Sub foo(ByVal i As Integer, Optional ByRef a() As Integer = Nothing)

Deadalus
02-16-2005, 01:11 PM
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.

Mike Rosenblum
02-16-2005, 01:14 PM
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:= {1} is not really a constant. It's an implicit call to creating a New Array(), and is really equivalent to this:= New Integer() {1} I think this is right?

Anyway, Slow-Mo, I would go with IcePlug's solution: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

OnErr0r
02-16-2005, 01:25 PM
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.

Mike Rosenblum
02-16-2005, 01:28 PM
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.

OnErr0r
02-16-2005, 01:42 PM
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.

Mike Rosenblum
02-16-2005, 02:25 PM
See long-winded, silly reply regarding Overoading vs. Optional Parameters here:

http://www.xtremevbtalk.com/showthread.php?p=945066#post945066

:),
Mike

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum