Why must I cast when I put type safe generic objects in the combo box?

D

dmac

Below is some really simple code - its just a trivial class used to
populate a combo box from which I want to pull out one of the
properties of the selected object. I am just curious to know why - or
better yet how to eliminate - the need to cast from an object when I
have specifically filled the combo box with a List(of T) . To replicate
this query, just create a new VB Windows application, put a button, a
combo box and a text box on the form and this code behind
All hail bob.


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim lst As New List(Of Bob)
lst.Add(New Bob("Elvis", "Presley"))
lst.Add(New Bob("King", "Kong"))
lst.Add(New Bob("Lassie", "The Dog"))
Me.ComboBox1.DataSource = lst
End Sub


Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object,
ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Me.TextBox1.Clear()
Dim myBob As Bob
myBob = DirectCast(Me.ComboBox1.SelectedItem, Bob) 'THIS is the
annoying bit
Me.TextBox1.Text = myBob.FirstNAme
End Sub

Private Class Bob
Private strFirstName As String
Private strSecondName As String
Public Sub New(ByVal strFirst As String, ByVal strSecond As
String)
strFirstName = strFirst
strSecondName = strSecond
End Sub
Public ReadOnly Property FirstName() As String
Get
Return strFirstName
End Get
End Property
Public ReadOnly Property SecondName() As String
Get
Return strSecondName
End Get
End Property
Public Overrides Function ToString() As String
Return FirstName + " " + SecondName
End Function
End Class

'Without the cast, i do not get access to my class members, even though
it is not objects in the combo box but bob objects?

Dave
 
K

Ken Tucker [MVP]

Hi,

The combobox's datasource is an object so the selecteditem needs to
be an object.

Ken
 
D

David Browne

dmac said:
Below is some really simple code - its just a trivial class used to
populate a combo box from which I want to pull out one of the
properties of the selected object. I am just curious to know why - or
better yet how to eliminate - the need to cast from an object when I
have specifically filled the combo box with a List(of T) . To replicate
this query, just create a new VB Windows application, put a button, a
combo box and a text box on the form and this code behind
All hail bob.

Just because you stuffed it with Bob's doesn't guarantee to the compiler
that some other sub-genius hasn't come along and put something else in
there.

David
 
H

Herfried K. Wagner [MVP]

dmac said:
myBob = DirectCast(Me.ComboBox1.SelectedItem, Bob) 'THIS is the
annoying bit

The return type of 'SelectedItem' is 'Object', which is the base type of all
classes. If the return type was 'Bob' then you would not need the cast, but
this is not the case.
 
C

Cor Ligthert [MVP]

To show in another way what the others are writting

\\\
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object,
ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
'this one below is a little bit without sense by the way
Me.TextBox1.Clear()
tthis one below is not needed as myBob is global
Dim myBob As Bob = DirectCast(Cobobox1.DataSource,Bob)
Me.TextBox1.Text = myBob(selectedindex).FirstNAme
End Sub
///

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top