469,960 Members | 1,876 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,960 developers. It's quick & easy.

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

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

Nov 22 '06 #1
4 1357
Hi,

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

Ken
-----------------------------
"dmac" <dr***@drmcl.free-online.co.ukwrote in message
news:11**********************@e3g2000cwe.googlegro ups.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
Nov 22 '06 #2


"dmac" <dr***@drmcl.free-online.co.ukwrote in message
news:11**********************@e3g2000cwe.googlegro ups.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

Nov 22 '06 #3
"dmac" <dr***@drmcl.free-online.co.ukschrieb:
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/>

Nov 22 '06 #4
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

"dmac" <dr***@drmcl.free-online.co.ukschreef in bericht
news:11**********************@e3g2000cwe.googlegro ups.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

Nov 24 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Michael Kennedy [UB] | last post: by
31 posts views Thread by Jamie Burns | last post: by
15 posts views Thread by mr.peteryu | last post: by
6 posts views Thread by Jon | last post: by
15 posts views Thread by Lloyd Dupont | last post: by
2 posts views Thread by =?Utf-8?B?Unlhbg==?= | last post: by
reply views Thread by rainxy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.