1

Although there are several posts on Stackoverflow about this kind of issue, then I have looked through them and have not found a solution for my problem.

In the code, I am going going through a list of lists with a foreach loop and adding created elements to another list. Although in the foreach loop each iteration gives an unique value, then outside of it the values are the same.

try
{
    List<Takeoff> takeoffs = new List<Takeoff>();

    List<List<String>> itemTable = queryTable("TK_ITEM", 52);

    foreach (List<String> row in itemTable)
    {
        // Second element in the constructor is Name.
        Takeoff takeoff = new Takeoff(row.ElementAt(0), row.ElementAt(3), row.ElementAt(11), 
            row.ElementAt(17), row.ElementAt(25), row.ElementAt(33), 
            row.ElementAt(37), row.ElementAt(45));

        MessageBox.Show(row.ElementAt(3)); // Each iteration gives an unique value.

        takeoffs.Add(takeoff);
    }

    // Values of both objects are the same.
    MessageBox.Show(takeoffs[0].Name);
    MessageBox.Show(takeoffs[1].Name);

    return takeoffs;
}
catch (Exception)
{
    MessageBox.Show("No material takeoff created!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);

    return null;
}

I have tried various methods of adding and displaying the values, but so far I have not found a solution which would work.

Could anyone point out to me where the problem is?

Edit: Declaration of takeoff

/*...*/
private static string name;
/*...*/

public Takeoff(string id, string name, string guid, string width, string height, string area, string volume, string count)
{
    /*...*/
    Name = name;
    /*...*/
}

/*...*/

public string Name
{
    get { return name; }
    set { name = value; }
}

/*...*/
7
  • 3
    Can you show the type Takeoff, including its Name property or field and its constructor? Commented Dec 21, 2015 at 13:52
  • Can you show Takeoff ctor? Commented Dec 21, 2015 at 13:52
  • 1
    As a side-note, consider reducing the number of parameters needed to pass to the TakeOff constructor. Maybe use property initialisation e.g. var takeOff = new TakeOff { Name = "Blah" }; Having to pass 8 parameters is a major maintenance headache waiting to happen. Commented Dec 21, 2015 at 13:56
  • For sanity, instead of doing MessageBox.Show(row.ElementAt(3)); in the loop why not display takeoff.Name? If you "know" row.ElementAt(3) is correct then you need to check if the construction of takeoff is. Commented Dec 21, 2015 at 13:58
  • 2
    As I suspected you have a static backing field. Static = 1 per type, not 1 per instance. Commented Dec 21, 2015 at 13:58

1 Answer 1

8

Your name backing field is static:

private static string name;

Don't do that. Just remove the static modifier, it's not necessary.

Static members belong to the type, rather than the instance. This means all instances of Takeoff share the same value of name, whichever value was assigned last.

Sign up to request clarification or add additional context in comments.

5 Comments

And I am one thousand percent certain this has at least one exact duplicate, but I couldn't find one with this specific problem.
Thank you, did not even notice that. Now that you pointed it out, it is obvious.
@CodeCaster, it's off-topic as it's a trivial typo. Just needs closing for that reason. No need to hunt for duplicates.
@David adding a modifier is not a typo. It is a fundamental misunderstanding of what static does, and warrants explanation.
@CodeCaster, it's unlikely to be of any help to anyone but the OP though. As per "While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers", it's justifed in closing it as a simple typo.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.