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

C#

 
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 
Sorry, my application doesn't serialize/deserialize. How can I save all my classes together in the same file ? With only one simple class (Parameters) it was correct, now with more classes I don't know how to fix the code. The code to test the serailize and deserialize method now is:

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

        public MainForm()
        {
//          param.ReadParameters(out param);
            param.InitParameters();
            p[0] = new Parameters.product();
            p[0].productName = "H2O";
            p[0].preProcessId[0] = 0;
            p[0].preProcessId[1] = 1;
            param.SaveParameters(p[0]);  // <<=  COMPILER ERROR
            param.ReadParameters(out p); // <<=  COMPILER ERROR

            pp[0] = new Parameters.preProcessing();
            pp[0].preProcessId = 0;
            pp[0].preProcessingName = "Derivative";
            pp[0].preProcessingParms[0] = 10.0;
            pp[0].preProcessingParms[1] = 20.0;
       }


and the file with the classes to save and the serialize/deserialize methods is:

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

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

        [Serializable()]
        public class product
        {
            public bool enabled;
            public string productName;
            public double zero;
            public double span;
            public string unit;
            public int[] preProcessId = new int[10];
        }
        [Serializable()]
        public class preProcessing
        {
            public int preProcessId;          // a number from 0 to 9
            public string preProcessingName;
            public double[] preProcessingParms = new double[5];  // max 5 parameters x preProcessing
        }

        // init object class Parameters
        public void InitParameters()
        {
            autoLockTime = 0;
            screenOffTime = 0;
            loopAutoStartTime = 0;

            preProcessing[] pp = new preProcessing[10];
            for (int i = 0; i < 9; i++)
            {
                pp[i] = new preProcessing();
                pp[i].preProcessId = i;
                pp[i].preProcessingName = "";
                for (int j = 0; j < 5; j++) pp[i].preProcessingParms[j] = 0.0;
            }

            product[] p = new product[30];
            for (int i = 0; i < 30; i++)
            {
                p[i] = new product();
                p[i].enabled = false;
                p[i].unit = "mm";
                p[i].productName = "H2O";
                p[i].zero = 0.0;
                p[i].span = 0.0;
                for (int j = 0; j < 10; j++)                    // we can have at max 10 preProcessing for product
                {
                    p[i].preProcessId[j] = 0;
                }
            }
        }

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

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 
AnswerRe: moving from iphone to asp.net Pin
Vishal Pand3y21-Mar-14 0:06
Vishal Pand3y21-Mar-14 0:06 
AnswerRe: moving from iphone to asp.net Pin
OriginalGriff21-Mar-14 2:16
mveOriginalGriff21-Mar-14 2:16 
QuestionMany instances of the same class by a loop Pin
mrRsChief20-Mar-14 15:32
mrRsChief20-Mar-14 15:32 
AnswerRe: Many instances of the same class by a loop Pin
Dave Kreskowiak20-Mar-14 17:06
mveDave Kreskowiak20-Mar-14 17:06 
AnswerRe: Many instances of the same class by a loop Pin
Wayne Gaylard20-Mar-14 20:03
professionalWayne Gaylard20-Mar-14 20:03 
AnswerRe: Many instances of the same class by a loop Pin
V.20-Mar-14 20:42
professionalV.20-Mar-14 20:42 
AnswerRe: Many instances of the same class by a loop Pin
OriginalGriff20-Mar-14 23:27
mveOriginalGriff20-Mar-14 23:27 

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.