VB.Net和C#&QUOT区别;至于新的WebControl" [英] Difference between VB.Net and C# "As New WebControl"

查看:178
本文介绍了VB.Net和C#&QUOT区别;至于新的WebControl"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我重构一些code和它的一部分,包括从VB.Net移动到C#。

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

旧code声明这样一个成员:

The old code declared a member like this:

Protected viewMode As New WebControl

新的code,我最终得到了工作,像这样的:

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

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

我可以presume的关键字的意思是:调用构造函数!但是,如何在VB.Net调用构造函数(参数少一个),我不能在C#中调用?

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#?

推荐答案

这工作在VB的原因,而不是在C#中,有无关组件。

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

默认构造函数的WebControl是受保护的。

The default constructor for WebControl is protected.

VB和C#有不同的跨$ P $什么保护的意思ptations。

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

在VB中,你可以从任何方法在任何类型的派生从类访问类的保护成员。

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

也就是说,VB允许这种code编译:

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

由于一个Derived1是碱,它可以访问的其他,这也是一个基保护成员

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

C#采取了不同的观点。它不允许的杯酒人生获得了VB一样。 它说的访问受保护成员可以经由相同的类型包含方法的类的这个或任何物体制成。

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.

由于富在这里是Derived1定义,C#将只允许富,从一个Derived1实例访问基地组织成员。有可能为其他是东西是不是Derived1(它可能,例如,是一个Derived2的),并且因此它不允许访问m_x

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".

在你的code这种情况下,VB允许杯酒人生获得的WebControl构造。

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

C#,但是,没有。

这篇关于VB.Net和C#&QUOT区别;至于新的WebControl"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆