Logo 
Search:

Asp.net Forum

Ask Question   UnAnswered
Home » Forum » Asp.net       RSS Feeds

Using an array as an optional parameter

  Asked By: Rani    Date: Nov 27    Category: Asp.net    Views: 2046
  

I'm trying to use an optional array in a Sub but am having trouble
figuring out how to assign the default value.

For instance:
Sub MySub(Optional MyString as String() = ... )

I want to put an empty array in there but can't quite figure out how
that should work.

Can someone point me in the right direction?

Share: 

 

3 Answers Found

 
Answer #1    Answered By: Haboos Kauser     Answered On: Nov 27

Optional arguments are probably the wrong way and not CLS compliant.

Overloading is better.


Sub MySub( )
Dim strParm() As String = {"up", "down", "left", "right"}
Call MySub(strParm())
End Sub


Sub MySub(MyString as String() )
.....
End Sub

 
Answer #2    Answered By: Hadil Khan     Answered On: Nov 27

Are there any performance hits to using optional  params? What do you mean
by CLS compliant? I always kind of thought it all compiled down to the same
code when it was all said and done.

 
Answer #3    Answered By: Dale Jones     Answered On: Nov 27

Basically there are some things you can do in C# and VB.net that means
other languages may not be able to inherit the code and/or consume it properly.

For example C# supports unsigned INTs which are not a type that all .net
languages are guaranteed to support. If one used them as parameters to a
function a language like VB.net or Perl.net could not call that function.

If the unsigned INT happened inside the function any language could call
that function but if it appeared as parameter/signature a language that
could not construct an unsigned INT would have trouble calling it.

Same thing with Case Sensitive methods in C#

C# could call a DoTask() or doTask() method and would be calling 2 distinct
methods because their are 2 separate mothods case-sensitivity is what makes
them different.

If VB.net called the function Dotask or any of the capitalization variants
it would be ambigous which to call so we would call the C# class with case
sensitive methods "NOT CLS compliant".

C# and VB.net compilers have switches to check for CLS compliant and warn
if violated.


There are other good practices besides CLS compliance that FXCOP can catch.....
http://www.gotdotnet.com/team/libraries/
Just because code works and is fast enough does not ensure it is
written the best way.

 
Didn't find what you were looking for? Find more on Using an array as an optional parameter Or get search suggestion and latest updates.




Tagged: