Why is it always returning the last element in list<>?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Abdel
    New Member
    • Jan 2011
    • 1

    Why is it always returning the last element in list<>?

    Hi everybody

    when I use a for loop or foreach on list<> that has a number of objects it always return the last object

    for example:

    Code:
    for (int i = 0 ; i < Program.session.length ; i++)
    {
     int id = Program.sessions[i].Session_ID
    }
    where Session_ID is a property in a class session
    and the list<session> is declared staticaly in Program class

    what happen is Session_ID is always set to 4 (I have 4 objects)

    please help
    thanks in advance
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Let's simplify
    Code:
    for (int i = 0 ; i = 3; i++) 
    { 
     id = i
    }
    What will the value of id be?

    Comment

    • Curtis Rutland
      Recognized Expert Specialist
      • Apr 2008
      • 3264

      #3
      This is a good learning opportunity for you, OP. What is it that you actually want the value of id to be?

      Comment

      Working...