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
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