英文:
System.MissingMemberException: The public member description for the type was not found
问题
我有以下VB.NET代码已经有一些年头了,一直都运行得很好:
Public Enum eAussendienstlerVermittler
<Description("Undefined")> Undefined = 0
<Description("Jack")> Jack = 1
<Description("John")> John = 2
<Description("Tom")> Tom = 3
<Description("Martha")> Martha = 4
End Enum
For Each e In System.Enum.GetValues(GetType(eAussendienstlerVermittler))
Dim nItem As iGDropDownListItem
nItem = New iGDropDownListItem
nItem.Value = CInt(CType(e, Integer))
nItem.Text = e.Description 'Raises error
_DropDownListAußendienstler.Items.Add(nItem)
Next
现在我做了一些我不知道的事情,出现了错误:
"System.MissingMemberException: 未找到类型 eAussendienstlerVermittler 的公共成员描述。"
在第一个
nItem.Text = e.Description
可能是什么原因导致了这个错误?
谢谢!
英文:
I have the following code in VB.NET since some years, and it worked fine:
Public Enum eAussendienstlerVermittler
<Description("Undefined")> Undefined = 0
<Description("Jack")> Jack = 1
<Description("John")> John = 2
<Description("Tom")> Tom = 3
<Description("Martha")> Martha = 4
End Enum
For Each e In System.Enum.GetValues(GetType(eAussendienstlerVermittler))
Dim nItem As iGDropDownListItem
nItem = New iGDropDownListItem
nItem.Value = CInt(CType(e, Integer))
nItem.Text = e.Description 'Raises error
_DropDownListAußendienstler.Items.Add(nItem)
Next
Now I did something that I am not aware of, and I get the error
"System.MissingMemberException: The public member description for the type eAussendienstlerVermittler was not found."
in the first
nItem.Text = e.Description
What may I have done to cause this error?
Thank you!
答案1
得分: 1
CType(e, Integer)已经是一个整数,所以没有必要再次转换它。
这是对枚举类型的一种误解,因为每个元素的名称并不是描述,而是名称本身,例如
因此,当您循环遍历所有值时,您会得到像"jack as node"这样的名称,您可以通过将其转换为字符串来访问它。
所以您的代码将如下所示:
For Each e As eAussendienstlerVermittler In System.Enum.GetValues(GetType(eAussendienstlerVermittler))
Dim nItem As iGDropDownListItem
nItem = New iGDropDownListItem
nItem.Value = CType(e, Integer)
nItem.Text = e.ToString
_DropDownListAußendienstler.Items.Add(nItem)
Next
英文:
CType(e, Integer) already is an Integer, so there is no need to convert it a second time.
it is a misconception of enum types as the name of each elements isn't description but the name it self for example
So when you loopß over all values, you get the name like jack as node, which you can access by converting ot to string
So your code would look like
For Each e As eAussendienstlerVermittler In System.Enum.GetValues(GetType(eAussendienstlerVermittler))
Dim nItem As iGDropDownListItem
nItem = New iGDropDownListItem
nItem.Value = CType(e, Integer)
nItem.Text = e.ToString
_DropDownListAußendienstler.Items.Add(nItem)
Next
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论