[英]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.这是我用所有变量制作的 class。 This class is not within the same class as the form.此 class 与表单不在同一个 class 内。
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.您正在寻找的是 Singleton:确保 class 只有一个实例并提供对其的全局访问点。
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?另请参阅: static class 和 singleton 模式之间的区别?
In order to access the variable just call the static Instance method that ensures to instaciate the class if needed:为了访问变量,只需调用 static 实例方法,确保在需要时实例化 class:
Singleton.Instance().cassette0 = "Cassette0";
To complement Matthias' response here's the thread-safe implementation of the Singleton pattern I use most of the time:为了补充 Matthias 的回应,这里是我大部分时间使用的 Singleton 模式的线程安全实现:
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.您可以使用 ExpressSettings 库轻松实现它。 It'll wite a settings.json file to the bin folder to store any kind of settings in a JSON format它会将 settings.json 文件写入 bin 文件夹,以 JSON 格式存储任何类型的设置
If you are on.Net Framework如果你在.Net Framework
Install-Package Twileloop.ExpressSettings -Version 1.0.0
Or, If you are on.Net Core或者,如果你在.Net Core
Install-Package Twileloop.ExpressSettingsCore -Version 1.0.0
Now read your data with your function, And return that constants现在用您的 function 读取您的数据,并返回该常量
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欲了解更多信息: https://github.com/sangeethnandakumar/Express-Settings
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.