473,382 Members | 1,165 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Array literals/constants as default arguments in functions?

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

Nov 20 '05 #1
2 3406
"Urs van Binsbergen" <eg********@robotnik-spam-goaway.ch> 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

Nov 20 '05 #2
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" <eg********@robotnik-spam-goaway.ch> wrote in message
news:3F**************@robotnik-spam-goaway.ch...
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

Nov 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Bob | last post by:
I've tried everything; and I can't seem to get past this VERY (seemingly) simply problem. I want to work with an array variable within a function(s). I can't get it to work; if I: 1) global...
41
by: Berk Birand | last post by:
Hi, I am just learning about the array/pointer duality in C/C++. I couldn't help wondering, is there a way to pass an array by value? It seems like the only way to do is to pass it by...
20
by: Petter Reinholdtsen | last post by:
Is the code fragment 'char a = ("a");' valid ANSI C? The problematic part is '("a")'. I am sure 'char a = "a";' is valid ANSI C, but I am more unsure if it is allowed to place () around the...
6
by: kobu.selva | last post by:
I was recently part of a little debate on the issue of whether constants and string literals are considered "data objects" in C. I'm more confused now than before. I was always under the...
13
by: hn.ft.pris | last post by:
Hi: I have the following simple program: #include<iostream> using namespace std; int main(int argc, char* argv){ const double L = 1.234; const int T = static_cast<const int>(L); int arr;
9
by: barcaroller | last post by:
Can variables be used for array size in C++? I know that in the past, I could not do the following: foo (int x) { type arr; } I have recently seen code that does exactly that. Is it right?
27
by: SasQ | last post by:
Hello. I wonder if literal constants are objects, or they're only "naked" values not contained in any object? I have read that literal constants may not to be allocated by the compiler. If the...
29
by: Richard Harter | last post by:
There is probably a simple way to do what I want but I don't see it. Any suggestions are welcome. Suppose I have a function foo with an argument that can be any of several types and that I want...
8
by: fauxanadu | last post by:
Hello: I've been working on an assignment and I have hit a stumbling block pertaining to an array of pointers. If I am correct in my assumptions, the array struct animal* zoo should have...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.