单行上的 VB.NET 公共属性
有什么方法可以像在 C# 中一样将公共属性放在 VB.NET 中的一行上吗?每次我尝试将所有内容移至一行时,都会遇到一堆错误。
C#:
public void Stub{ get { return _stub;} set { _stub = value; } }
VB.NET
Public Property Stub() As String
Get
Return _stub
End Get
Set(ByVal value As String)
_stub = value
End Set
End Property
谢谢
编辑:我应该澄清一下,我正在使用 VB 9.0。
Is there any way I can put Public Properties on a single line in VB.NET like I can in C#? I get a bunch of errors every time I try to move everything to one line.
C#:
public void Stub{ get { return _stub;} set { _stub = value; } }
VB.NET
Public Property Stub() As String
Get
Return _stub
End Get
Set(ByVal value As String)
_stub = value
End Set
End Property
Thanks
EDIT: I should have clarified, I'm using VB 9.0.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以在 VB 10 和 C#,两者都比您显示的 C# 短:
对于不平凡的属性,听起来您可以将所有内容放在VB中的一行上 - 但因为它有点冗长,我怀疑您最终会得到一条很长的线,从而损害可读性...
You can use automatically implemented properties in both VB 10 and C#, both of which will be shorter than the C# you've shown:
For non-trivial properties it sounds like you could get away with putting everything on one line in VB - but because it's that bit more verbose, I suspect you'd end up with a really long line, harming readability...
是的,你可以
,你甚至可以让它更短并且完全不可读;-)
Yes you can
and you can even make it shorter and not at all readable ;-)
如果愿意使用稍微不同的语法,并链接到 C# 程序集(或使用 IL),则可以在一行上定义一个变量,其行为类似于属性。
使用 VS2017 和 .Net 4.7.2 进行测试。
C# 代码(目前在 VB.Net 中不可用):
然后在您的 VB 程序中,例如:
这会导致:
It is possible to define a variable on one line, that behaves like a property, if one is willing to use a slightly different syntax, and link to a C# assembly (or play around with IL).
Tested with VS2017 and .Net 4.7.2.
The C# code (currently not available in VB.Net):
Then in your VB program, for example:
This results in: