for Iterations - Day 1 - Part 14

Sign in to queue

Description

When working with data or objects (such as controls) in applications, you often need to loop or, rather, iterate through groups. This video explains how to work with a simple “for iteration” statement. Since we'll be working with lots of different groups (such as collections of objects and collections of data) we’ll need to understand a little about how to navigate through the collection, or list, of those items.

Download the source code in c#

Download the source code in VB.Net

Embed

Download

Download this episode

The Discussion

  • User profile image
    james

    Hi Bob, great series of videos. just finished day 1 and coming back to this video because....
    i understand the theory of what the for loop is doing etc just fine. but this line i get confused...
    message = message + i.ToString() + System.Environment.NewLine;
    when i go through loop in my head i come to the conclusion you shouldnt need the "message +" part.. as in effect "message" has no value/string. yet when you remove it only "9" is displayed. also if you put "message = i.ToString() + message + System.Environment.NewLine;"  the NewLine is being initiated.
     
    if you could clear this up for me i would appreciate it! cheers 

  • User profile image
    james

    @james: sorry the .NewLine is NOT being initiated....

  • User profile image
    Clint

    @james: System.Environment.NewLine doesn't have to be initiated.

    He did create message and on every loop, he is recreating message with what was there, the loop counter, then a new line character.  Without the new line character there, it would look like this:
    56789

    instead of:
    5
    6
    7
    8
    9

  • User profile image
    Kristian

    @james:  you could also use message +=  i.ToString() + System.Environment.NewLine;
    This is the same as message = message + i.ToString() + System.Environment.NewLine;

  • User profile image
    Christian

    Yes I have to say overall the concept was clear but the lines of code were very unclear to me... More clarification would be better... for example
    when the variable is set to this string userValue = myTextBox.Text = "2";
    myResultReturned.Text = "You have won a million dollars";
    LOL, I know this is probably wrong but the point I am asking/illustrating is that why does the first line take a value in and why does the second line read a value out... how does the code know the difference of what it is displaying / running onto the screen versus taking in???

  • User profile image
    magicwin31

    I have no idea what I am doing wrong. This is my code: 

    private void myButton_Click(object sender, RoutedEventArgs e)

           {

    string message = "";

    for (int i = 0; 1 < 10; i++)

               {

                   message = message + i.ToString() + System.

    Environment.NewLine;

                }

                myTextBlock.Text = message;
    and for some reason it has a green line under myTextBlock and said there is unreachable code detected. What am I doing wrong?

  • User profile image
    shamelessto​ad

    @magicwin31: try to change from this  for (int i = 0; 1 < 10; i++) to this for (int i = 0; i < 10; i++)

  • User profile image
    magicwin31

    @shamelesstoad that worked, thank you so much.

  • User profile image
    Jorge

    @Christian, thats because the first line is used with a textBox, wich takes values "in", and the second line is used with a textBlock, wich takes values "out".

  • User profile image
    Jorge

    There's something unclear to me,
    Why does the next code just writes down "9"? I mean, shouldnt it start with "1", print it, goes for the "2", prints it and so? I tried this code as i've done it several times in Java and it does print the whole secuence instead of just "9", so why isnt this working here?
    privatevoid myButton_Click(object sender, RoutedEventArgs e)
            {
    for (int i = 0; i < 10; i++)
                {
                    mytxtBlock.Text = i.ToString() + System.Environment.NewLine;
                }
            }
     
     

  • User profile image
    Jorge

    EDIT: Actually it should start with "0" :P

  • User profile image
    Griffin Wiebel

    These videos are helping me not only learning the WP7 development process, but C# at the same time!

Add Your 2 Cents