Question

I am using ASP.NET 3.5 with C#2008.

I have a table with data like below :

Id Code Message Details
-- ---- ------  ------------
1  111  xxxxx   xxxxxxxxxxx
2  222  yyyyy   yyyyyyyyyyy
3  333  zzzzz   zzzzzzzz

and so on..

This is static data and I want to use it across application.

I have created a class with properties as below :

public class CodeDetails
{
   public int Id { get; set; }
   public int Code { get; set; }
   public string message { get; set; }
   public string details { get; set; }
}

And created another class codeDetailsList to whom I want to make as singletone as below :

public class codeDetailsList
{
    private List<CodeDetailst> lstCodeDetailst;

    private CodeDetailst()
    {
       lstCodeDetails = new List<CodeDetails>();
    }
}

Now, what I want to do is, I want to add the items of above given tabular data into this singleton class's list and want to access it throughout application.

My question is how to add the items and access them?

Was it helpful?

Solution

Similar to Varun's answer, I just wanted to do a full implementation.

public class CodeDetailsList
{
    private static readonly CodeDetailsList _instance = new CodeDetailsList();
    public static CodeDetailsList Instance 
    {
        get { return _instance; }
    }

    public ReadOnlyCollection<CodeDetails> lstCodeDetailst { get; private set; }

    private codeDetailsList()
    {
        var masterList = new List<CodeDetails>();

        masterList.Add(new CodeDetails(1, 111, "xxxxx", "xxxxxxxxxxx"));
        masterList.Add(new CodeDetails(2, 222, "yyyyy", "yyyyyyyyyyy"));
        //... And so on ...

        //mark the list as read only so no one can add/remove/replace items in the list
        lstCodeDetailst= masterList.AsReadOnly();
    }
}

public class CodeDetails
{
   public CodeDetails(id, code, message, details)
   {
       Id = id;
       Code = code;
       Message = message;
       Details = details;
   }

   //Mark the setters private so no one can change the values once set.
   public int Id { get; private set; }
   public int Code { get; private set; }
   public string Message { get; private set; }
   public string Details { get; private set; }
}

The constructor for CodeDetailsList will be called once when you first try to access Instance (If you had other static members in the class the constructor would run on the first time any static member was called).

Because lstCodeDetaillst is a ReadOnlyCollection callers will not be able to add, remove, or replace objects in the list. Also because now CodeDetails has private setters all of the items in it are effectively "read only" too.

OTHER TIPS

public class CodeDetailsList
{
    public static readonly CodeDetailsList Instance = new CodeDetailsList();

    public List<CodeDetails> ListCodeDetails { get; private set; }

    private CodeDetailsList()
    {
        ListCodeDetails = new List<CodeDetails>
       {
           new CodeDetails { Id = 1, Code = 1, Details = "xxxxx", Message = "xxxxx"},
           new CodeDetails { Id = 2, Code = 2, Details = "yyyyy", Message = "yyyy"} // ...
       };
    }
}

You should initialize the data in the constructor of codeDetailsList. The constructor should remain private to insure you do not create a new instance. Access the data using the Instance field on CodeDetailsList.

Did you intentionally omit the getInstance() method from your Singleton class? anyway...

The add might look something like:

CodeDetails codeDetails = new CodeDetails();
codeDetails.setId("id1");
codeDetails.setCode("code1");
codeDetails.setMessage("message1");
codeDetails.setDetails("details1");

(CodeDetailsList.getInstanceMethod()).add(codeDetails);

To access:

CodeDetails codeDetails = (CodeDetails)(CodeDetailsList.getInstaceMethod()).get(0);

You can put it in a loop if you have number of records

Hope this helps

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top