Home All Groups Group Topic Archive Search About

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

Author
22 Nov 2006 11:47 AM
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

Author
22 Nov 2006 12:22 PM
Ken Tucker [MVP]
Hi,

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

Ken
-----------------------------
Show quoteHide quote
"dmac" <dr***@drmcl.free-online.co.uk> wrote in message
news:1164196063.234073.314870@e3g2000cwe.googlegroups.com...
> 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
>
Author
22 Nov 2006 12:28 PM
David Browne
"dmac" <dr***@drmcl.free-online.co.uk> wrote in message
news:1164196063.234073.314870@e3g2000cwe.googlegroups.com...
> 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
Author
22 Nov 2006 12:49 PM
Herfried K. Wagner [MVP]
"dmac" <dr***@drmcl.free-online.co.uk> schrieb:
>        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.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>
Author
24 Nov 2006 6:07 AM
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

Show quoteHide quote
"dmac" <dr***@drmcl.free-online.co.uk> schreef in bericht
news:1164196063.234073.314870@e3g2000cwe.googlegroups.com...
> 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
>