List of objects
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: List of objects

Hybrid View

  1. #1
    Join Date
    Jul 2009
    Posts
    3

    Angry List of objects

    Well, I'm trying to create a app using Visual Studio 8. I'm new in the c# language...

    The problem is that when I add items to a List of objects, and in the end when a do a foreach to see if everything is fine, the result is allways the same... Can anyone help me finding the "bug"?
    Here is some code:


    /**********************************/

    List<Option> options = new List<Option>();

    number=1;


    do
    {



    Option option = new Option(number);



    options.Add(option);

    number++;

    } while (number != 5);

    foreach (Option o in options)
    {

    result += "\n" + o.number;
    }
    MessageBox.Show(result);

    /*****************************/

    This results in a MessageBox with:

    4
    4
    4
    4

  2. #2
    Join Date
    May 2007
    Posts
    437

    Re: List of objects

    Quote Originally Posted by licantropo View Post
    Well, I'm trying to create a app using Visual Studio 8. I'm new in the c# language...

    The problem is that when I add items to a List of objects, and in the end when a do a foreach to see if everything is fine, the result is allways the same... Can anyone help me finding the "bug"?
    Here is some code:


    /**********************************/

    List<Option> options = new List<Option>();

    number=1;


    do
    {



    Option option = new Option(number);



    options.Add(option);

    number++;

    } while (number != 5);

    foreach (Option o in options)
    {

    result += "\n" + o.number;
    }
    MessageBox.Show(result);

    /*****************************/

    This results in a MessageBox with:

    4
    4
    4
    4
    the code si working except defining variable and classes.

    Code:
            struct Option
            {
                public int number;
                public Option(int num)
                {
                    number = num;
                }
            };
            private void button1_Click(object sender, EventArgs e)
            {
                List<Option> options = new List<Option>();
                string result = string.Empty;
                int number = 1;
                do
                {
                    Option option = new Option(number);
                    options.Add(option);
                    number++;
    
                } while (number != 5);
                foreach (Option o in options)
                {
                    result += "\n" + o.number;
                }
    
                MessageBox.Show(result);
            }
    i am receiving the message box with following content.
    1
    2
    3
    4
    Last edited by ashukasama; July 1st, 2009 at 09:37 PM. Reason: changed class to struct
    ashu
    always use code tag

  3. #3
    Join Date
    Jul 2009
    Posts
    3

    Re: List of objects

    Actually, this is list of objects, defined by the class :

    /***********/
    class Option
    {

    public int number;

    public Option(int Num)
    {

    this.number= Num;

    }
    /*************/

    List<Option> options = new List<Option>();

    number=1;


    do
    {



    Option option = new Option(number);



    options.Add(option);

    number++;

    } while (number != 5);

    foreach (Option o in options)
    {

    result += "\n" + int.Parse(o.number);
    }
    MessageBox.Show(result);

    /***********************/

  4. #4
    Join Date
    May 2007
    Posts
    437

    Re: List of objects

    Quote Originally Posted by licantropo View Post
    Actually, this is list of objects, defined by the class :

    /***********/
    class Option
    {

    public int number;

    public Option(int Num)
    {

    this.number= Num;

    }
    /*************/

    List<Option> options = new List<Option>();

    number=1;


    do
    {



    Option option = new Option(number);



    options.Add(option);

    number++;

    } while (number != 5);

    foreach (Option o in options)
    {

    result += "\n" + int.Parse(o.number);
    }
    MessageBox.Show(result);

    /***********************/
    if you use int.Parse you will notice following error.

    Error 1 The best overloaded method match for 'int.Parse(string)' has some invalid arguments
    Error 2 Argument '1': cannot convert from 'int' to 'string'

    if you change o.number to string then there will be lot of changes in code.... so please be sure before posting that your code is working...
    ashu
    always use code tag

  5. #5
    Join Date
    Jul 2009
    Posts
    3

    Re: List of objects

    I really don't understand... In my original app this code does not work...
    But when a do a test using a new project, works just fine... with no errors and warnings.
    I suppose the problem is somewhere else...

    Has i have already said, Iím a newbie in c#. And in Lists also.

    I'm sorry by the inconvenience.

    thanks

  6. #6
    Join Date
    May 2007
    Posts
    437

    Re: List of objects

    use the code i provided.. also please post your original application code
    ashu
    always use code tag

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center