Ok im struggling to write a very basic program, it is a program that is the user enters 28 it will write back the month is feb, if the user enters 30 it will show the months with 30 and the same for 31 and say no days if a number other than them is entered .

Any help my if statement aint working. :(

its just the if statement i need help on :(

Edited 5 Years Ago by c#learner: n/a

I have added a textBox and a button controls on the form. Into textBox you write the number of days in month and press the button:

private void button1_Click(object sender, EventArgs e)
        {
            int noOfDays = int.Parse(textBox1.Text);
            DateTime today = DateTime.Today;
            int tempDays;
            List<string> listOfMonths = new List<string>();
            for (int i = 1; i <= 12; i++)
            {
                tempDays = DateTime.DaysInMonth(today.Year, i);
                if (noOfDays == tempDays)
                    listOfMonths.Add(System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(i));
            }
            StringBuilder sb = new StringBuilder();
            foreach (string month in listOfMonths)
                sb.AppendLine(month);
            MessageBox.Show(String.Format("List of months that have {0} days:\n{1}", noOfDays, sb.ToString()));
        }

Thanks for your answer but its not what what the question is looking for, its just asking for an if statement simply if a nubmer( in this case either 28,29,30,31) is entered show this text, if another number is entered show this text and if one of those number isnt entered show text wrong number.

thanks for spending time making that though.

So why hesitate, use a case statement!

Because I am a beginner and have no idea what that is, Im struggling with this question, anyone know how to do it.

Here's a technique.
Let me know if it's too fancy :)

using System;
using System.Collections.Generic;
using System.Linq;

namespace DW_390972
{
   using KVP_S2I = KeyValuePair<string, int>;

   class Program
   {
      private static List<KVP_S2I> lst_kvpMonths = new List<KeyValuePair<string, int>>()
      {
         {new KVP_S2I ("January", 31)},
         {new KVP_S2I ("February", 28)},
         {new KVP_S2I ("March", 31)},
         {new KVP_S2I ("April", 30)},
         {new KVP_S2I ("May", 31)},
         {new KVP_S2I ("June", 30)},
         {new KVP_S2I ("July", 31)},
         {new KVP_S2I ("August", 31)},
         {new KVP_S2I ("September", 30)},
         {new KVP_S2I ("October", 31)},
         {new KVP_S2I ("November", 30)},
         {new KVP_S2I ("December", 31)}
      };

      static void Main(string[] args)
      {
         Console.Write("Enter a number: ");
         int iNum = 0;
         int.TryParse(Console.ReadLine().Trim(), out iNum);

         if (!lst_kvpMonths.Any(kvp => kvp.Value.Equals(iNum)))
         {
            Console.WriteLine("Invalid number");
            return;
         }
         //
         (
            from kvp in lst_kvpMonths
            where kvp.Value.Equals(iNum)
            select kvp
         ).ToList().ForEach(kvp => Console.WriteLine(kvp.Key));
      }
   }
}

c#learner, You mentioned your IF statement is not working. Can you post the statement or some code that's giving you trouble?
I'm not really expecting you to use this example, but I cannot assume how you've made your program.

Mitja, I really like the System.Globalization thing!!!
I modified my program to use it.

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

namespace DW_390972
{
   using KVP_S2I = KeyValuePair<string, int>;

   class Program
   {
      private static List<KVP_S2I> lst_kvpMonths = GetMonths();
      private static List<KVP_S2I> GetMonths()
      {
         DateTime today = DateTime.Today;
         List<KVP_S2I> lst = new List<KVP_S2I>();
         for (int i = 1; i <= 12; i++)
         {
            lst.Add(
               new KVP_S2I(
                  CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(i),
                  DateTime.DaysInMonth(today.Year, i)));
         }

         return lst;
      }

      static void Main(string[] args)
      {
         Console.Write("Enter a number: ");
         int iNum = 0;
         int.TryParse(Console.ReadLine().Trim(), out iNum);

         if (!lst_kvpMonths.Any(kvp => kvp.Value.Equals(iNum)))
         {
            Console.WriteLine("Invalid number");
            return;
         }
         //
         (
            from kvp in lst_kvpMonths
            where kvp.Value.Equals(iNum)
            select kvp
         ).ToList().ForEach(kvp => Console.WriteLine(kvp.Key));
      }
   }
}

Edited 5 Years Ago by thines01: clarity

You all have missed the key point in the original question.
The fact that the OP is a beginner!
Here is a basic program that demonstrates the use of the if else command.

static void Main(string[] args)
{
    Console.Write("Enter a number: ");
    int iNum = 0;
    // read a value entered on the keyboard and converts it to a number
    int.TryParse(Console.ReadLine().Trim(), out iNum);

    if (iNum == 28)
    {
        Console.WriteLine("Month is February");
    }
    else if (iNum == 30)
    {
        Console.WriteLine("Months are April ..."); // other month need to be added
    }
    else if (iNum == 31)
    {
        Console.WriteLine("Months are ..."); // add months
    }
    else
    {
        Console.WriteLine("No months have that many days.");
    }
    Console.Write("Press any key to continue...");
    Console.ReadKey();
}

Edited 5 Years Ago by nick.crane: n/a

Here is the same program again only using the switch command mentioned by ddanbe.
This is very similar to using the if - else if - else if of the previous program.

static void Main(string[] args)
{
    Console.Write("Enter a number: ");
    int iNum = 0;
    // read a value entered on the keyboard and converts it to a number
    int.TryParse(Console.ReadLine().Trim(), out iNum);

    switch (iNum)
    {
        case 28:
            Console.WriteLine("Month is February");
            break;
        case 30:
            Console.WriteLine("Months are April ..."); // other month need to be added
            break;
        case 31:
            Console.WriteLine("Months are ..."); // add months
            break;
        default:
            Console.WriteLine("No months have that many days.");
            break;
    }

    Console.Write("Press any key to continue...");
    Console.ReadKey();
}

Edited 5 Years Ago by nick.crane: n/a

This article has been dead for over six months. Start a new discussion instead.