Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to have a data c# class in my wpf project to store simple string and number data. Anyone have a good example or idea?

What I have tried:

    internal class Att
    {
        BillInfo Aug2022W = new BillInfo(91.93M, "August 2022");//the data in class object
        BillInfo Aug2022I = new BillInfo(66.00M, "August 2022");// form instantiating with 
        BillInfo Sept2022W = new BillInfo(91.82M, "September 2022");// constructor from
        BillInfo Sept2022I = new BillInfo(66.00M, "September 2022");// BillInfo class
        BillInfo Oct2022W = new BillInfo(91.82M, "October 2022");
        BillInfo Oct2022I = new BillInfo(66.00M, "October 2022");
        BillInfo Nov2022W = new BillInfo(91.82M, "November 2022");
        BillInfo Nov2022I = new BillInfo(66.00M, "November 2022");
        BillInfo Dec2022W = new BillInfo(00.01M, "December 2022");
        BillInfo Dec2022I = new BillInfo(66.00M, "December 2022");
        BillInfo Jan2023W = new BillInfo(00.01M, "January 2023");
        BillInfo Jan2023I = new BillInfo(00.01M, "January 2023");
        BillInfo Feb2023W = new BillInfo(84.28M, "February 2023");
        BillInfo Feb2023I = new BillInfo(00.01M, "February 2023");
        BillInfo March2023W = new BillInfo(84.23M, "March 2023");
        BillInfo March2023I = new BillInfo(60.32M, "March 2023");

        BillInfo[] ArrayBillInfo = new BillInfo[16]; // making array of class objects     
        
        public BillInfo RonsMethod()
        {
            ArrayBillInfo[0] = Aug2022W; // method to assign each bill object data to 
            ArrayBillInfo[1] = Aug2022I; // the object array
            ArrayBillInfo[2] = Sept2022W;
            ArrayBillInfo[3] = Sept2022I;
            ArrayBillInfo[4] = Oct2022W;
            ArrayBillInfo[5] = Oct2022I;
            ArrayBillInfo[6] = Nov2022W;
            ArrayBillInfo[7] = Nov2022I;
            ArrayBillInfo[8] = Dec2022W;
            ArrayBillInfo[9] = Dec2022I;
            ArrayBillInfo[10] = Jan2023W;
            ArrayBillInfo[11] = Jan2023I;
            ArrayBillInfo[12] = Feb2023W;
            ArrayBillInfo[13] = Feb2023I;
            ArrayBillInfo[14] = March2023W;
            ArrayBillInfo[15] = March2023I;

            return ArrayBillInfo[15]; // return just the most recent array location
        }

        public BillInfo[] RonsMethod2() // return whole object array
        {
            
            return ArrayBillInfo;
        }}}

internal class BillInfo
    {
        public decimal Amount { get; set; }  //   object for data object array
        public string Date { get; set; }

        public BillInfo(decimal amount, string date)
        {
            Amount = amount;
            Date = date;
        }}
Posted
Updated 21-Apr-23 21:44pm
v2

My suggestion would be to access your raw data from a collection that implements IEnumerable to avoid having to hand code everything. I would use DateTime to store the date rather than a magic string. The necessary conversion can be handled in the BillInfo constructor.


C#
public class BillInfo
   {
       public DateTime Date { get; set; }
       public decimal Amount { get; set; }
       private readonly CultureInfo cultureInfo = new("en-GB");
       public BillInfo(decimal amount, string dateStr)
       {
           Amount = amount;
           Date = DateTime.Parse($"1 {dateStr}", cultureInfo);
       }
   }

So the basic set up would be along these lines.


C#
private static void Main()
   {
       List<(decimal amount, string dateStr)> RawData = new()
       {
           (91.93M, "August 2022"),
           (66.00M, "August 2022"),
           (91.82M, "September 2022"),
           (66.00M, "September 2022"),
           (91.82M, "October 2022"),
           (66.00M, "October 2022")

       };
       List<BillInfo> billInfos = new();
       foreach ((decimal amount, string dateStr) in RawData)
       {
           billInfos.Add(new BillInfo(amount, dateStr));
       }
       //To convert to array if needed
       BillInfo[] billInfoArray = billInfos.ToArray();
       //To retun the last item use
       var lastItemAdded = billInfos.Last();

       Console.ReadLine();
   }
 
Share this answer
 
Comments
ronCodeTracker 23-Apr-23 1:40am    
Is the BillInfo class supposed to be static?
Graeme_Grant 23-Apr-23 6:59am    
As common in-memory storage for sharing app-wide. Multiple objects are stored in a list, so same principle applies.
ronCodeTracker 23-Apr-23 2:17am    
nevermind
George Swan 23-Apr-23 2:27am    
Static classes are useful when you need a singleton instance but in this case many instances are created. I do not think you can have instance members in a static class which means the instance constructor would not be allowed. Also, the trend these days is to use interfaces as it makes the code more versatile but static classes cannot implement an interface. So, for these reasons, I would use a non-static instance.
If the data is common across the app, use a static class. I use the DataStore suffix to clarify what it is for. For Example:
C#
public static class AppDataStore
{
    public ApplicationTheme Theme { get; set; }

    public UserProfile User { get; set; }
{

Now to access the user, from anywhere in the app, all I have to do is:
C#
string userName = AppDataStore.User.Name;
 
Share this answer
 
Comments
ronCodeTracker 21-Apr-23 22:03pm    
Is ApplicationTheme and UserProfile classes not shown here?
Graeme_Grant 21-Apr-23 22:05pm    
It is a mock ... an example, so no, they do not exist. All that I am demonstrating is how to share common themed data ... you may have another for common billing information that you want to share across the app in BillingDataStore...

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