jdross
jdross

Reputation: 1206

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

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.

Upvotes: 0

Views: 187

Answers (2)

FishBasketGordo
FishBasketGordo

Reputation: 23122

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:

Upvotes: 4

StriplingWarrior
StriplingWarrior

Reputation: 156459

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.

Upvotes: 2

Related Questions