473,320 Members | 1,939 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,320 software developers and data experts.

how to pass in empty value for array argument?

Tee
Hi,

When I have a function like:
private string xyz(string Text)

I can call it without passing in a string with xyz("") or xyz(string.Empty)

but if the paramater needed is an array like:
private string xyz(string[] ArrayName)

how do I call it without giving an array using the same idea as above?
Thanks,
Tee
Nov 16 '05 #1
5 22034
Tee <th*@streamyx.com> wrote:
When I have a function like:
private string xyz(string Text)

I can call it without passing in a string with xyz("") or xyz(string.Empty)
No, both of those *are* passing strings. Empty strings, but strings
nonetheless.
but if the paramater needed is an array like:
private string xyz(string[] ArrayName)

how do I call it without giving an array using the same idea as above?


What I think you're after is null.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
Jon,

I believe the current best practise is to pass in an empty array, such as:
.... xyz( new string[0] );

Especially if this is an internally called routine where you trust the input
parameters, as you can go straight into (for example) a foreach loop without
the additional branches required to test for null.

Obviously if this may be called by third party code then you will need to
check this for null first, and therefore either technique would be suitable.

Cheers,
Chris.
"Jon Skeet [C# MVP]" wrote:
Tee <th*@streamyx.com> wrote:
When I have a function like:
private string xyz(string Text)

I can call it without passing in a string with xyz("") or xyz(string.Empty)


No, both of those *are* passing strings. Empty strings, but strings
nonetheless.
but if the paramater needed is an array like:
private string xyz(string[] ArrayName)

how do I call it without giving an array using the same idea as above?


What I think you're after is null.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #3
Chris Ballard <wo*********@typeYAHOOhere.com> wrote:
I believe the current best practise is to pass in an empty array, such as:
... xyz( new string[0] );
That entirely depends on what the method is expecting. Some methods
will accept null and others won't.
Especially if this is an internally called routine where you trust the input
parameters, as you can go straight into (for example) a foreach loop without
the additional branches required to test for null.
Possibly - or possibly passing in something which isn't an array at all
(which is what was stated as the goal) may have distinct meaning.
Obviously if this may be called by third party code then you will need to
check this for null first, and therefore either technique would be suitable.


Not necessarily - it's up to the method. Many of the public methods I
write will throw a NullArgumentException if null is passed when it
shouldn't be.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
Jon,

Sorry I was making the assumption that xyz is an internally created method,
and my response was from the standpoint of the implementor of such a method.

Obviously if this method is part of a third party library, then anyone
calling into it will need to consult the related documentation to find out
what values are appropriate for indicating an "empty" parameter.

Chris.

"Jon Skeet [C# MVP]" wrote:
Chris Ballard <wo*********@typeYAHOOhere.com> wrote:
I believe the current best practise is to pass in an empty array, such as:
... xyz( new string[0] );


That entirely depends on what the method is expecting. Some methods
will accept null and others won't.
Especially if this is an internally called routine where you trust the input
parameters, as you can go straight into (for example) a foreach loop without
the additional branches required to test for null.


Possibly - or possibly passing in something which isn't an array at all
(which is what was stated as the goal) may have distinct meaning.
Obviously if this may be called by third party code then you will need to
check this for null first, and therefore either technique would be suitable.


Not necessarily - it's up to the method. Many of the public methods I
write will throw a NullArgumentException if null is passed when it
shouldn't be.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #5
Chris Ballard <wo*********@typeYAHOOhere.com> wrote:
Sorry I was making the assumption that xyz is an internally created method,
and my response was from the standpoint of the implementor of such a method.

Obviously if this method is part of a third party library, then anyone
calling into it will need to consult the related documentation to find out
what values are appropriate for indicating an "empty" parameter.


I don't think the third-party/internal divide makes any difference, to
be honest. In some cases it's a good idea to use an empty array for
parameters, in some cases null is more appropriate. It depends on
whether you want to pass an empty collection of values or say "I have
no collection to pass, not even an empty one". That decision needs to
be made both for internal methods and public ones.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6

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

Similar topics

3
by: Nath | last post by:
Please help!? I am new to writing html, javascript, pretty new to MySQL but quite proficient at writing Perl and i'm a quick learner. I am building a database driven website and i am a little...
5
by: Seeker | last post by:
Newbie question here... I have a form with some radio buttons. To verify that at least one of the buttons was chosen I use the following code ("f" is my form object) : var btnChosen; for...
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
1
by: Mark Dicken | last post by:
Hi All I have found the following Microsoft Technet 'Q' Article :- Q210368 -ACC2000: How to Pass an Array as an Argument to a Procedure (I've also copied and pasted the whole contents into...
5
by: wilson | last post by:
Dear all, In this time, I want to pass array to function. What should I declare the parameter in the function?i int array or int array? Which one is correct? ...
21
by: vmsgman | last post by:
Here is a code sample ... int blah = ReadFile( defArray, defFileName, w, h); // Read File Contents into memory array and return for processing public int ReadFile( ref ushort nArray, string...
14
by: Abhi | last post by:
I wrote a function foo(int arr) and its prototype is declared as foo(int arr); I modify the values of the array in the function and the values are getting modified in the main array which is...
11
by: venkatagmail | last post by:
I have problem understanding pass by value and pass by reference and want to how how they are or appear in the memory: I had to get my basics right again. I create an array and try all possible...
24
by: =?Utf-8?B?U3dhcHB5?= | last post by:
Can anyone suggest me to pass more parameters other than two parameter for events like the following? Event: Onbutton_click(object sender, EventArgs e)" Event handler: button.Click += new...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
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: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.