How Does ComboBox Determine What To Display?

  • Thread starter Thread starter eBob.com
  • Start date Start date
E

eBob.com

I implemented as class like this ...

Public Class ComboxChoice
Public ChoiceText As String
Public LastUsed As Date
Public UsedCount As Integer
'to be serializable we have to have a parameterless constructor
Sub New()
End Sub
Sub New(ByRef Choice As String, ByRef LastUsed As Date, _
ByVal UsedCount As Integer)
Me.ChoiceText = Choice
Me.LastUsed = LastUsed
Me.UsedCount = UsedCount
End Sub

Public Shadows ReadOnly Property ToString() As String
Get
Return Me.ChoiceText
End Get
End Property

'Public Overrides Function tostring() As String
' Return ChoiceText
'End Function
End Class

.... and I don't know what I was thinking when I made ToString a property
rather than a function. BUT this ...

MsgBox(New ComboxChoice("1st choice", DateTime.Now, 1).ToString)

.... gave the expected result, i.e. "1st choice", while this ...

cbx1.Items.AddRange(New Object() {New ComboxChoice("1st choice",
DateTime.Now, 1), _
New ComboxChoice("2nd choice",
DateTime.Now, 1)})

.... (where cbx1 is a ComboBox) does not - i.e. the items in the combobox do
not appear as "1st choice" and "2nd choice". I understand that ComboBox is
seeing the items as having a type of Object while in the MsgBox statement
ToString is clearly a member of ComboxChoice. But what kind of invocation
is ComboBox doing which manages to get the Object .ToString function rather
than my objects ToString property? Isn't "Shadows" supposed to completely
hide any ToString member in Object?

Thanks, Bob
 
I implemented as class like this ...

Public Class ComboxChoice
Public ChoiceText As String
Public LastUsed As Date
Public UsedCount As Integer
'to be serializable we have to have a parameterless constructor
Sub New()
End Sub
Sub New(ByRef Choice As String, ByRef LastUsed As Date, _
ByVal UsedCount As Integer)
Me.ChoiceText = Choice
Me.LastUsed = LastUsed
Me.UsedCount = UsedCount
End Sub

Public Shadows ReadOnly Property ToString() As String
Get
Return Me.ChoiceText
End Get
End Property

'Public Overrides Function tostring() As String
' Return ChoiceText
'End Function
End Class

... and I don't know what I was thinking when I made ToString a property
rather than a function. BUT this ...

MsgBox(New ComboxChoice("1st choice", DateTime.Now, 1).ToString)

... gave the expected result, i.e. "1st choice", while this ...

cbx1.Items.AddRange(New Object() {New ComboxChoice("1st choice",
DateTime.Now, 1), _
New ComboxChoice("2nd choice",
DateTime.Now, 1)})

... (where cbx1 is a ComboBox) does not - i.e. the items in the combobox do
not appear as "1st choice" and "2nd choice". I understand that ComboBox is
seeing the items as having a type of Object while in the MsgBox statement
ToString is clearly a member of ComboxChoice. But what kind of invocation
is ComboBox doing which manages to get the Object .ToString function rather
than my objects ToString property? Isn't "Shadows" supposed to completely
hide any ToString member in Object?

Thanks, Bob

The default behavior is to call Object.ToString(). You have not overriden
..ToString - but have shadowed it's implementation. So, the default object
implmentation will be used. Shadows only hides the parent implementation from
your descendants.
 
Tom Shelton said:
The default behavior is to call Object.ToString(). You have not overriden
.ToString - but have shadowed it's implementation. So, the default object
implmentation will be used. Shadows only hides the parent implementation
from
your descendants.

Thank you Tom, I was confused about the meaning of "Shadows". That
confusion came in part from a compiler/VS message! If my definition of the
ToString property omits "Shadows" then I get the message: "property
'ToString' conflicts with function 'ToString' in the base class 'Object' and
should be declared 'Shadows'". I would say that 'Shadows' in this case
didn't really resolve the conflict, or at least permitted an ambiguity.

Thanks again for your help, Bob
 
Back
Top