Thread Status:
Not open for further replies.

How To C# - back to basics series

begallegal1 Feb 11, 2014

  1. begallegal1

    begallegal1 Medicine Man Lifetime Gold TeamXPG
    235/282

    Joined:
    Oct 31, 2011
    Messages:
    3,694
    Likes Received:
    3,131
    Trophy Points:
    235
    Gender:
    Male
    Location:
    In a Field of Green
    Console:
    Xbox
    Hello all, while taking a break from larger projects I decided to make a TUT series I will be going "back to basics" showing some of the most commonly used methods in C#, I know these type of tuts are all over youtube, and I am by no means an expert, but hopefully it can help out a few members here ;)

    Part 1 - if else statements

    https://www.youtube.com/watch?v=bLNkeEqFZV8

    Reference Code:
    Code:
    if (textBox1.Text == textBox2.Text)
    {
    resultLbl.Text = "text boxes match";
    }
    else
    {
    resultLbl.Text = "text boxes do not match";
    }
     
  2. begallegal1

    begallegal1 Medicine Man Lifetime Gold TeamXPG
    235/282

    Joined:
    Oct 31, 2011
    Messages:
    3,694
    Likes Received:
    3,131
    Trophy Points:
    235
    Gender:
    Male
    Location:
    In a Field of Green
    Console:
    Xbox
    Part 2 switch statements

    https://www.youtube.com/watch?v=R1mPzK5pFyw

    Reference code:
    Code:
    	int value = int.Parse(textBox1.Text);
                switch (value)
                {
                        case 1:
                        resultLbl.Text = "the number is " + 1;
                	    break;
                        case 2:
                        resultLbl.Text = "the number is " + 2;
                        break;
                        case 3:
                        resultLbl.Text = "the number is " + 3;
                        break;
                        default:
                        resultLbl.Text = "the number is not valid";
                        break;
                }
    
     
  3. begallegal1

    begallegal1 Medicine Man Lifetime Gold TeamXPG
    235/282

    Joined:
    Oct 31, 2011
    Messages:
    3,694
    Likes Received:
    3,131
    Trophy Points:
    235
    Gender:
    Male
    Location:
    In a Field of Green
    Console:
    Xbox
    Part 3 While Loops

    Here's a console example
    [​IMG]


    static void Main(string[] args)
    {
    int x = 0;// our integer variable

    while (x < 16)//we are going to "loop" 16 times so our results show 0-15
    {
    Console.WriteLine(x);//write our number each time we loop

    x++;//add 1 to our variable each time we loop so we stop correctly

    }

    }



    ******************************************************************************************************************************
    Here's a form example
    [​IMG]


    private void button1_Click(object sender, EventArgs e)
    {
    int myNum = 0;// this time we will use an integer variable

    while (myNum < 16)//we are going to "loop" 16 times so our results show 0-15
    {
    //write our number with a statement to our text box each time we loop
    textBox1.AppendText("Current Loop Count: " +myNum+"\n");
    //add 1 to our variable each time we loop so we stop correctly
    myNum++;
    }
    }

    *******************************************************************************************************************************
    now here is an extra long way of accomplishing the exact same result as our first console version above, but shows how to use a "bool" as a switch to stop whatever code you have running inside the loop, here we run a loop inside a loop :LOL:
    Code:
    static void Main(string[] args)
            {
                bool x = false;// Our bool will be used to turn the loop on and off
                int myNum = 0;//variable to use in the 2nd while loop
    
                while (x == false)//while our bool is false
                {
                    //here your program could run anything for any amount of time you choose
    
                    while(myNum < 16)//we are going to "loop" 16 times so our results show 0-15
                    {
                        Console.WriteLine(myNum);//write our number each time we loop
    
                        myNum++;//add 1 to our variable each time we loop so we stop correctly
                    }
                    x = true; //turn our bool true to stop our main loop  
                }
            }
    
     
  4. begallegal1

    begallegal1 Medicine Man Lifetime Gold TeamXPG
    235/282

    Joined:
    Oct 31, 2011
    Messages:
    3,694
    Likes Received:
    3,131
    Trophy Points:
    235
    Gender:
    Male
    Location:
    In a Field of Green
    Console:
    Xbox
    Part 4 For Loops



    A "for" loop is almost like the "while" loop we learned about above so here we will write the same program using a for loop instead (results will be same as pics in above post)

    Here's a console example
    Code:
    static void Main(string[] args)
            {        
                int myNum = 0;//variable to use while counting up our loop
    
                //for loops look intimidating but here's how they break down 
    
     //        our variable i ; if it is less than 16 ; add 1 to our variable
                for (int i = 0; i < 16; i++)
                {
                    Console.WriteLine(myNum);//print our number
                    myNum++;//add one to our number
                }
                Console.Read();
            }
    **************************************************************************
    Here's a form example

    Code:
    private void button1_Click(object sender, EventArgs e)
            {
                int myNum = 0;// our integer variable
    
          // our variable i ; if it is less than 16 ; add 1 to our variable
                for (int i = 0; i < 16; i++)
                {
                    //write our number with a statement to our text box each time we loop 
                    textBox1.AppendText("Current Loop Count: " +myNum+"\n");
                    //add 1 to our variable each time we loop 
                    myNum++;
                }   
            }
     
Thread Status:
Not open for further replies.

Share This Page

Close