Monday, May 10, 2010

Visual C#.Net: Optional Parameters


I've been developing desktop applications in VB6 before and been using the OPTIONAL class where a parameter is not totally required in your function. However, upon developing applications in Visual C#.Net, I didn't find that option. After googling around I found out that you can also do optional parameters on your function. Here is how you can do it.

in the declarations, add this:

using System.Runtime.InteropServices;

After doing it, you may be able to use Optional class like how I used it below;

public string WhatsUp (string UrMessage, [Optional] string Name)
{
     if (Name != "")
     {
          return UrMessage + ", " + Name;
     }
     else
     {
          return UrMessage;
     }
}


Enjoy developing applications people.
Noel Ablon Web Developer

I am Noel Ablon. A father of two wonderful kids. A loving husband to a wondeful wife. A Bible believing Christian. A web developer / graphics artist / programmer or a fullstack web developer.

No comments:

Post a Comment