如何在 VB 10 中编写私有设置自动属性?

发布于 2024-07-23 22:23:23 字数 244 浏览 12 评论 0原文

在 C# 中:

public string Property { get; private set; }

在 VB 中?

请投票或/并分享您的想法!

in C#:

public string Property { get; private set; }

in VB?

Please vote or/and share your ideas!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

泡沫很甜 2024-07-30 22:23:23

像这样:

Private Thingy As Integer
Property Thing() As Integer
    Get
        Return Thingy
    End Get
    Private Set(ByVal value As Integer)
        Thingy = value
    End Set
End Property

VB10 中的 Auto 属性

Property PartNo As Integer = 44302

但在 VB 中仍然无法使用私有设置,甚至在 VB10 中也无法完成,请参阅此处:

来自 MSDN(如约翰所说):

需要的属性定义
标准语法:

  • 指定不同的辅助功能
    获取和设置过程。 为了
    例如,您可能想要使
    设置过程 Private 和 Get
    程序公开。

Like this:

Private Thingy As Integer
Property Thing() As Integer
    Get
        Return Thingy
    End Get
    Private Set(ByVal value As Integer)
        Thingy = value
    End Set
End Property

Auto property in VB10

Property PartNo As Integer = 44302

But with a private set still can't be done in vb not even in VB10 see here:

From MSDN (as john said):

Property Definitions That Require
Standard Syntax :

  • Specify different accessibility for
    the Get and Set procedure. For
    example, you might want to make the
    Set procedure Private and the Get
    procedure Public.
提赋 2024-07-30 22:23:23

根据 这篇 MSDN 文章,您不能:

自动实现的属性是
方便,支持很多
编程场景。 然而,有
是您不能使用的情况
自动实现的属性并且必须
相反,使用标准或扩展,
属性语法。

您必须使用扩展
如果需要,可以使用属性定义语法
执行以下任一操作:

[...]

  • 指定不同的辅助功能
    获取和设置过程。 为了
    例如,您可能想要使
    设置过程 Private 和 Get
    程序公开。

According to this MSDN article, you can't:

Auto-implemented properties are
convenient and support many
programming scenarios. However, there
are situations in which you cannot use
an auto-implemented property and must
instead use standard, or expanded,
property syntax.

You have to use expanded
property-definition syntax if you want
to do any one of the following:

[...]

  • Specify different accessibility for
    the Get and Set procedure. For
    example, you might want to make the
    Set procedure Private and the Get
    procedure Public.
筱果果 2024-07-30 22:23:23

我认为这是不可能的(目前)。

请参阅 MSDN 上的此链接
上面的文章甚至链接到另一篇混合访问级别。

我在 Microsoft Connect 上发现了,因此他们正在考虑(如果是针对 VS2010 那是另一个问题)。

I don't think that is possible (yet).

See this link on MSDN.
The above article even links to another one about mixed access levels.

I found this on Microsoft Connect, so they are thinking about it (If it will be for VS2010 that's another question).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文