属性可以返回默认值(类似于某些内置 .NET 属性)吗?
有没有办法让一个类可以返回默认类型而不指定属性?
如果我有一个具有一些属性的类,其中一个属性返回字符串值,其他属性返回其他类型,我可以执行以下操作吗?
Dim StudentGrade as new StudentGradeClass
调用并获取默认属性,
Dim CurrentGrade as string=StudentGrade
而不是这个
Dim CurrentGrade as string=StudentGrade.Current
我问这个问题的原因是当我的类具有其他类作为属性并且只想获取默认返回值时。
Is there a way to have a class that can return a default type without specifying the property?
If I had a class that had a few properties and one property returned a string value and others returned additional types, could I do the following?
Dim StudentGrade as new StudentGradeClass
Call and get default property,
Dim CurrentGrade as string=StudentGrade
instead of this
Dim CurrentGrade as string=StudentGrade.Current
The reason I am asking this is when I have classes that have other classes as properties and would like to just get a default return value.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
隐式转换运算符(VB 中的加宽转换运算符)基本上可以满足您的需求,但这实际上是可读性的障碍。我建议你不要这样做,除非你有一个非常非常好的理由。
参考资料:
如何:定义转换运算符 (Visual Basic)
有没有办法在 VB.NET 中定义隐式转换运算符?< /p>
An implicit conversion operator (Widening conversion operator in VB) will get you basically what you want, but this is really a hindrance to readability. I would advise you not to do it unless you have a really, really, really good reason.
References:
How to: Define a Conversion Operator (Visual Basic)
Is there a way to define an implicit conversion operator in VB.NET?
.NET 框架不支持您所说的“默认属性”。您可能会通过使用 隐式运算符来伪造它,但我建议这样做可能是一个坏主意。
The .NET framework has no support for what you refer to as a "default property." You could potentially fake it through the use of implicit operators, but I would suggest that it is probably a bad idea to do so.