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