[英]Create new instance of settings
我將要編寫一個振動分析程序,就像一個學校項目一樣,該程序將使用傳感器測量振動和其他一些東西,然后使用一些算法對其進行分析。
無論如何,我希望用戶能夠設置參數和變量,因此創建了一些不同的設置。 不過這是正確的,因為我將有許多相同類型的傳感器(此時我不知道有多少個),我想在添加傳感器時創建一個新的設置或這些設置的實例。 此外,有些類型的變量我不知道會有多少這種類型的實例(例如說不同的RPM來比較振動),因此希望能夠添加該變量的另一個實例。
有誰知道該如何完成? 這可能很簡單,但是Google沒有給我任何東西,嘗試使用構造函數創建新實例根本沒有用。
到目前為止,這是我嘗試過的:
AccelerometerSettings Sensor3 = new AccelerometerSettings();
給了我一個名為Sensor3的加速度計設置的新實例,但是
Sensor3.accelerometerResolution = 10;
(我在double類型的加速度計設置中有一個設置,稱為“分辨率”)沒有任何幫助。 或實際上,它給我一個'='錯誤,並說這是無效令牌,而accelerometerResolution是一個字段,但用作類型。
編輯:這是Visual Studio自動生成的Settings類的代碼:
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.18052
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace Fault_detection_system {
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")]
internal sealed partial class AccelerometerSettings : global::System.Configuration.ApplicationSettingsBase {
private static AccelerometerSettings defaultInstance = ((AccelerometerSettings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new AccelerometerSettings())));
public static AccelerometerSettings Default {
get {
return defaultInstance;
}
}
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("Accelerometer-Name")]
public string accelerometerName {
get {
return ((string)(this["accelerometerName"]));
}
set {
this["accelerometerName"] = value;
}
}
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("20.0")]
public decimal accelerometerResolution {
get {
return ((decimal)(this["accelerometerResolution"]));
}
set {
this["accelerometerResolution"] = value;
}
}
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("10.0")]
public decimal accelerometerAccuracyUp {
get {
return ((decimal)(this["accelerometerAccuracyUp"]));
}
set {
this["accelerometerAccuracyUp"] = value;
}
}
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("10.0")]
public decimal accelerometerAccuracyDown {
get {
return ((decimal)(this["accelerometerAccuracyDown"]));
}
set {
this["accelerometerAccuracyDown"] = value;
}
}
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("10")]
public decimal faultFrequency {
get {
return ((decimal)(this["faultFrequency"]));
}
set {
this["faultFrequency"] = value;
}
}
}
這是您的Settings類,您可以在運行時通過靜態屬性Default
進行訪問,該屬性允許您從app.config文件(其類型為AccelerometerSettings
訪問已加載的設置,並可以在其中訪問其屬性,如下所示:
var resolution = AccelerometerSettings.Default.accelerometerResolution;
以及是否要更改設置並將其保存在user.config中
AccelerometerSettings.Default.accelerometerResolution = 42.0;
AccelerometerSettings.Default.Save();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.