本文介绍了如何跨多个表单共享变量/数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对C#比较陌生,以前使用过其他几种语言,但是如果我能够恰当地解释它,请帮助理解如何实现以下的最佳方法。我已经阅读了几篇文章等等,似乎没有人解释我想要做什么,或者我只是没有理解这个概念,而且每个人都在建议不要在编码中重复自己,这是我的理念。 ,但这在其他语言中很容易。现在,我已经把它放在胸前了,我会试着解释一下我想要做什么。



我想有一些不断的信息,可以使用以其他几种形式。即。我已经设置了一个数据库,我可以轻松访问,但在数据库中我有一个为其分配整数的字段,它相对于我要用于显示的字符串常量之一。我还设置了一个枚举器,用于存储信息并用它来比较结果。我已在单独的类的命名空间中设置。



ie。

public enum transactionType {Unassigned,Income,Expense,Transfer,现金,信贷,贷款};



我知道,这给每个元素分配一个整数,从零开始增加1.我也知道我有用这是通过使用transactionType.Income进行分配和比较等等,这很好,因为这意味着我不必记住每个元素使用的数字,我只需要记住这个名字。



我想做的是,有一个LIST或ARRAY,其中包含完整的描述,可以通过枚举器类型引用,以便它可以显示在一个表单,一个报表,并填写一个组合框等。然而,我找不到一种方法来使用它在所有形式,而不是在每种形式实现它,这是重复的,并且容易出错,需要在如果需要,每种形式。



我读过最好在课堂上实现它,而不是引用课程,但我不能让它工作,或者只是误解了这个概念。



之后,这个相当漫长的解释,我希望是可以理解的,有人可以指出我正确的方向,并解释,帮助或提供我需要做的一些例子。



非常感谢。



我尝试过:



这个我现在是这样做的,通过使用一个数组,这不一定是最好的方式,但它是如何在过去做的,但由于这些数据是不变的,永远不会改变,我认为这是最好的选择。我也尝试创建它作为列表。



public static string [] TransactionTypes = new string [] {Unassigned,Income,Expense ,转移,现金,信用,贷款};

I am relatively new to C#, have used several other languages before, but would like help understanding how the best way to implement the following, if I can explain it appropriately. I have read several articles etc. and none seem to explain what I am trying to do, or I am simply not grasping the concept, and the fact that everyone keeps advising "don't repeat yourself in coding", which is my philosophy to, but this was easy in other languages. Now, I have got that off my chest, I will try to explain what I want to do.

I would like to have some constant information, that can be used in several other forms. ie. I have set up a database, which I can access easily, but in the database I have a field that is assigning an integer to it, that is relative to one of the string constants I which to use to display. I have also set up an enumerator that is used to store the information and use it to compare the result. This I have setup in the namespace of a separate class.

ie.
public enum transactionType { Unassigned, Income, Expense, Transfer, Cash, Credit, Loan };

This, I know, assigns an integer to each element starting with zero and increasing by 1. I also, know that I have use this to assign and compare by using transactionType.Income for example and so on, which is great, as it means I don't have to remember what number I use for each element, I simply have to remember the name.

What, I would like to do, is have a LIST or ARRAY, with the full description in in, that can be referenced by the enumerator type, so that it can be displayed either in a form, on a report, and to fill a combobox etc. However, I cannot find a way to use it across all forms without implementing it in each form, which is repetitive, and open to mistakes, and needs to be changed in every form if need be.

I have read that it would be best to implement it in a class and than reference the class, but I cannot get this to work, or simply am misunderstanding the concept.

After, this rather long winded explanation, which I hope is understandable, could someone point me in the right direction, and explain, help or provide some example of what I need to do.

Many thanks.

What I have tried:

This is how I am doing it at the moment, by using an array, which is not necessarily the best way, but was how it was done in the old days, but as this data is constant and never changes, I thought it the best option. I have also, tried creating it as a LIST.

public static string[] TransactionTypes = new string[] { "Unassigned", "Income", "Expense", "Transfer", "Cash", "Credit", "Loan" };

推荐答案

public enum transactionType { Unassigned, Income, Expense, Transfer, Cash, Credit, Loan };

然后你声明一个这种类型的变量:

And you then declare a variable of that type:

transactionType tt = transactionType.Expense;

然后枚举的ToString的默认实现打印名称,而不是数值:

Then the default implementation of ToString for an enum prints the name, not the numeric value:

Console.WriteLine(tt);

打印:

Prints:

Expense

同样:

Similarly:

transactionType tt;
for (int i = (int) transactionType.Unassigned; i <= (int) transactionType.Loan; i++)
    {
    tt = (transactionType) i;
    Console.WriteLine(tt);
    }

将依次打印每个值:

Will print each value in turn:

Unassigned
Income
Expense
Transfer
Cash
Credit
Loan

因此,如果你持有你在数据库中读取的价值 transactionType 变量,在任何使用变量的地方都会将值打印为名称。

您还可以非常轻松地获取枚举中的所有名称:

So if you hold the value you read from the DB in a transactionType variable, anywhere you use the variable will print the value as a name.
You can also get all the names in the enum very easily:

string[] values = Enum.GetNames(typeof(transactionType));
foreach (string s in values)
    {
    Console.WriteLine(s);
    }

还将打印:

Will also print:

Unassigned
Income
Expense
Transfer
Cash
Credit
Loan

但一般来说,你不需要这个。您可以直接将组合框设置为枚举值的范围:

But generally, you don't need this. You can set a combobox to the range of enum values directly:

myComboBox.DataSource = Enum.GetValues(typeof(transactionType));

并使用值立即:

And use the value immediately:

Console.WriteLine(myComboBox.SelectedValue);

甚至:

Or even:

transactionType tt = (transactionType)myComboBox.SelectedValue;
Console.WriteLine(tt);


这篇关于如何跨多个表单共享变量/数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 19:07