Click here to Skip to main content
13,046,375 members (113,226 online)

I have a C# code problem with an if and switch statement

jdsad2008 asked:

Open original thread
I am going to write a program that will tell the user the number of days in a individual month. The program should allow the user to enter a whole number representing the month (Jan = 1, Feb = 2, etc.). After checking that this is a valid month number (i.e., between 1 and 12), program should then determine the number of days in that month two different times: once using a series of if statements and once using a switch statement.
Place the resulting number of days in two different output Labels.
NOTE: Assume that February always has 28 days.


private void button1_Click(object sender, EventArgs e)
           int monthNo = 0;
           string[] month = new string[13];
           month[0] = "\0";
           month[1] = "January"; month[2] = "February"; month[3] = "March";
           month[4] = "April"; month[5] = "May"; month[6] = "June";
           month[7] = "July"; month[8] = "August"; month[9] = "September";
           month[10] = "October"; month[11] = "November"; month[12] = "December";

               monthNo = int.Parse(monthNoText.Text);
               if (monthNo <= 0 || monthNo > 12)
                   MessageBox.Show("Invalid Input!!",
                       "Must be 1 to 12!!");
                   switch (monthNo)
                       case 1:
                       case 3:
                       case 5:
                       case 7:
                       case 8:
                       case 10:
                       case 12:
                           switchText.Text = "31 days";
                       case 4:
                       case 6:
                       case 9:
                       case 11:
                           switchText.Text = "30 days";
                       case 2:
                           switchText.Text = "28 days";

this is what i got but i need the same info in stch label and if label
Tags: C#


When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the The Code Project Open License (CPOL).

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 26 Mar 2009
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100