VB.NET 枚举到字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20360025/
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
Enum to String in VB.NET
提问by Pratap Das
I have this enumeration
我有这个枚举
Public Enum Applications
Unknown = 0
AA = 1
BB = 2
CC = 3
End Enum
Private Const CALLING_APP As Applications= Applications.CC
CALLING_APP.ToString() is giving me "3". But I want "CC" - what am I doing wrong?
CALLING_APP.ToString() 给我“3”。但我想要“CC”——我做错了什么?
回答by alu
Pass "F" as parameter for the ToString()method: CALLING_APP.ToString("F")
传递“F”作为该ToString()方法的参数:CALLING_APP.ToString("F")
回答by ??ssa P?ngj?rdenlarp
try this:
尝试这个:
Private CALLING_APP As Applications= Applications.CC ' no "Const"
' CALLING_APP.ToString will return CC
Constcan apparently change how NET recognizes the constant. As a Const, I get Cannot find the method on the object instancewhile Intellisense "sees" it correctly. If you must use Constfor some reason, you can get the text return this way:
Const显然可以改变 NET 识别常量的方式。作为一个 Const,Cannot find the method on the object instance当 Intellisense 正确“看到”它时,我得到了。如果Const出于某种原因必须使用,则可以通过以下方式返回文本:
Dim strName as string = [Enum].GetName(GetType(Applications), CALLING_APP ))
It is basically what .ToString does for us behind the scenes. Typed as it is, your code should work.
这基本上就是 .ToString 在幕后为我们所做的。按原样输入,您的代码应该可以工作。

