System.MissingMemberException: 未找到该类型的公共成员描述

huangapple go评论104阅读模式
英文:

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
    &lt;Description(&quot;Undefined&quot;)&gt; Undefined = 0
    &lt;Description(&quot;Jack&quot;)&gt; Jack = 1
    &lt;Description(&quot;John&quot;)&gt; John = 2
    &lt;Description(&quot;Tom&quot;)&gt; Tom = 3
    &lt;Description(&quot;Martha&quot;)&gt; 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 &#39;Raises error 

        _DropDownListAu&#223;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)已经是一个整数,所以没有必要再次转换它。

这是对枚举类型的一种误解,因为每个元素的名称并不是描述,而是名称本身,例如

System.MissingMemberException: 未找到该类型的公共成员描述

因此,当您循环遍历所有值时,您会得到像"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&#223;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

System.MissingMemberException: 未找到该类型的公共成员描述

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&#223;endienstler.Items.Add(nItem)

Next

huangapple
  • 本文由 发表于 2023年6月18日 20:28:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76500537.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定