Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET General > Dynamic array as optional parameter


Reply
 
Thread Tools Display Modes
  #1  
Old 02-16-2005, 07:12 AM
Slow-Mo Slow-Mo is offline
Freshman
 
Join Date: Dec 2004
Posts: 41
Default 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.
Reply With Quote
  #2  
Old 02-16-2005, 07:24 AM
AFterlife's Avatar
AFterlife AFterlife is offline
Contributor
 
Join Date: Dec 2003
Location: Mi
Posts: 654
Default

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
Reply With Quote
  #3  
Old 02-16-2005, 07:34 AM
Slow-Mo Slow-Mo is offline
Freshman
 
Join Date: Dec 2004
Posts: 41
Default

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?
Reply With Quote
  #4  
Old 02-16-2005, 07:47 AM
AFterlife's Avatar
AFterlife AFterlife is offline
Contributor
 
Join Date: Dec 2003
Location: Mi
Posts: 654
Default

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.
Reply With Quote
  #5  
Old 02-16-2005, 12:00 PM
Iceplug's Avatar
Iceplug Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: California, USA
Posts: 16,583
Default

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 )
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #6  
Old 02-16-2005, 12:03 PM
OnErr0r's Avatar
OnErr0r OnErr0r is offline
Obsessive OPtimizer

Administrator
* Guru *
 
Join Date: Jun 2002
Location: Debug Window
Posts: 13,685
Default

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.
Reply With Quote
  #7  
Old 02-16-2005, 01:11 PM
Deadalus Deadalus is offline
Promising Talent

Retired Moderator
* Guru *
 
Join Date: May 2002
Location: Brussels
Posts: 3,601
Default

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.
Reply With Quote
  #8  
Old 02-16-2005, 01:14 PM
Mike Rosenblum's Avatar
Mike Rosenblum Mike Rosenblum is offline
Microsoft Excel MVP

Forum Leader
* Guru *
 
Join Date: Jul 2003
Location: New York, NY, USA
Posts: 7,848
Default

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:
Code:
= {1}
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
__________________
My Articles:
| Excel from .NET | Excel RibbonX using VBA | Excel from VB6 | CVErr in .NET | MVP |
Avatar by Lebb
Reply With Quote
  #9  
Old 02-16-2005, 01:25 PM
OnErr0r's Avatar
OnErr0r OnErr0r is offline
Obsessive OPtimizer

Administrator
* Guru *
 
Join Date: Jun 2002
Location: Debug Window
Posts: 13,685
Default

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.
Reply With Quote
  #10  
Old 02-16-2005, 01:28 PM
Mike Rosenblum's Avatar
Mike Rosenblum Mike Rosenblum is offline
Microsoft Excel MVP

Forum Leader
* Guru *
 
Join Date: Jul 2003
Location: New York, NY, USA
Posts: 7,848
Default

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.
__________________
My Articles:
| Excel from .NET | Excel RibbonX using VBA | Excel from VB6 | CVErr in .NET | MVP |
Avatar by Lebb
Reply With Quote
  #11  
Old 02-16-2005, 01:42 PM
OnErr0r's Avatar
OnErr0r OnErr0r is offline
Obsessive OPtimizer

Administrator
* Guru *
 
Join Date: Jun 2002
Location: Debug Window
Posts: 13,685
Default

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.
Reply With Quote
  #12  
Old 02-16-2005, 02:25 PM
Mike Rosenblum's Avatar
Mike Rosenblum Mike Rosenblum is offline
Microsoft Excel MVP

Forum Leader
* Guru *
 
Join Date: Jul 2003
Location: New York, NY, USA
Posts: 7,848
Default

See long-winded, silly reply regarding Overoading vs. Optional Parameters here:

http://www.xtremevbtalk.com/showthre...066#post945066

,
Mike
__________________
My Articles:
| Excel from .NET | Excel RibbonX using VBA | Excel from VB6 | CVErr in .NET | MVP |
Avatar by Lebb
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->