0

I have a static dictionary that i create globally. I declare it like this =>

    static Dictionary<int, Artikels> dicArtikels = new Dictionary<int, Artikels>();

Artikels is a class i have created to store the products.

            for (int rijenTeller = 0; rijenTeller < aantalRijen; rijenTeller++)
            {

                Artikels artikel = new Artikels();

                if (dicArtikels.ContainsKey(rijenTeller))
                {
                    artikel = dicArtikels[rijenTeller];
                }
                else
                {
                    dicArtikels.Add(rijenTeller, artikel);
                }

                        artikel.naam = txtNameInstance.Text;
                        artikel.prijs = txtPriceInstance.Text;

                        dicArtikels[rijenTeller] = artikel;
                        artikel = null;
             }

Well when i print the output with a button like below it's always showing the value of the last instance. That't not what i want. I want the value of the seperate instances but they are always getting the same value. =>

    protected void Button3_Click(object sender, EventArgs e)
    {
        try
        {
            Artikels artikel = new Artikels();
            artikel = dicArtikels[0];
            Response.Write(artikel.naam.ToString() + " -- " + artikel.prijsExclBTW.ToString());

            artikel = dicArtikels[3];
            Response.Write(artikel.naam.ToString() + " -- " + artikel.prijsExclBTW.ToString());

            artikel = dicArtikels[1];
            Response.Write(artikel.naam.ToString() + " -- " + artikel.prijsExclBTW.ToString());

            artikel = dicArtikels[2];
            Response.Write(artikel.naam.ToString() + " -- " + artikel.prijsExclBTW.ToString());
        }
        catch (Exception ex)
        {
            Response.Write("Foutbericht Artikelchangedssssssss: " + ex.Message);
        }
    }
  • 13
    Why do you use a static dictionary in ASP.NET?All users will share the same values and you're getting concurrency issues and other interesting things. – Tim Schmelter Jan 30 '15 at 14:11
  • 1
    @TimSchmelter Right, he's up for many other "interesting things", none of which is "interesting" in a particularly good way :-) – Sergey Kalinichenko Jan 30 '15 at 14:15
  • 2
    All the names are being set to the same thing: artikel.naam = txtNameInstance.Text; Same with prijsExclBTW. – JBrooks Jan 30 '15 at 14:16
  • See [Are static class instances unique to a request or a server in ASP.NET?](http://stackoverflow.com/questions/194999/are-static-class-instances-unique-to-a-request-or-a-server-in-asp-net) – Alex K. Jan 30 '15 at 14:20
  • Well instead of a dictionary what would you use? – user3512855 Feb 01 '15 at 14:59

0 Answers0