Click here to Skip to main content
15,887,027 members
Home / Discussions / C#
   

C#

 
QuestionDeveloping a C# Project Pin
Bobba Praneeth24-Mar-14 0:09
Bobba Praneeth24-Mar-14 0:09 
AnswerRe: Developing a C# Project Pin
Keith Barrow24-Mar-14 1:23
professionalKeith Barrow24-Mar-14 1:23 
AnswerRe: Developing a C# Project Pin
V.24-Mar-14 3:49
professionalV.24-Mar-14 3:49 
AnswerRe: Developing a C# Project Pin
BobJanova24-Mar-14 3:58
BobJanova24-Mar-14 3:58 
AnswerRe: Developing a C# Project Pin
OriginalGriff24-Mar-14 6:23
mveOriginalGriff24-Mar-14 6:23 
Question[solved] Array of classes nested into array of classes (and serialization) Pin
Mario 5623-Mar-14 22:23
Mario 5623-Mar-14 22:23 
AnswerRe: Array of classes nested into array of classes (and serialization) Pin
Pete O'Hanlon23-Mar-14 22:48
mvePete O'Hanlon23-Mar-14 22:48 
GeneralRe: Array of classes nested into array of classes (and serialization) Pin
Mario 5623-Mar-14 23:43
Mario 5623-Mar-14 23:43 
This code seems to work:

C#
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

namespace Base_NIR
{
    // the object that needs to be serialized.
    [Serializable()]
    public class Parameters
    {
        public int autoLockTime;
        public int screenOffTime;
        public int loopAutoStartTime;

        public class product
        {
            public bool enabled;
            public string productName;
            public double zero;
            public double span;
            public string unit;
            public preProcessing[] preProc = new preProcessing[10];
        }
        public class preProcessing
        {
            public string preProcessingName;        // preprcessing name
            public double[] param = new double[5];  // 5 parameters from preprocessing
        }

        [NonSerialized()]
        public static string member5 = "hello world!";  // A field that is not serialized.

        // init object class Parameters
        public void InitParameters()
        {
            autoLockTime = 0;
            screenOffTime = 0;
            loopAutoStartTime = 0;
            preProcessing[] pr = new preProcessing[10];
            pr[0].preProcessingName = "bbb";
            product[] p = new product[30];
            p[0].preProc[1].preProcessingName = "aaa";
            p[0].enabled = false;
            p[0].unit = "mm";
            p[0].productName = "H2O";
            p[0].preProc[3].param[5] = 123.45f;
            p[0].preProc[2].preProcessingName = "Derivative";
        }

        //-----------------------------------------------------------------
        // Opens a file and serializes the object into it in binary format.
        // Let the input object unchanged.
        //-----------------------------------------------------------------
        public void SaveParameters(Parameters obj)
        {
            Stream stream = File.Open("Parameters.bin", FileMode.Create);
            BinaryFormatter formatter = new BinaryFormatter();

            formatter.Serialize(stream, obj);
            stream.Close();
        }

        //-----------------------------------------------------------------
        // Opens file "Parameters.bin" and deserializes the object from it.
        // Return the output object in the same input object !!!!!
        //-----------------------------------------------------------------
        public void ReadParameters(out Parameters obj)
        {
            Stream stream = File.Open("Parameters.bin", FileMode.Open);
            BinaryFormatter formatter = new BinaryFormatter();

            obj = (Parameters)formatter.Deserialize(stream);
            stream.Close();
        }
    }
}


And I can also write:
private Parameters.product[] p = new Parameters.product[30];
private Parameters pr = new Parameters();

public MainForm()
{
    pr.InitParameters();
    pr.ReadParameters(out pr);
    p[0].preProc[2].preProcessingName = "xxx";
    pr.SaveParameters(pr);
    // Return the output object in the same input object !!!!!
    pr.ReadParameters(out pr);
}



But everytime I write in a field I have the error of nullReference exception.
GeneralRe: Array of classes nested into array of classes (and serialization) Pin
Pete O'Hanlon24-Mar-14 2:23
mvePete O'Hanlon24-Mar-14 2:23 
GeneralRe: Array of classes nested into array of classes (and serialization) Pin
Mario 5624-Mar-14 2:42
Mario 5624-Mar-14 2:42 
GeneralRe: Array of classes nested into array of classes (and serialization) Pin
Pete O'Hanlon24-Mar-14 2:48
mvePete O'Hanlon24-Mar-14 2:48 
GeneralRe: Array of classes nested into array of classes (and serialization) Pin
Mario 5624-Mar-14 5:54
Mario 5624-Mar-14 5:54 
AnswerRe: Array of classes nested into array of classes (and serialization) Pin
BobJanova24-Mar-14 4:14
BobJanova24-Mar-14 4:14 
GeneralRe: Array of classes nested into array of classes (and serialization) Pin
Mario 5624-Mar-14 6:17
Mario 5624-Mar-14 6:17 
GeneralRe: Array of classes nested into array of classes (and serialization) Pin
Mario 5624-Mar-14 7:44
Mario 5624-Mar-14 7:44 
GeneralRe: Array of classes nested into array of classes (and serialization) Pin
BobJanova24-Mar-14 8:05
BobJanova24-Mar-14 8:05 
GeneralRe: Array of classes nested into array of classes (and serialization) Pin
Mario 5624-Mar-14 10:24
Mario 5624-Mar-14 10:24 
QuestionRegarding Role based Login Pin
Member 1067835223-Mar-14 22:08
Member 1067835223-Mar-14 22:08 
SuggestionRe: Regarding Role based Login Pin
Richard MacCutchan23-Mar-14 23:01
mveRichard MacCutchan23-Mar-14 23:01 
GeneralRe: Regarding Role based Login Pin
Member 1067835224-Mar-14 18:28
Member 1067835224-Mar-14 18:28 
GeneralRe: Regarding Role based Login Pin
Richard MacCutchan24-Mar-14 22:23
mveRichard MacCutchan24-Mar-14 22:23 
QuestionGeolocation with c# winforms Pin
Member 1069141122-Mar-14 12:37
Member 1069141122-Mar-14 12:37 
AnswerRe: Geolocation with c# winforms Pin
Peter Leow22-Mar-14 17:05
professionalPeter Leow22-Mar-14 17:05 
Questiondynamic sql result not shown in DevExpress DataGrid Pin
Jassim Rahma21-Mar-14 12:26
Jassim Rahma21-Mar-14 12:26 
Questionmoving from iphone to asp.net Pin
dsp120-Mar-14 23:41
dsp120-Mar-14 23:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.