【问题标题】:Create several instances of settings创建多个设置实例
【发布时间】:2013-12-09 18:59:50
【问题描述】:

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

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

    AccelerometerSettings SomeRandomAccelerometer = New AccelerometerSettings();

我想要

    AccelerometerSettings TheNameTypedInTheTextBox = New AccelerometerSettings();

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

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

提前非常感谢!

【问题讨论】:

  • 您提供的示例仅修改了变量的名称。可能不是你的本意。您可以选择有一个外部配置文件,该文件根据其保存设置的传感器类型传递给 AcceleromterSettings 构造函数。然后构造函数将使用该文件来确定其设置。配置文件路径可以保存在以 textBox 值为键的字典中,但这会导致每个新传感器都需要更改源。
  • 不太熟悉 AccelerometerSettings,但如果我有一项任务将 on 对象与字符串相关联,我可能会使用 Dictionary。还有其他几种方法可以做到这一点,但我认为这是最简单的方法。
  • @Michael:谢谢,但我不太明白!我已经使用 Visual Studios 工具创建了设置文件。但是你的意思是我应该添加一些其他类型的配置文件?如果是这样,那是什么类型的配置文件,我应该在该文件中保存什么?源代码的更改,你的意思是我必须为我添加的每个传感器修改我的代码吗?
  • @Eugene:有趣!我在哪里保存该字典,以便它在会话之间存储?您的解决方案是否意味着只要我使用不同的字符串,我可以将“AccelerometerSettings”(我的设置类)命名为相同的东西?
  • @user2950764 我对移动开发不是很熟悉,所以不确定存储选项。您可以随意命名您的对象,名称无关紧要 - 您存储的是字符串键和一个对象。密钥必须是唯一的。

标签: c# textbox settings


【解决方案1】:

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

AccelerometerSettings TheNameTypedInTheTextBox = New AccelerometerSettings();

是 AccelerometerSettings 类型的变量的 C# 声明,它也被初始化为一个新的对象实例。

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

例如,您可以使用字典类

Dictionary<TKey,TValue>

声明并初始化你的字典为

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

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

【讨论】:

  • 谢谢加里!毫无疑问,我对这种编程相当陌生!我的想法是,由于我使用 Visual Studio 工具创建了一组新设置,因此要手动执行此操作,我将创建设置文件的新实例并重命名它。您和 Eugenes 的解决方案似乎很棒,但是他们提出了一些新问题。首先,我如何以一种简单的方式存储这些设置,以便设置在会话之间保持不变?我必须在启动时读取所有这些设置,还是它们会自动存储?提前致谢!
  • @user2950764 你可能想为此创建一个新问题,并标记/赞成你喜欢的答案。
【解决方案2】:

这是一个非常基本、幼稚的实现,可以实现接近您正在寻找的东西。它可以大大改进,但它应该让你走上正轨。

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

【讨论】:

  • 非常感谢!只是一些问题,SettingsManager() 方法和 LoadSettings 是做什么的?在启动时读取所有设置?设置 = null; 有什么用?在 GetSettings() 中,我看到它只返回 null?
  • @user2950764 查看编辑。如果您还有其他问题,请告诉我。
  • 再次@Michael,非常感谢!我得到了很多奇怪的错误,关于“类型'Program.AccelerometerSettings'已经包含'defaultInstance'(我从未创建或使用过的变量)的定义,并且几乎所有我尝试的属性都相同添加。当我双击错误时,我会看到自动生成的代码。知道这是什么意思吗?
  • 我不确定为什么会出现上述错误,但我删除了文件并创建了一个新文件,现在我似乎完全没有任何问题。
猜你喜欢
  • 1970-01-01
  • 2013-11-23
  • 1970-01-01
  • 2022-11-16
  • 2017-03-30
  • 2018-07-11
  • 2017-11-15
  • 2020-07-08
  • 1970-01-01
相关资源
最近更新 更多