2 Replies - 588 Views - Last Post: 27 March 2010 - 07:13 AM Rate Topic: -----

#1 Naelex  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 01-March 10

Calling a Validation Method

Posted 26 March 2010 - 10:33 AM

I created an app which takes in a date and displays it in the form '21st January, 2008',
The program works perfectly, however i need to validate the input date without using the built in c# functions.
Here is the current working code in the Main method:

while (true)
            {
                try
                {
                    string suffix = string.Empty;

                    Console.WriteLine("Please enter date...");
                    DateTime InputDate = DateTime.Parse(Console.ReadLine());

                    if (InputDate.Day.ToString().EndsWith("1"))
                    {
                        if (InputDate.Day.ToString().StartsWith("1") && InputDate.Day != 1)
                            suffix = "th";
                        else
                            suffix = "st";
                    }
                    else if (InputDate.Day.ToString().EndsWith("2"))
                    {
                        if (InputDate.Day.ToString().StartsWith("1"))
                            suffix = "th";
                        else
                            suffix = "nd";
                    }
                    else if (InputDate.Day.ToString().EndsWith("3"))
                    {
                        if (InputDate.Day.ToString().StartsWith("1"))
                            suffix = "th";
                        else
                            suffix = "rd";
                    }
                    else
                        suffix = "th";


                    string Output = InputDate.Day + suffix + " " + InputDate.ToString("MMMM") + ", " + InputDate.Year;
                    Console.WriteLine("\n" + Output + "\n");
                }

                catch
                {
                    Console.WriteLine("Please enter a correct date\n");
                }
           }



Here is the code to manually validate the input date:
public bool dateCheck(int iDay, int iMonth, int iYear)
        {
            if ((iYear % 4 == 0 && iYear % 100 != 0) || (iYear % 400 == 0))//leap year
            {
                if (iMonth == 2 && iDay > 29)
                {
                    return false;
                }
            }
            else if (iMonth == 2 && iDay > 28)
            {
                return false;
            }
            if ((iMonth = 4 || iMonth == 6 || iMonth == 9 || iMonth == 11) && iDay > 30)
            {
            return false;
            }
            return true;
        }



My problem is that i cannot get the method to work correctly (despite alot of attempts) so that it is called to validate the input date, instead of using try + catch.

Any help as to how to solve this would be greatly appreciated, Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Calling a Validation Method

#2 MentalFloss  Icon User is offline

  • "ADDICTED"[2:5]
  • member icon

Reputation: 526
  • View blog
  • Posts: 1,397
  • Joined: 02-September 09

Re: Calling a Validation Method

Posted 26 March 2010 - 11:04 AM

Quote

The program works perfectly, however i need to validate the input date without using the built in c# functions.


Why? Is it homework?
Was This Post Helpful? 0
  • +
  • -

#3 Naelex  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 01-March 10

Re: Calling a Validation Method

Posted 27 March 2010 - 07:13 AM

View PostMentalFloss, on 26 March 2010 - 10:04 AM, said:

Quote

The program works perfectly, however i need to validate the input date without using the built in c# functions.


Why? Is it homework?


Coursework yes, what's the problem? not asking anyone to do it for me, i just need some direction as to how to use a method - tried loads of online tutorials just having trouble getting to grips with how to use them correctly, or if it's even neccesary to do so for my needs.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1