473,394 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 1443
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Michael Kennedy [UB] | last post by:
Hi Everyone, I have this multithreaded C# windows forms application which does a lot of image processing. Occasionally, I get the following error: A generic error occurred in GDI+....
31
by: Jamie Burns | last post by:
Hello, I am writing a client / server application. There is 1 server, and many clients. The server processes requests from each client, and typically creates and manipulates C++ objects on their...
17
by: Hazz | last post by:
In this sample code of ownerdraw drawmode, why does the '(ComboBox) sender' line of code need to be there in this event handler? Isn't cboFont passed via the managed heap, not the stack, into this...
10
by: Chet Cromer | last post by:
I am creating a set of base classes and sub classes to use throughout a program I'm developing. The base class represents a generic "lookup table" from my database that contains lists of things...
15
by: mr.peteryu | last post by:
Hi, Can someone explain the idea behind casting to an interface? For example: -> I have an IInterface that contains a Read() method. -> I have an object "obj" that implements IInterface. ...
16
by: the.duckman | last post by:
G'Day. Anybodey got an idea on this problem. Say I have a function object doCast(object obj, Type t); It's job is to cast the obect (obj) to a new type (t) and return it. Sounds so...
6
by: Jon | last post by:
Hi, When I try to compile the following generic class, the compiler gives me many errors "Cannot conver type '...' to '...'" on the lines indicated. Besides, the C# compiler gives me errors even...
15
by: Lloyd Dupont | last post by:
Don't mistake generic type for what you would like them to be!! IFoo<Ahas nothing in common with IFoo<B>! They are completely different type create dynamically at runtime. What you ask is a...
2
by: =?Utf-8?B?Unlhbg==?= | last post by:
Hi, How can I get around runtime error that says I can not explicit cast List<SubClassto ICollection<Class>? Generic List inhertis generic ICollection and Subclass inherits Class, then...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.