|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Why must I cast when I put type safe generic objects in the combo box?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 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 > "dmac" <dr***@drmcl.free-online.co.uk> wrote in message Just because you stuffed it with Bob's doesn't guarantee to the compiler 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. > that some other sub-genius hasn't come along and put something else in there. David "dmac" <dr***@drmcl.free-online.co.uk> schrieb: The return type of 'SelectedItem' is 'Object', which is the base type of all > myBob = DirectCast(Me.ComboBox1.SelectedItem, Bob) 'THIS is the > annoying bit 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/> 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 >
SQL UPDATE query help please
How to get a form's property value from a class? Russian text output Help needed in using FSO's, TextStreams, etc. --- Code Review and Advice requested How to identiy numerics in a string? Exception handling, Retry, Resume Next PDF Creation components ListView ListItems - can they be hidden? How to uniquely identify a process? adding listbox selected items |
|||||||||||||||||||||||