首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >创建多个设置实例

问创建多个设置实例
EN

Stack Overflow用户
提问于 2013-12-10 02:59:51
回答 2查看 66关注 0票数 1

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

...at至少这是理论上的。有人知道如何根据文本框中的字符串创建我的AccelerometerSettings类的实例吗?也就是说,而不是;

代码语言:javascript
复制
    AccelerometerSettings SomeRandomAccelerometer = New AccelerometerSettings();

我想要一份

代码语言:javascript
复制
    AccelerometerSettings TheNameTypedInTheTextBox = New AccelerometerSettings();

每一个新的加速度计。或者我完全误解了SettingsClass的工作原理?

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

首先要感谢大家!

EN

回答 2

Stack Overflow用户

发布于 2013-12-10 03:16:47

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

AccelerometerSettings TheNameTypedInTheTextBox =新建AccelerometerSettings();

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

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

例如,您可以使用字典类

代码语言:javascript
复制
Dictionary<TKey,TValue>

将您的字典声明并初始化为

代码语言:javascript
复制
var MySettings = new Dictionary<string,AccelerometerSettings>();

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

票数 1
EN

Stack Overflow用户

发布于 2013-12-10 04:02:15

这里是一个非常基本的,天真的实现,它将实现您正在寻找的东西。它可以得到极大的改进,但它应该会让你走上正确的道路。

代码语言:javascript
复制
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;
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20478511

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档