PropertyInfo(Of T) - constructor question

PropertyInfo(Of T) - constructor question

Old forum URL: forums.lhotka.net/forums/t/5052.aspx


JoeFallon1 posted on Thursday, July 03, 2008

There are many constructors for this class. I am not sure which one gets called when the type T is String.

--------------------------------------------------------------------------------------------------------------------------------

Public Class PropertyInfo(Of T)

  Public Sub New(ByVal name As String, ByVal friendlyName As String)

  Public Sub New(ByVal name As String, ByVal defaultValue As T)

End Class

--------------------------------------------------------------------------------------------------------------------------------

In the code below, I am trying to set the Friendly name as the 2nd parameter. But how do I know it is not really setting the defaultValue which is also the 2nd parameter of a different constructor? Which constructor is really getting called in this case?

e.g. Dim x As PropertyInfo(Of String) = New PropertyInfo(Of String)("Data", "Object data")

Joe

RockfordLhotka replied on Thursday, July 03, 2008

That's a good question. Breaking change or not, I should probably remove that second ctor overload, forcing you to provide a friendly name if you provide a default value.

JoeFallon1 replied on Thursday, July 03, 2008

Rocky,

I agree that removing it is a good idea.

While trying to create a subclass for PropertyInfo(Of T) I am having some trouble with the defaultValue. You made the Property Overridable but did not give us access to the Private variable _defaultValue. This caused me to have to create my own variable in my subclass and then use it inside the overridden Property. Can you do one of these 2 suggestions:

1. Make _defaultValue Protected instead of Private.

2. Create a Protected Overridable function named GetDefaultValue As T.

Then call that function inside the constructor with 2 parameters:
Public Sub New(ByVal name As String, ByVal friendlyName As String)

Protected Overridable Function GetDefaultValue() As T
 
Dim result As T

  If
GetType(T).Equals(GetType(String)) Then
   
result = DirectCast(DirectCast(String.Empty, Object), T)
Else
 
result = Nothing
End If

  Return result

End Function

By doing this I can override the function and add defaults for numeric values too.

e.g.

Protected Overrides Function GetDefaultValue() As T
  ...

  ElseIf GetType(T).Equals(GetType(Integer)) OrElse GetType(T).Equals(GetType(Long)) OrElse GetType(T).Equals(GetType(Decimal)) OrElse GetType(T).Equals(GetType(Short)) OrElse GetType(T).Equals(GetType(Byte)) Then

  result = DirectCast(DirectCast(0, Object), T)

...
End Function

Joe

Copyright (c) Marimer LLC