-1

I have a VB6 function that has an optional date interval parameter that I'm trying to convert to C#. I'm also not sure of the best way to handle this in code. Here is the VB function declaration:

Private Function ReplaceDateTextNonBinary(psTable as String, psColumn as String, psColumnOffSet as String, psDateFormat as String, Optional psInterval as String = "n")

This function uses the optional parameter in the following DateAdd method call.

DateTime = DateAdd(psInterval, oSQL.Value(psColumnOffset), Date$)

Here is how I was planning to convert the function declaration to C# using the params keyword.

private static bool ReplaceDateTextNonBinary(string psTable, string psColumn, string pColumnOffset, string psDateFormat, params string psInterval)

I think this would work but I'm don't know how to code this that would take any date interval as a string. I was thinking of using a switch...case statement but that didn't seem very elegant.

Any thoughts.

  • The C# `params` keyword is similar to the `ParamArray` VB keyword, so it's not what you're looking for here. – Jon Egerton Feb 11 '15 at 16:27
  • 4
    Please use Google before you ask for help. The obvious query is "c# optional parameter". Take the first hit. – Hans Passant Feb 11 '15 at 16:30
  • Sorry for posting a duplicate question. I've been looking on Google and on here most of the day and came up with nothing. I'm not sure how to phase the question any better. – GhostHunterJim Feb 11 '15 at 16:41

1 Answers1

2

You can use an optional parameter in C# just like this:

private static bool ReplaceDateTextNonBinary(string psTable, 
                                             string psColumn, 
                                             string pColumnOffset, 
                                             string psDateFormat, 
                                             string psInterval = "n")

If the optional parameter isn't passed, it'll get the value "n". Note that optional parameters must be listed last in the method signature. Also note this is a C# 4.0 feature and won't work with earlier versions of C# (in which case, simple overloading is probably your best bet).

See: Named and Optional Arguments (C# Programming Guide)

The prior to 4.0 way to do it would be something like this:

private static bool ReplaceDateTextNonBinary(string psTable, 
                                             string psColumn, 
                                             string pColumnOffset, 
                                             string psDateFormat)
{
    return ReplaceDateTextNonBinary(psTable,
                                    psColumn,
                                    pColumnOffset,
                                    psDateFormat,
                                    "n");
}

private static bool ReplaceDateTextNonBinary(string psTable, 
                                             string psColumn, 
                                             string pColumnOffset, 
                                             string psDateFormat, 
                                             string psInterval)
{
    // your implementation here
} 
Matt Burland
  • 44,552
  • 18
  • 99
  • 171