vb.net 如何动态创建“枚举”类型的属性值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13422328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 11:11:00  来源:igfitidea点击:

How can I create an "enum" type property value dynamically

vb.netlistpropertiesdictionaryenums

提问by Dan Rowe

I need to create a property in a class that will give the programmer a list of values to choose from. I have done this in the past using the enums type.

我需要在一个类中创建一个属性,该属性将为程序员提供一个可供选择的值列表。我过去曾使用枚举类型完成此操作。

Public Enum FileType
    Sales
    SalesOldType
End Enum

Public Property ServiceID() As enFileType
    Get
        Return m_enFileType
    End Get
    Set(ByVal value As enenFileType)
        m_enFileType = value
    End Set
End Property

The problem is I would like to populate the list of values on init of the class based on SQL table values. From what I have read it is not possible to create the enums on the fly since they are basically constants.

问题是我想根据 SQL 表值填充类的 init 上的值列表。从我读到的内容来看,不可能即时创建枚举,因为它们基本上是常量。

Is there a way I can accomplish my goal possibly using list or dictionary types? OR any other method that may work.

有没有办法可以使用列表或字典类型来实现我的目标?或任何其他可行的方法。

采纳答案by Steven Doggart

If the values aren't going to change after the code is compiled, then it sounds like the best option is to simply auto-generate the code. For instance, you could write a simple application that does something like this:

如果代码编译后这些值不会改变,那么听起来最好的选择是简单地自动生成代码。例如,您可以编写一个简单的应用程序,执行如下操作:

Public Shared Sub Main()
    Dim builder As New StringBuilder()
    builder.AppendLine("' Auto-generated code.  Don't touch!!  Any changes will be automatically overwritten.")
    builder.AppendLine("Public Enum FileType")
    For Each pair As KeyValuePair(Of String, Integer) In GetFileTypesFromDb()
        builder.AppendLine(String.Format("    {0} = {1}", pair.Key, pair.Value))
    End For
    builder.AppendLine("End Enum")
    File.WriteAllText("FileTypes.vb", builder.ToString())
End Sub

Public Function GetFileTypesFromDb() As Dictionary(Of String, Integer)
    '...
End Function

Then, you could add that application as a pre-build step in your project so that it automatically runs each time you compile your main application.

然后,您可以将该应用程序添加为项目中的预构建步骤,以便每次编译主应用程序时它都会自动运行。

回答by Kratz

I don't know if this will answer your question, but its just my opinion on the matter. I like enums, mostly because they are convenient for me, the programmer. This is just because when I am writing code, using and enum over a constant value gives me not only auto-complete when typing, but also the the compile time error checking that makes sure I can only give valid enum values. However, enums just don't work for run-time defined values, since, like you say, there are compile time defined constants.

我不知道这是否会回答您的问题,但这只是我对此事的看法。我喜欢枚举,主要是因为它们对我这个程序员来说很方便。这只是因为当我编写代码时,在常量值上使用和枚举不仅可以在键入时自动完成,还可以进行编译时错误检查,以确保我只能提供有效的枚举值。但是,枚举不适用于运行时定义的值,因为就像您说的那样,存在编译时定义的常量。

Typically, when I use flexible values that are load from an SQL Table like in your example, I'll just use string values. So I would just store Salesand SalesOldTypein the table and use them for the value of FileType. I usually use strings and not integers, just because strings are human readable if I'm looking at data tables while debugging something.

通常,当我使用从您的示例中的 SQL 表加载的灵活值时,我将只使用字符串值。所以我只是将Sales和存储SalesOldType在表中并将它们用于FileType. 我通常使用字符串而不是整数,因为如果我在调试某些东西时查看数据表,字符串是人类可读的。

Now, you can do a bit of a hybrid, allowing the values to be stored and come from the table, but defining commonly used values as constants in code, sorta like this:

现在,您可以进行一些混合,允许存储值并来自表,但将常用值定义为代码中的常量,有点像这样:

 Public Class FileTypeConstants
       public const Sales = "Sales"
       public const SalesOldType = "SalesOldType"
 End Class

That way you can make sure when coding with common values, a small string typo in one spot doesn't cause a bug in your program.

这样您就可以确保在使用通用值进行编码时,一个地方的小字符串拼写错误不会导致您的程序出现错误。

Also, as a side note, I write code for and application that is deployed internally to our company via click-once deployment, so for seldom added values, I will still use an enum because its extremely easy to add a value and push out an update. So the question of using and enum versus database values can be one of how easy it is to get updates to your users. If you seldom update, database values are probably best, if you update often and the updates are not done by users, then enums can still work just as well.

另外,作为旁注,我编写了通过单击一次部署在我们公司内部部署的应用程序的代码,因此对于很少添加的值,我仍然会使用枚举,因为它非常容易添加值并推出一个更新。因此,使用枚举与数据库值的问题可能是向用户获取更新的难易程度之一。如果你很少更新,数据库值可能是最好的,如果你经常更新并且更新不是由用户完成的,那么枚举仍然可以正常工作。

Hope some of that helps you!

希望其中一些对您有所帮助!