简体   繁体   中英

C# Reading variables and using them in other class

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;
}

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";

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;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM