[SOLVED]Problem with EWR

Hi everyone

Trying to store data settings, I am experiencing some problems. I think it is more C# trouble that GHI related stuff but anyway here is what I want to do:

Settings in my application is a set of data (a class that I called DATASET) stored as an hashtable(key,dataset_element).

My class is:


[Serializable]
    class DATASET
    {
        public static Hashtable datatable = new Hashtable();
        public DATASET()
        {
            //datatable = new Hashtable();
        }
        public void add_data(int key,DataSetEl my_object)
        {
            datatable.Add(key,my_object);
        }
        public void show_all_table()
        {

            foreach(DictionaryEntry de in datatable)
            {
                Debug.Print("Key=" + de.Key + "\t Value=" + de.Value.ToString());
            }
        }
    }

and here is the class for DataSetElement:

[Serializable]
    class DataSetEl
    {
        string Var;
        string Name;
        string Source;
        string Param1;
        string Param2;
        string Param3;
        string offset;
        string amortissement;
        int nb_decimale;
        bool sign; //0=negatif, 1 positif
        int nb_digit;


        //constructeur par défaut
        public DataSetEl(string my_var,string my_name,string my_source,string my_Param1,string my_Param2,string my_Param3,string my_offset,string my_amortissement,int my_nb_dec,bool my_sign,int my_nb_digit)
        {
            Var = my_var;
            Name = my_name;
            Source = my_source;
            Param1 = my_Param1;
            Param2 = my_Param2;
            Param3 = my_Param3;
            offset = my_offset;
            amortissement = my_amortissement;
            nb_decimale = my_nb_dec;
            sign = my_sign;
            nb_digit = my_nb_digit;
        }
        public override string ToString()
        {
        return (Var+"\t"+Name+"\t"+Source+"\t"+Param1+"\t"+Param2+"\t"+Param3+"\t"+offset+"\t"+amortissement+"\t"+nb_decimale+"\t"+sign+"\t"+nb_digit);
        }
    }

my main function runs:


settingsReference= ExtendedWeakReference.RecoverOrCreate(typeof(DATASET),0,ExtendedWeakReference.c_SurvivePowerdown);
// If the entry was just created, its value is intitated with Touche1
            if(settingsReference.Target == null)
            {
                Datasets = new DATASET();
                DataSetEl afirstdatasetel = new DataSetEl("Touche1","Key1","System","Touche","","","0","",5,false,5);
                Datasets.add_data(1,afirstdatasetel);
            }
            else 
            {
                Datasets = (DATASET) settingsReference.Target;
            }
            Datasets.show_all_table();
  
            settingsReference.Target =Datasets;
            GHIElectronics.NETMF.System.Util.FlushExtendedWeakReferences();

On the first run data are properly printed out, which seems that Datasets is properly initialized.
On the second run, I am expecting the same print out but nothing append and the hashtable is empty (_count=0)

Do you have any idea about how to solve this (EWR + hashtable(key,object)) ?

Why is the datatable static?

you’re right, there’s no reason… I will check that right now and will let you know what’s happening…

Test done without success…

[quote]
The error is:
#### Exception System.NullReferenceException - CLR_E_NULL_REFERENCE (1) ####
#### Message:
#### USU.DATASET::show_all_table [IP: 0009] ####
#### USU.Program::Main [IP: 0077] ####
Une exception de première chance de type ‘System.NullReferenceException’ s’est produite dans USU.exe
Une exception non gérée du type ‘System.NullReferenceException’ s’est produite dans USU.exe[/quote]

The debugger shows that datatable is set to null…

You should also uncomment the initialization line in the constructor

 //datatable = new Hashtable();

Hello Godfather.

So I have to remove

= new Hashtable();

from

public Hashtable datatable = new Hashtable();

.

Am I right?

No, wait. I haven’t seen that. It should be fine.
What line in the main method throws the Exception?

The line that show exceptions is in the show_all_table method in the Dataset class:

foreach(DictionaryEntry de in datatable)

In fact datatable is null which crash the code… But I do not understand why datatable is null

I am not sure about static data serialization. Change it to non-static and see if it fixes the problem.

Hello architect

datatable is non static since the first post of GodFather… but this still does not work.

Also I do not understand why settingsReference and Settings are declared as static in
http://informatix.miloush.net/microframework/FAQ.aspx?Core%2FExtendedWeakReference

In fact I wonder if Hashtable is easily serializable… To be honnest I think it approaches my C# skill limit…

You are right Hashtable is not serializable. Thinking about it, Dictionary<K,V> from big .NET was not serializable either.
You should store those values in some other collection type.

This is quite confusing because in the “big” .net framework, Hashtable is described as:


[SerializableAttribute]
[ComVisibleAttribute(true)]
public class Hashtable : IDictionary, ICollection, 
	IEnumerable, ISerializable, IDeserializationCallback, ICloneable

This seems therefore serializable but I afraid to be obliged to define my owns serialization scheme. Am I right?

Ok after verification in .netmf documentation:

public class Hashtable : ICloneable, IDictionary, ICollection, IEnumerable

Therefore hashtable is definitely not serializable on our system…

Any idea of collection that can grow up on demand and that can be serializable?

Am I wrong if I said that there’s only arraylist that can suit my needs?

Check this article:

http://www.iamraf.net/Articles/Creating-a-GUI-Application-with-the-NET-Micro-Framework

I read the article but something sounds strange to me. It says that SwimTimingCollection can be serialized but authors have written

[NonSerialized]
SwimTimingCollection _List;

This sounds to me that the collection won’t be serialized…

Actually I think that in its code, author serialize simple type:

int _Number;
DateTime _TimeStart;
TimeSpan _Elapsed;

and do not want to serialize the reference to the collection that is also a member of the SwimTiming class.

Do you agree with me?

Actually, I have used ArrayList and this works… but as usual people wants to have an elegant way.

Why don’t you use an ArrayList or an array (T[])?

In my last message I said:

I use ArrayList and it works great, howver the key of hashtable avoid duplicated elements, this feature is important in my case and therefore if there’s an non-complex way to serialize the hashtable I woould be interested :slight_smile:

You can use two arraylists (one for keys and one for values) to write the entries from the hashtable in before serialization and restore them to the hashtable after deserialization.