Click here to Skip to main content
16,013,921 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more: , +
My question is how do i serialize the values in my static list to a .dat file given the sample of my classes below. Please reply with example codes thanks


C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace JustMe
{
    [Serializable]
   public class FITS
    {
        
        static List<Fitness> fitness;
        
        static FITS()
        {
            fitness = new List<Fitness>();
        }
        
        public void  AddFitness(Fitness addF)
        {
            
            for (int i = 0; i < fitness.Count; i++)
            {
                if (fitness[i].Id.Equals(addF.Id) == true)
                {
                    ExceptionHandler error = new ExceptionHandler(addF.Id);
                    throw (error);
                }

            }
          
            fitness.Add(addF);

        }
        
        public  void DeleteFitness(Fitness deleteF)
        {
            fitness.Remove(deleteF);
        }

        
        public  void SelectSort()
        {
            
            int smallest;

            for (int i = 0; i < fitness.Count - 1; i++)
            {
                smallest = i;
                
                for (int index = i + 1; index < fitness.Count; index++)
                {

                    if (fitness[index].Id.CompareTo(fitness[smallest].Id) < 0)
                    {
                        smallest = index;
                    }
                }
                
                var temp = fitness[i];
                fitness[i] = fitness[smallest];
                fitness[smallest] = temp;

            }

        }
        
        public  void SelectSort1()
        {
            
            int smallest1;

            for (int i = 0; i < fitness.Count - 1; i++)
            {
                smallest1 = i;
                
                for (int index1 = i + 1; index1 < fitness.Count; index1++)
                {

                    if (fitness[smallest1].Id.CompareTo(fitness[index1].Id) < 0)
                    {
                        smallest1 = index1;
                    }
                }
                
                var temp1 = fitness[i];
                fitness[i] = fitness[smallest1];
                fitness[smallest1] = temp1;

            }

        }

        public  void clear()
        {
            fitness.Clear();
        }

        public Fitness getFitnessClass(int index)
        {
            return fitness[index];
        }

        public  int Count()
        {
            return fitness.Count;
        }
    }
}



above is my class that holds my list while below is my class the serializes values in the list to a .dat file

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;

namespace JustMe
{
   public class SerialiseHandler
    {
        public void WriteListToFile(string filePath)
        {
            FileStream outFile;
            BinaryFormatter bFormatter = new BinaryFormatter();
            string newpath = HttpContext.Current.Server.MapPath(filePath);
            
            outFile = new FileStream(newpath, FileMode.Create, FileAccess.Write);

            
            bFormatter.Serialize(outFile, FITS.GetValues());

            
            outFile.Close();
        }

       
        public FITS ReadListFromFile(String filePath)
        {
            FileStream inFile;
            BinaryFormatter bFormatter = new BinaryFormatter();
            FITS fits = new FITS();
            string newpath = HttpContext.Current.Server.MapPath(filePath);
            
            inFile = new FileStream(newpath, FileMode.Open, FileAccess.Read);

            
            fits = (FITS)bFormatter.Deserialize(inFile);

            inFile.Close();
            return fits;
        }
    }
}
Posted
Updated 5-Apr-12 13:34pm
v3
Comments
[no name] 5-Apr-12 8:13am    
Subject line edited.

Do not ask for urgent help, it is very rude. This is a volunteer site and people will answer you on there time, not yours.
AmitGajjar 5-Apr-12 8:14am    
correct !!!
IamBlessed 5-Apr-12 8:24am    
Apologies if i sounded rude... Am very anxious thats all :)

1 solution

[Serializable] attribute works only on instance public properties, you can't use it for static members.

You can implement ISerializable interface in your class. That way you have complete control of what data is serialized and how, but it takes some coding.

As an alternative, consider implementing 'fitness' member as a singleton instead of static field.
 
Share this answer
 
Comments
IamBlessed 5-Apr-12 9:02am    
Thanks
IamBlessed 5-Apr-12 9:10am    
Can you please give an example of how to implement sigleton on my class please....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900