开发者

Can a property return a default value (similar to some built-in .NET properties)?

开发者 https://www.devze.com 2023-04-08 14:26 出处:网络
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 ret开发者_JAVA百科urned a string value and o

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 ret开发者_JAVA百科urned 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.


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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消