• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 256
  • Last Modified:

User Date function using Overloading not working

Most expedient guru's,
I am coming to grips with C# from the sloppy world of VB.Net.  In truth I like the tight discipline of C# code.  But sometimes it just drives me bonkers the number of incredibly minute details the compiler chooses to have a nitpick with.

In this case I am writing a date function that can use either 2 arguments or 3 arguments.  The third argument is just to accomodate a parameter that isn't in use anymore.

What I'm getting back is:

The best overloaded method match for DatePart has some invalid argurments.

Argument 3 cannot convert from 'System.DayOfWeek' to 'int'.

I thought System.DayOfWeek was an integer?

public string DatePart(String fmt, String MyDate)
    //Overloading solution
    {
        return DatePart(fmt, MyDate, DayOfWeek.Sunday);
    }

    public string DatePart(String fmt, String MyDate, int StartDay)
    {

        DateTime dt = Convert.ToDateTime("1/1/1900");
        string  result = "";
        if  (!isDate(MyDate))
        {
            result = "";
        }
        else
        {
        dt = Convert.ToDateTime(MyDate);
            switch(fmt)
            {
                case "m":
                    result=dt.Month.ToString();
                    break;
                case "d":
                    result = dt.Day.ToString();
                    break;
                case "yyyy":
                    result = dt.Year.ToString();
                    break;
                case "w":
                    result = dt.DayOfWeek.ToString();
                    break;   
                case "mmmm":
                    result = dt.Month.ToString();
                    break;
                default:
                    result = "";
                    break;
            }
        }
        return result;

    }

Open in new window

0
tcalbaz
Asked:
tcalbaz
  • 2
  • 2
1 Solution
 
mrjoltcolaCommented:
No, enum types have a distinct type. http://msdn.microsoft.com/en-us/library/sbbt4032(v=vs.80).aspx

Yes, you can cast an enum to an int, but you shouldn't rely on the actual integer values of those, since that is a CLR implementation detail.

So I recommend you change your function to:

    public string DatePart(String fmt, String MyDate, DayOfWeek StartDay)

Open in new window

0
 
Todd GerbertIT ConsultantCommented:
Don't give this comment any points...

Just wanted to point out that the purpose behind that behavior is to keep you from passing in some insane value like 753 - which would cause unpredictable behavior or a crash (if you actually tried to use the StartDay value somehow) - so it's a good thing, not really a minute detail. This probably isn't the best example of the benefits of that kind of type safety, but you get the idea. ;)
0
 
mrjoltcolaCommented:
Right. And using enum's can simply coding using the codesense in Visual Studio / other environments for code completion, since it knows the possible values you may type in an expression.
0
 
tcalbazAuthor Commented:
Thank you very much for your help. It all checked out.  I'm starting to get used to C#'s intolerance of ambiguity.
0
 
tcalbazAuthor Commented:
Thank you very much for your help. It all checked out.  I'm starting to get used to C#'s intolerance of ambiguity.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now