2013-12-09 126 views
1

我即将创建一个应用程序,我要测量并因此能够存储大量加速度计的设置。问题是,我不知道现在有多少,因此我希望能够为每个加入的加速度计创建一个加速度计设置的新实例(加速计的名称,频率范围等)系统。为了能够配置设置,我添加了一个ListBox,它将显示加速度计的名称,并且当点击此框中的项目时,页面上的文本框应该填充设置。创建设置的几个实例

...至少这是理论。有谁知道如何基于文本框中的字符串创建AccelerometerSettings类的实例?那就是,而不是;

AccelerometerSettings SomeRandomAccelerometer = New AccelerometerSettings(); 

我想有

AccelerometerSettings TheNameTypedInTheTextBox = New AccelerometerSettings(); 

为每个新的加速度计。还是我完全误解了SettingsClass的工作原理?

如何在列表框中显示我所有的加速度计,并在单击时使设置显示在文本框中?

非常感谢提前!

+1

您提供的示例仅仅修改变量的名称。可能不是你的意图。您可以选择使用传递给AcceleromterSettings构造函数的外部配置文件,这个文件基于它所保存的传感器类型。然后构造函数将使用该文件来确定其设置。配置文件路径可以以textBox值作为关键字保存在字典中,但这会导致每个新传感器都需要更改源文件。 – Michael

+1

对AccelerometerSettings不太熟悉,但是如果我有一个任务将一个on对象与一个字符串联系起来,我可能会使用Dictionary 。还有其他一些方法可以完成,但我认为这是最简单的方法。 – Evgeni

+0

@迈克尔:谢谢,但我不太理解!我已经有了使用Visual Studios工具创建的设置文件。但是你的意思是我应该添加一些其他类型的配置文件?如果是这样,那么什么类型的配置文件,以及我应该保存在那个文件中?通过修改源代码,你的意思是我必须修改我添加的每个传感器的代码。 – user2950764

回答

1

您似乎有一些基本的理解问题。

AccelerometerSettings TheNameTypedInTheTextBox = New AccelerometerSettings();

是类型为AccelerometerSettings的变量的c#声明,该变量也被初始化为新的对象实例。

要在运行时维护命名的设置集合,您应该使用集合类。

例如,你可以使用一本字典类

Dictionary<TKey,TValue> 

声明并初始化你的字典作为

var MySettings = new Dictionary<string,AccelerometerSettings>(); 

使用上MySettings的Add()方法添加一个新实例名称(由用户提供)。字典

+0

谢谢加里!毫无疑问,我对这种编程相当陌生!我的想法是,因为我用Visual Studio工具创建了一套新的设置,所以要手动完成它,我会创建一个设置文件的新实例并对其进行重命名。你和Eugenes的解决方案看起来很棒,但是他们提出了一些新的问题。首先,我如何以简单的方式存储这些设置,以便设置从会话到会话保持不变?我是否必须在启动时阅读所有这些设置,或者是否自动保存?提前致谢! – user2950764

+1

@ user2950764您可能需要为此创建一个新问题,并标记/上传您喜欢的答案。 – Evgeni

+0

@Eugene:你可能是对的。对不起,我忘了! – user2950764

1

这是一个非常基本的,天真的实现的东西,将实现接近你在找什么。它可以大大改善,但它应该让你在正确的轨道上。

using System.Collections.Generic; 

class SettingsManager 
{ 
    Dictionary<string, AcceleromterSettings> settings; 

    // contructor for SettingsManager class; a fundamental C# concept 
    public SettingsManager() 
    { 
     LoadSettings(); 
    } 

    // creates new instances of AccelerometerSettings and stores them 
    // in a Dictionary<key, value> object for later retrieval. The 
    // Dictionary object uses a unique-key to access a value 
    // which can be any object. 
    void LoadSettings() 
    { 
     settings = new Dictionary<string, AcceleromterSettings>(); 

     AcceleromterSettings horizontal = new AcceleromterSettings(); 
     horizontal.AttributeOne = 101; 
     horizontal.AttributeTwo = 532; 
     horizontal.AttributeThree = 783; 
     settings.Add("Horizontal", horizontal); 

     AcceleromterSettings vertical = new AcceleromterSettings(); 
     vertical.AttributeOne = 50; 
     vertical.AttributeTwo = 74; 
     vertical.AttributeThree = 99; 
     settings.Add("Vertical", vertical); 
    } 

    // Retrieves settings from the Dictionary while hiding 
    // specific implementation details. 
    public AcceleromterSettings GetSettings(string name) 
    { 
     AcceleromterSettings setting = null; 

     // this method of Dictionary returns a Boolean value indicating 
     // whether or not the value retrieval worked. If the Key is found 
     // in the dictionary, the 'out' parameter is modified reflect 
     // the value, and the method returns true. If the key is not found 
     // the 'out' parameter is not modified, and the method returns false. 
     if (!settings.TryGetValue(name, out setting)) 
      throw new ArgumentException(string.Format("Settings not found for settings name {0}", name)); 

     return setting; 
    } 
} 
+0

非常感谢!但是,只有一些问题,SettingsManager()方法和LoadSettings有什么作用?在启动时阅读所有设置?什么是设置= null;在GetSettings()中,因为我看到它只是返回null? – user2950764

+1

@ user2950764请参阅编辑。如果您还有其他问题,请告诉我。 – Michael

+0

再次@迈克尔,非常感谢!虽然我收到了很多奇怪的错误,但是“类型'Program.AccelerometerSettings'已经包含了对'defaultInstance'(一个我从未创建和使用过的变量)的defenition,并且对于我尝试的几乎所有的属性都是一样的当我双击错误时,我得到自动生成的代码。任何想法是什么? – user2950764

相关问题