I'm writting a class that simplifies my coding when using QBFC (quickbooks integration).
I am going to have many functions with optional paramaters of list of strings. The problem I have is that I am unsure how to specify the default value. I want to pass an empty list to code if a list is not specified. How does one do this. I'm sure this is a simple answer, but I'm coming up with a blank on every search I've done.
Thanks,
Cam
CamPosted Jun 23, 2008, 2:32 PM
Thanks for the reply, actually I just found the answer. I really wasn't asking about the optional keyword, I couldn't figgure out how to set an empty list as the default for the paramater. Here is the answer from the msdn (I wasn't reading very well):
Required for Optional parameters. Any constant or constant expression that evaluates to the data type of the parameter. If the type is Object, or a class, interface, array, or structure, the default value can only be Nothing.
So in my code I simply listed, "Optional ByVal ListIDList As List(Of String) = Nothing" in my parameter list.
I knew it was simple, I was simply just missing the answer right in front of my face.
Thanks for the QUICK response though.
Mahesh ChandPosted Jun 23, 2008, 2:22 PM
If you are using VB.NET, you can use keyword "optional" in front of a parameter. Every optional argument must declare a default value and there is no need of IsMissing function. For example:
Sub MyMethod(Optional ByVal i As Integer = 3)
---------------------------------------------------
C#, however, does not support optional parameters. You will have to overload your methods. For example, you may have a method with three overloaded forms something like this:
public string AddParams(string str1);
public string AddParams(string str1, string str2);
public string AddParams(string str1, string str2, string str3);
Other approach is, you MUST passs a null or emptry string and in your method check the value of the parameter and if its null or empty, depending on your approach, you can ignore that parameter.