PC Review


Reply
Thread Tools Rate Thread

Array literals/constants as default arguments in functions?

 
 
Urs van Binsbergen
Guest
Posts: n/a
 
      2nd Sep 2003
Hi!

This sounds probably quite silly, but I just can't figure out this one.
Consider the following which lets "a" be an array with 0 elements:

Dim a() As Integer
a = New Integer() {}

Now what do I have to code if I want an empty array as default value in
a function? This one doesn't work:

Function test(Optional ByVal a As Integer() = New Integer() {})
End Function

It says: Constant expression required. Is this not possible in general
because default values can only be simple types?

Greets and thanks,
Urs

 
Reply With Quote
 
 
 
 
Armin Zingler
Guest
Posts: n/a
 
      3rd Sep 2003
"Urs van Binsbergen" <ego-(E-Mail Removed)> schrieb
> This sounds probably quite silly, but I just can't figure out this
> one. Consider the following which lets "a" be an array with 0
> elements:
>
> Dim a() As Integer
> a = New Integer() {}
>
> Now what do I have to code if I want an empty array as default value
> in a function? This one doesn't work:
>
> Function test(Optional ByVal a As Integer() = New Integer() {})
> End Function
>
> It says: Constant expression required. Is this not possible in
> general because default values can only be simple types?


Yes. Add an overloaded version:

Function test()
Test(New Integer() {})
End Function


--
Armin

 
Reply With Quote
 
Jay B. Harlow [MVP - Outlook]
Guest
Posts: n/a
 
      3rd Sep 2003
Urs,
In addition to the overload that Armin used.

Seeing as the parameter is an Optional array, consider using a ParamArray
instead:

Function Test(ByVal ParamArray a() As Integer) As Integer
End Function

Then when you call it, passing no parameters you will get an empty array.

Dim x As Integer
x = Test()

Passing a list of intergers you will get an array of integers.

x = Test(1,2,3,4,5)

Passing an array of integers you will get that array of integers.

Dim a() As Integer = {1,2,3,4,5}
x = Test(a)

Hope this helps
Jay

"Urs van Binsbergen" <ego-(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi!
>
> This sounds probably quite silly, but I just can't figure out this one.
> Consider the following which lets "a" be an array with 0 elements:
>
> Dim a() As Integer
> a = New Integer() {}
>
> Now what do I have to code if I want an empty array as default value in
> a function? This one doesn't work:
>
> Function test(Optional ByVal a As Integer() = New Integer() {})
> End Function
>
> It says: Constant expression required. Is this not possible in general
> because default values can only be simple types?
>
> Greets and thanks,
> Urs
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
String functions vs. XML literals... speed Arthur Dent Microsoft VB .NET 1 8th Jul 2008 08:06 PM
Functions and worksheets, passing arguments to functions? newbie Microsoft Excel Discussion 3 3rd Jan 2008 06:54 PM
Constants Tied to Arguments in Methods Ronald Dodge Microsoft Excel Programming 3 14th Nov 2007 08:06 AM
VBA - array or collection literals? Marcus =?iso-8859-15?Q?Sch=F6neborn?= Microsoft Excel Programming 2 21st Sep 2007 08:52 PM
Array of literals or better? Duncan Welch Microsoft ASP .NET 2 27th Jul 2004 01:36 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:36 PM.