1

In my project, I want to read some variables. This data is read from the form. However, I want to use them as a variable in my whole project. What is the correct way to use these variables through the whole project?

This is the part where the variables are read.

public void ReadSetupData() {
    Constants setup = new Constants();
    setup.cassette0 = tbCassette0.Text;
    setup.cassette1 = tbCassette1.Text;
    setup.cassette2 = tbCassette2.Text;
    setup.cassette3 = tbCassette3.Text;
    setup.cassette4 = tbCassette4.Text;
    setup.flowController = tbFlowController.Text;
    setup.valve = tbValve.Text;
    setup.flowDeviation = Convert.ToDouble(tbMaxFlowDev);
    setup.flowSet = Convert.ToDouble(tbFlowInput);
    setup.flushTime = Convert.ToDouble(tbFlushTime);
    setup.flushTimeCalibration = Convert.ToDouble(tbFlushTimeCalibration);
    setup.intervalAveragePoints = Convert.ToDouble(tbIntervalAverage);
    setup.movingAverageSize = Convert.ToDouble(tbMovingAverageSize);
    setup.secsPerConcentration = Convert.ToDouble(tbSecsPerConc);
}

This is a class I made with all the variables. This class is not within the same class as the form.

public class Constants{
     public string cassette0;
     public string cassette1;
     public string cassette2;
     public string cassette3;
     public string cassette4;
     public string flowController;
     public string valve;
     public double flowDeviation;
     public double secsPerConcentration;
     public double intervalAveragePoints;
     public double movingAverageSize;
     public double flowSet;
     public double flushTime;
     public double flushTimeCalibration;
}
Minal Chauhan
  • 6,025
  • 8
  • 21
  • 41
  • What should happen when the form gets fileld out muttliple times? Should it overwrite the same instance (same variables)? – Matthias Apr 13 '21 at 07:32
  • 1
    Checkout static classes. You could make a public static class GlobalConstants { public static string cassette0; ...} and that will be available globally. Generally not a good coding practice, but sharing it so you're aware of it. – slacker Apr 13 '21 at 07:32
  • The data should only get read once, this can be protected by disabling the read button. – Zjwamerjong Apr 13 '21 at 07:39
  • If i use static in front of the variables it is not possible to read the data in the method ReadSetupData() – Zjwamerjong Apr 13 '21 at 07:40
  • You can also write a class "Configuration" or something where you provide all the values. For your first project you may use a static class and fill it once on startup, later on you may use a singleton or system classes. See [here](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/static-classes-and-static-class-members) some description to static classes and how they work. – Chrᴉz remembers Monica Apr 13 '21 at 07:42
  • By the way I recommend you to learn how to properly implement class properties. [Class properties](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/using-properties) and [Names of Properties](https://learn.microsoft.com/en-us/dotnet/standard/design-guidelines/names-of-type-members#names-of-properties) – Qwertyluk Apr 13 '21 at 07:54
  • Does this answer your question? [Accessing variables in other Windows Form class](https://stackoverflow.com/questions/12042303/accessing-variables-in-other-windows-form-class) – Thomas Weller Apr 13 '21 at 08:29
  • You neither need a static class nor a Singleton. The problem here is scope. Variables typically disappear at the next closing brace `}`. Simply create it somewhere else and then pass it to any object that needs it. Both static class and Singleton are bad practice. You will have no control over your architecture. – Thomas Weller Apr 13 '21 at 08:32
  • So i should use {get; set;} to get it working for this project? – Zjwamerjong Apr 13 '21 at 08:56
  • You should use properties and private fileds overall but just that will not solve your original problem. – Qwertyluk Apr 13 '21 at 10:07

3 Answers3

0

What you are looking for is a Singleton: Ensures a class has only one instance and provide a global point of access to it.

public class Singleton

{
    public string cassette0;
    public double flushTimeCalibration;
    private static Singleton _instance;

    // Constructor is 'protected'

    protected Singleton()
    {
    }

    public static Singleton Instance()
    {
        // Uses lazy initialization.

        // Note: this is not thread safe.

        if (_instance == null)
        {
            _instance = new Singleton();
        }

        return _instance;
    }
}

Please see also: Difference between static class and singleton pattern?

In order to access the variable just call the static Instance method that ensures to instaciate the class if needed:

Singleton.Instance().cassette0 = "Cassette0";
Matthias
  • 1,386
  • 3
  • 24
  • 59
0

To complement Matthias' response here's the thread-safe implementation of the Singleton pattern I use most of the time:

    public sealed class Constants
    {
        private static readonly Constants instance = new Constants();

        static Constants() { }

        private Constants() { }

        public static Constants Instance { get { return instance; } }   

        public string cassette0;
        public string cassette1;
        public string cassette2;
        public string cassette3;
        public string cassette4;
        public string flowController;
        public string valve;
        public double flowDeviation;
        public double secsPerConcentration;
        public double intervalAveragePoints;
        public double movingAverageSize;
        public double flowSet;
        public double flushTime;
        public double flushTimeCalibration;
    }

Then call it where you need like this:

var constants = Constants.Instance;
Andrii
  • 133
  • 1
  • 10
  • We're using static initializer `private static readonly Constants instance = new Constants();`, which are thread-safe by CLR. We could even make it shorter by using: `public static Constants Instance { get; } = new Constants();` – Andrii Apr 13 '21 at 08:59
  • How i understand this code is you make the variables accessible for the whole project and give a way to access them. However, how do i assign the values to these variables. These are read from the form. – Zjwamerjong Apr 13 '21 at 09:07
  • Just get the `Constants.Instance` and you will be able to set value to its public fields. Like so: `var constants = Constants.Instance; constants.cassette0 = "yourValue"` or even `Constants.Instance.cassette0` – Andrii Apr 13 '21 at 09:10
0

You can achieve it easily using ExpressSettings library. It'll wite a settings.json file to the bin folder to store any kind of settings in a JSON format

If you are on .Net Framework

Install-Package Twileloop.ExpressSettings -Version 1.0.0

Or, If you are on .Net Core

Install-Package Twileloop.ExpressSettingsCore -Version 1.0.0

Now read your data with your function, And return that constants

public Constants ReadSetupData() {
    Constants setup = new Constants();
    setup.cassette0 = tbCassette0.Text;
    setup.cassette1 = tbCassette1.Text;
    setup.cassette2 = tbCassette2.Text;
    setup.cassette3 = tbCassette3.Text;
    setup.cassette4 = tbCassette4.Text;
    setup.flowController = tbFlowController.Text;
    setup.valve = tbValve.Text;
    setup.flowDeviation = Convert.ToDouble(tbMaxFlowDev);
    setup.flowSet = Convert.ToDouble(tbFlowInput);
    setup.flushTime = Convert.ToDouble(tbFlushTime);
    setup.flushTimeCalibration = Convert.ToDouble(tbFlushTimeCalibration);
    setup.intervalAveragePoints = Convert.ToDouble(tbIntervalAverage);
    setup.movingAverageSize = Convert.ToDouble(tbMovingAverageSize);
    setup.secsPerConcentration = Convert.ToDouble(tbSecsPerConc);
    return setup;
}

Then save it to settings file,

var setup = ReadSetupData();
Settings<Constants>.Write(setup);

Now when you want to read it anywhere else

var setup = Settings<Constants>.Read();

For more info: https://github.com/sangeethnandakumar/Express-Settings

Sangeeth Nandakumar
  • 1,362
  • 1
  • 12
  • 23