Synaps3
Synaps3

Reputation: 1657

Serialize a non-instantiated class

I have a class that I'm using to hold various data for my application. I never actually instantiate the class because I want the data to be accessible from all forms. I want to serialize this class, but it won't allow me to unless I create an instance of it. Is there a way around this or maybe a better way to accomplish what I'm trying to do?

Here is the class:

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;
using System.IO;

namespace Man
{
    public class ListProduct
    {
        public string Name;
        public int Quantity;
        public decimal Cost;
        public DateTime Date;
    }

    public class Product
    {
        public string Name;
        public bool IsCompound;
        public decimal BuyPrice;
        public decimal SellPrice;
        public List<ListProduct> SubItems = new List<ListProduct>();
    }

    public class ListEmployee
    {
        public string FirstName;
        public string LastName;
        public decimal Cost;
        public decimal Hours;
        public DateTime Date;
    }

    public class Employee
    {
        public string FirstName;
        public string LastName;
        public decimal Wage;
    }

    [Serializable()]
    public class Items : ISerializable
    {
        public static List<Product> ProdList = new List<Product>();
        public static List<Employee> EmpList = new List<Employee>();

        public static List<ListProduct> BuyList = new List<ListProduct>();
        public static List<ListProduct> SellList = new List<ListProduct>();

        public static List<ListEmployee> EmpHours = new List<ListEmployee>();
    }
}

Upvotes: 0

Views: 336

Answers (3)

Magicbj&#248;rn
Magicbj&#248;rn

Reputation: 619

I don't know if there is a method for this in your IDE, but in Xamarin, when programming for mobile devices, you can register a class by a certain name. using

using System;

namespace myApplication

[Register ("AppDelegate")]
public partial class AppDelegate
{
}

You can register the class across the program, so you can call properties and methods inside the class anywhere, without instantiating the class.

Upvotes: 0

Ankush Madankar
Ankush Madankar

Reputation: 3834

You might required Singleton here, Singleton is class which have private constructor. Initiate your class variable in constructor.

Let me do bit of code for class Items as Singleton:

public class Items
{
    public readonly List<Product> ProdList;
    public readonly List<Employee> EmpList;
    public readonly List<ListProduct> BuyList;
    public readonly List<ListProduct> SellList;
    public readonly List<ListEmployee> EmpHours;

    private static Items _Instance;

    private Items() 
    {
        ProdList = new List<Product>();
        EmpList = new List<Employee>();
        BuyList = new List<ListProduct>();
        SellList = new List<ListProduct>();
        EmpHours = new List<ListEmployee>();
    }

    public static Items Instance
    {
        get { return _Instance ?? (_Instance = new Items()); }
    }
}

Now Items has property Instance which will have single object of Items and by this property you can access all of public properties of class Items.

Items.Instance.ProdList.Add(new Product { Name = "Name of product" });

Check out singleton pattern in this question

https://stackoverflow.com/a/2667058/2106315

Upvotes: 2

Mr Jones
Mr Jones

Reputation: 134

I'm not saying this is a good way of doing it, but in this case I always create one object I share with all the forms. In your example that would be the class 'Items'. Create an instance of this object (when the applications starts for example) and then pass this object to all forms when opening them.

That is by far the easiest way of having some persistence in your application.

Upvotes: 0

Related Questions