Question Listbox, Objects, Inheritance and Polymorphism

bentleyr

New member
Joined
Jan 31, 2012
Messages
3
Programming Experience
Beginner
I wasn't quite sure how to label this but that should cover it all.

I am using Linq to Sql classes to query a SQL Server table. That part is working fine.

I'd like to add the records to a listbox and not just text. I can do this and it works. To get it to display something meaningful in the listbox, I have to override the ToString() function.

So, I've taken the base class tblVendor and created a new sub class tblVendorListObject that inherits tblVendor and overrides ToString to return Me.vendorName.

I then use a foreach loop to iterate through the records returned by the query. Next, I create a new instance of tblVendorListObject and attempt to assign the current item in the foreach loop to it.

This is where I seem to be failing miserably. I interpret everything I've read as to indicate that because I'm trying to convert a base class to a sub class I should be able to simply assign them. This doesn't work. I've paused at the debug.print command and tried to use CType through the immediate window and get the message "Cannot convert to 'Class tblVendorListObject'."

Here's the code (simple) in the sub class:

Public Class tblVendorListObject
Inherits tblVendor

Public Overrides Function ToString() As String
Return Me.VendorName
End Function ' ToString

End Class

The conversion occurs (attempts to at least) in this code:

Private Sub populateLstVendors()

Dim db As New dcVendorsDataContext

Dim vendors = From v In db.tblVendors
Order By v.VendorName _
Select v

For Each v As tblVendor In vendors
Dim vendorObject As New tblVendorListObject()
vendorObject = CType(v, tblVendorListObject)
Debug.Print("")
lstVendors.Items.Add(vendorObject)

Next

lstVendors.SelectedIndex = 0

End Sub

All of the individual pieces seem to work except for the conversion. Is there something wrong with the inheritance idea?

Thanks ahead of time! I am really stumped on this.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,321
Location
Sydney, Australia
Programming Experience
10+
To get it to display something meaningful in the listbox, I have to override the ToString() function.
No you don't. You simply set the DisplayMember property of the ListBox to the name of the property or column you want displayed.
 

bentleyr

New member
Joined
Jan 31, 2012
Messages
3
Programming Experience
Beginner
Alright, I'll give this a whirl. There are some other points in which I'll need it to display some different columns concatenated into one string. To go this way, I'll have to see if Linq can combine strings into an aliased column somehow. Or, I'm back into overriding ToString().
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,321
Location
Sydney, Australia
Programming Experience
10+
You could use LINQ to generate an anonymous type with one property for the original object and another for the concatenated value, then bind that, e.g.
With myListControl
    .DisplayMember = "FullName"
    .ValueMember = "Person"
    .DataSource = people.Select(Function(p) New With {.Person = p, .FullName = String.Format("{0} {1}", p.GivenName, p.FamilyName)}).ToArray()
End With
 

bentleyr

New member
Joined
Jan 31, 2012
Messages
3
Programming Experience
Beginner
Alright. Whatever you've done with Linq here is something that I haven't before. I don't have time at the moment but I will take a look at this. This looks interesting. Thanks!
 
Top Bottom