none
Is a database or a file more suitable to store varying user input and then retrieve values to find average or sum? c# RRS feed

  • Question

  • Hello, 

    I have created Windows form application, in which the user inputs data into a form, however the amount of forms each user will have will be different. 

    My initial solution was to have my forms on tabs, and the user can add as many tabs as they want, and a new form gets renderred on each new tab. However I then need to sum and average some of the values from each form, which I struggled to do. 

    It was suggested to me that it would be a better idea to have a single form, and a button which once clicked stores the input data, and refreshes the form, when the user proceeds to the next stage, I would then need to retrieve those values and average or sum them. 

    The stored user input will need to be organised and I will need to recall the values later on. 

    Would it be better to store the user input in a file or to create a database for this sort of thing? 

    I've never done either, so I'm not sure which applies better to the situation.

    Appreciate any help, thank you.
    Friday, March 22, 2019 6:15 PM

Answers

  • Hello,

    Sure you could use a database or file while another choice would be using a Singleton pattern which if you research there are varying versions around thread safe but for one user, one standard Windows Form project this will work.

    The idea would be to have containers to hold user information e.g. list<T>, arrays, standard properties along with methods to perform calculations etc.

    The following is a very simple example where there is a list<int> which values are added via an Add method and a read only property to perform a simple sum.

    using System.Collections.Generic;
    using System.Linq;
    
    namespace WindowsFormsApp2
    {
        public sealed class UserContainer
        {
            private static readonly UserContainer instance = new UserContainer();
            public static List<int> DemoList;
            static UserContainer()
            {
               DemoList = new List<int>();
            }
    
    
            private UserContainer()
            {
            }
    
            public int Demo
            {
                get { return DemoList.Sum(); }
            }
    
            public void Add(int value)
            {
                DemoList.Add(value);
            }
    
            public static UserContainer Instance => instance;
        }
    }

    To add a value

    UserContainer.Instance.Add(10);

    To get the sum at anytime

    int result = UserContainer.Instance.Demo;
    Again, this is a super simple example which can be expanded on according to your needs.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, March 22, 2019 7:50 PM
    Moderator