We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

User Date function using Overloading not working

tcalbaz
tcalbaz asked
on
Medium Priority
265 Views
Last Modified: 2013-12-16
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

Comment
Watch Question

Top Expert 2009
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
Todd GerbertSenior Engineer
CERTIFIED EXPERT
Top Expert 2010

Commented:
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. ;)
Top Expert 2009

Commented:
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.

Author

Commented:
Thank you very much for your help. It all checked out.  I'm starting to get used to C#'s intolerance of ambiguity.

Author

Commented:
Thank you very much for your help. It all checked out.  I'm starting to get used to C#'s intolerance of ambiguity.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.