Returning a specific property from a collection of properties?

  • Thread starter Thread starter Brian Mitchell
  • Start date Start date
B

Brian Mitchell

I have a class that contain one property, and that property contains a
collection of more properties (as follows:)



Public Class Class1



Private _Name As New cName



Public Property Name() As cName

Get

Return _Name

End Get



Set(ByVal Value As cName)

_Name = Value

End Set

End Property





Public Class cName

Private _First As String

Private _Middle As String

Private _Last As String



Public Property First() As String

Get

Return _First

End Get

Set(ByVal Value As String)

_First = Value

End Set

End Property



Public Property Middle() As String

Get

Return _Middle

End Get

Set(ByVal Value As String)

_Middle = Value

End Set

End Property



Public Property Last() As String

Get

Return _Last

End Get

Set(ByVal Value As String)

_Last = Value

End Set

End Property

End Class



End Class



Ok, Pretty straightforward so far and everything work great. If I want to
set the name I would simply set Name.First="whatever".



Now, how do I set up the Name property to return just the first name if the
user doesn't specify which name to return?



i.e. instead of saying something like TextBox1.Text=Name.First, how do I get
the same results by using TextBox1.Text=Name?



Is this possible or am I stuck specifically stating which name I want
returned?







Thank You!!
 
i.e. instead of saying something like TextBox1.Text=Name.First, how do I get
the same results by using TextBox1.Text=Name?



Is this possible or am I stuck specifically stating which name I want
returned?

You would implement a ToString Method which is best practise for all classes.

Public Overrides Function ToString() as String
' You can make this say whatever you want for example
Return String.Format("{0} {1}", Me.Firstname, Me.Surname)
End


hth

Richard
 
This discussion interests me because I wonder if there is a way to define one of the functions or properties as the default value to be returned when no specific function or property has been explicitly called. I guess, that is what Brian would want to do. And maybe in propriety, you could set the ToString function as the default function.
 
Back
Top