Difference between VB.Net and C# “As New WebContro

2020-03-27 03:53发布

问题:

I was refactoring some code, and part of it included moving it from VB.Net to C#.

The old code declared a member like this:

Protected viewMode As New WebControl

The new code, I eventually got working, like this:

protected WebControl _viewMode = new WebControl(HtmlTextWriterTag.Span);

I can presume that the New keyword meant: call the constructor! But how was VB.Net calling a constructor (a parameter-less one) that I couldn't call in C#?

回答1:

The reason this worked in VB, and not in C#, had nothing to do with assemblies.

The default constructor for WebControl is protected.

VB and C# have different interpretations of what "protected" means.

In VB, you can access a protected member of a class from any method in any type that derives from the class.

That is, VB allows this code to compile:

class Base
    protected m_x as integer
end class

class Derived1
    inherits Base
    public sub Foo(other as Base)
        other.m_x = 2
    end sub
end class

class Derived2
    inherits Base
end class

Because a "Derived1" is a base, it can access protected members of "other", which is also a base.

C# takes a different point of view. It doesn't allow the "sideways" access that VB does. It says that access to protected members can be made via "this" or any object of the same type as the class that contains the method.

Because "Foo" here is defined in "Derived1", C# will only allows "Foo" to access "Base" members from a "Derived1" instance. It's possible for "other" to be something that is not a "Derived1" (it could, for example, be a "Derived2"), and so it does not allow access to "m_x".

In this case of your code, VB allowed "sideways" access to the "WebControl" constructor.

C#, however, did not.



回答2:

The default constructor for WebControl (implicit in the VB line) is to use a span. You can call that constructor in c# as well as VB.NET.



回答3:

Accessing inherited protected constructors from a derived class in any context would raise data encapsulation issues.

Historically, C# since very first version allowed such access. But it was fixed in VS 2005. Derived classes can call their base protected constructors only from their own constructor now.

class Base
{
    protected Base()
    {
    }
}

class Derived : Base
{
    public Derived() : base() // Still allowed in VS 2005
    {
    }

    public void Main()
    {
        Base b = new Base(); // Allowed in VS 2003, but error in VS 2005
    }
}

In VB.NET, you can initialize variables in two ways. First with the assignment operator followed the declaration; second with the "As New" statement.

In case of the protected constructor, the "As New" always works fine. As for initialization by assignment it will raise a compilation error. But if you have more than one constructor in the base class, the assignment initialization will work as well!

Class Base
    Protected Sub New()
    End Sub
End Class

Class Derived
    Inherits Base

    Public Sub Main()
        Dim foo As New Base // Allowed
        Dim moo As Base = New Base() // Error if Base has only one constructor
    End Sub
End Class

Probably the reason why VB.NET allows this kind of access is in compatibility with legacy code.

More details: http://blogs.msdn.com/b/peterhal/archive/2005/06/29/434070.aspx



回答4:

Webcontrol wc = tab;