views:

269

answers:

2

hi, i have a static dicitionary in my class which hold 12000 values. when i try to save my class i need to refresh the static dictionary, i mean i need to add some data in Static Dicitionary at server side. the problem is that after adding the values into Static Dicitionary ,it still retains 12000 values, not 12001(the last one doesn't get added). it is not able to serialize and deserialize the static member. i think, as static member are not part of the object ,so it doesn't get seralized. i can implement ISerializable interface and add the last member. but i think it's not a good idea. is there a better way to do tht. i m working on c# windows application.

A: 

What behaviour would you expect if you sent data from several different clients to the server?

Suppose client A had added items X and Y, and client B had added items Y and Z. I'm guessing that you'd want the static dictionazry to end up with item X, Y and Z, but not two Ys.

I think you will need to special code in your ISerializable implementation, and I think that's quite legitimate.

I would have an extra non-static member list variable called something like "myDictionaryAdditions" when ever I add to static dictionary I would add to this list. Presumably this will get correctly trasnfered to the server. Now you just need some code in teh de-serializer to transfer non-dups to the static dictionary.

djna
+1  A: 

You may serialize. Here is a code,

[Serializable ]
public class Numbers
{
    public int no;
    public static int no1;
}
class Test
{
    static void Deser()
    {
        Numbers a;
        FileStream fs = new FileStream("a1.txt", FileMode.Open );
        System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bs = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
        a = (Numbers)bs.Deserialize(fs);
        Numbers.no1 = (int)bs.Deserialize(fs);
        fs.Close();
        Console.WriteLine(a.no + " " + Numbers.no1);
    }
    static void Ser()
    {
        Numbers a = new Numbers();
        a.no = 100;
        Numbers.no1 = 200;

        FileStream fs = new FileStream("a1.txt", FileMode.Create);
        System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bs = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
        bs.Serialize(fs, a);
        bs.Serialize(fs, Numbers.no1);
        fs.Close();
   }
}
adatapost