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

C#

 
QuestionUsing nonce for authentication Pin
TygerBS24-Mar-14 2:21
TygerBS24-Mar-14 2:21 
AnswerRe: Using nonce for authentication Pin
Pete O'Hanlon24-Mar-14 3:17
mvePete O'Hanlon24-Mar-14 3:17 
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 
Many times in C/C++ I have used arrays of structs into struct and it was very nice and useful to aggregate data sets. Now with C# My question is, can I make the same using class ? I know, there are struct also in C#, but they are allocated into stack and I don't want use to much stack area, and I have two method to serialize and deserialize not static objects classes and I don't know if and how struct are serializable.
Here is my code:

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

namespace My_Space
{
    // 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 class preProcessing
            {
                public string preProcessingName;        // preprcessing name
                public double[] param = new double[5];  // 5 parameters from preprocessing
            }
        }

        // init object class Parameters
        public void InitParameters()
        {
            autoLockTime = 0;
            screenOffTime = 0;
            loopAutoStartTime = 0;
            product[] p = new product[30];
            p[0].enabled = false;
            p[0].unit = "mm";
            p[0].productName = "H2O";

            product.preProcessing[] pp = new product.preProcessing[10];
            pp[0].preProcessingName = "Derivative";
            pp[0].param[0] = 100.0f;
            pp[0].param[1] = 200.0f;
// BUT I CANNOT LINK THE 10 PRE-PROCESSING ARRAY TO
// THE 30 PRODUCTs AND WRITE something like with struct
// p[3].pp[2].param[4] = 123.45f;
        }

        //-----------------------------------------------------------------
        // 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();
        }
    }
}


I can also write in other classes:

C#
private Parameters q = new Parameters q();
private Parameters.product[] p = new Parameters.product[30];
private Parameters.product.preProcessing[] pr = new Parameters.product.preProcessing[10];

public Form()
{
    // Return the output object in the same input object !!!!!
    q.ReadParameters(out q);

    p[0].productName = "aaa";
    pr[0].preProcessingName = "bbb";

}

but I cannot link the preProcessing object to the product object and I cannot serialize array of classes as p or pr.

Maybe using static class ... ?
How can I serialize/deserialize structs ?
What is the best solution ?
Thanks

modified 24-Mar-14 12:21pm.

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 
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 

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.