按名称访问程序设置

发布于 2024-08-16 05:45:44 字数 492 浏览 6 评论 0原文

我正在用 C# 编写一个程序。该程序控制一个物理滤镜轮,该滤镜轮具有可互换的轮子 A 至 H。每个轮子可容纳 8 个滤镜。我希望用户能够为每个轮子中的每个过滤器存储友好名称(总共 64 个)。该设备和程序能够识别车轮 ID (AH) 和过滤器位置 (1-8),因此存储这 64 个名称并能够通过 ID 和 POS 引用它们的最佳方式是什么。我可以为每个用户创建一个用户设置,但存在两个问题:1)登录到计算机的每个用户的名称都不相同(我认为),2)以便以编程方式访问特定名称我必须使用一个巨大的 case 语句。或者有没有办法通过名称访问设置?像这样..?

char WheelID = 'A';
int FilterPos = 4;
NewName = "FriendlyName";

string SettingIWant = WheelID.ToString() + FilterPos.ToString();

Properties.Settings[SettingIWant].Text = NewName;

I am writing a program in C#. The program controls a physical filter wheel which has interchangable wheels A through H. Each wheel can hold 8 filters. I want the user to be able to store friendly names for each filter in each wheel (64 total). The device and program are capable of identifying the wheel ID(A-H) and filter position (1-8) so what is the best way to store these 64 names and be able to reference them by the ID and POS. I could create a user setting for each one but there are two problems with that: 1) The names would not be the same for every user that logs onto the machine(i think), and 2) in order to access the specific names programmatically I have to use a HUGE case statement. Or is there a way to access settings by the name? like this..?

char WheelID = 'A';
int FilterPos = 4;
NewName = "FriendlyName";

string SettingIWant = WheelID.ToString() + FilterPos.ToString();

Properties.Settings[SettingIWant].Text = NewName;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

雨轻弹 2024-08-23 05:45:45

好吧,我只是愚蠢,忽略了这种访问设置的方式......

string SettingToChange = WheelID.ToString() + Position.ToString();
            Settings1.Default[SettingToChange] = NewName;
            Settings1.Default.Save();

它工作得很好。唯一的问题是每个用户存储的值并不相同,但他们只需要处理这个问题!

Ok, I was just dumb and overlooked this way accessing the settings...

string SettingToChange = WheelID.ToString() + Position.ToString();
            Settings1.Default[SettingToChange] = NewName;
            Settings1.Default.Save();

It works just fine. The only issue is the stored values won't be the same for every user but they will just have to deal with that!

彩虹直至黑白 2024-08-23 05:45:45

好吧,您可以采用数据库方法。

“内存中”的解决方案是字典。

char WheelID = 'A';
int FilterPos = 4;
NewName = "FriendlyName";

string SettingIWant = WheelID.ToString() + FilterPos.ToString();
Dictionary<string, string> properties = new Dictionary<string, string>()
properties.Add(SettingIWant, NewName);

Ant 然后您可以使用括号语法访问数据

properties[SettingIWant]

Well, you could take the database approach.

An "in memory" solution, would be Dictionaries.

char WheelID = 'A';
int FilterPos = 4;
NewName = "FriendlyName";

string SettingIWant = WheelID.ToString() + FilterPos.ToString();
Dictionary<string, string> properties = new Dictionary<string, string>()
properties.Add(SettingIWant, NewName);

Ant then you can access the data using bracket syntax

properties[SettingIWant]
只为守护你 2024-08-23 05:45:45

当然有。只需覆盖 [] 运算符即可。

Sure there is. Just override the [] operator.

街角卖回忆 2024-08-23 05:45:45

好的。我只是做了一些最小的研究,这就是我认为你想做的。

首先,实现 Rodrigo Garcia 的 Dicitonary 事物,但使用 System.Collections.Specialized.StringDictionary 除外。这是可以添加到设置文件中的类型之一。

设置应用程序设置时,如果列表中没有看到“类型”,请选择“浏览...”。

如果集合发生变化,请确保始终写出更改!

OK. I just did some minimal research and here's what I think you want to do.

First, implement Rodrigo Garcia's Dicitonary thing, except use System.Collections.Specialized.StringDictionary. This is one of the types that can be added to a settings file.

When setting up your application settings, under Type, select "Browse..." if you don't see it in the list.

And make sure you always write the changes out, if the collection changes!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文