Can someone explain why this VB.NET code fails to compile?

Imports System.ComponentModel
Public Enum Mode
    Mode1
    Mode2
End Enum

Public Class Class1
    <DefaultValue(Mode.Mode1)> _
    Dim Mode As Mode = Mode.Mode1
End Class

The resulting compiler error is "error BC30469: Reference to a non-shared member requires an object reference." It seems to be some sort of scoping confusion between the field and the enum (if you rename the field it works).

We're encountering this while adding a web reference to our web service (so the classes are auto-generated). It only fails in VB.NET - the equivalent code in C# works just fine.

9 Comments

  • Dim Mode as New Mode = Mode.Mode1

  • Uh - that doesn't compile either.

  • Try using a fully qualified class name in the attribute. The VB compiler is getting confused on just which Mode you are referring to.



    Imports System.ComponentModel

    Public Enum Mode

    Mode1

    Mode2

    End Enum



    Public Class Class1

    &lt;DefaultValue(MyNamespace.Mode.Mode1)&gt; _

    Dim Mode As MyNamespace.Mode = MyNamespace.Mode.Mode1

    End Class





    I believe that there is a design guideline to not name public properties the same as a class name. This applies to public fields as well.

  • That does in fact fix it - unfortunately, the actual class is being autogenerated by the framework, so I can't actually fix it by directly editing the code. But I am trying to understand why exactly it breaks.



    I hadn't run across that design guideline before (I should check whether FxCop complains about it). There are lots of examples in the framework where property names and class names are the same (Control.Size and Control.Region are two examples).

  • No offence, but who cooks up code like:

    Dim Mode as Mode ....



    NEVER name a variable after a type. It's sad enough that VB.NET allows that apparently.

  • The exact code that I've listed is an artificial repro case that we're having for the problem.



    The fact that the member is field is a byproduct of the fact that this is a generated web service proxy class. On the web service server side, this is property, where in fact the MS recommendation is that you DO name the property the same as its type.

  • If Macs are so great why are you futzing around in a .net site?

  • I have the same problm

  • Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    If Len(Page.Request.QueryString("ID")) 0 Then
    Label5.Text = Page.Request.QueryString("ID")
    End If
    If Session("Login") Is Nothing Then Server.Transfer("../SessionExpired.aspx")
    lblMessage.Text = ""
    Call ValidationScript()
    If Not IsPostBack Then
    cboG1.Enabled = False
    lblUserName.Text = Session("OperatorName")
    Me.cmdDelete.Attributes.Add("onclick", _
    "return confirm('Are you sure you want to delete?');")

    If Len(Page.Request.QueryString("ID")) 0 Then
    cmdSave.Text = "Save Changes"
    cmdDelete.Visible = True
    Call OperatorLoginValidation()
    Call RetrieveOperator(Page.Request.QueryString("ID"))
    End If
    Call LoadMaster()
    End If
    End Sub

Comments have been disabled for this content.