chris_dudley
Member
- Joined
- Mar 31, 2008
- Messages
- 7
- Programming Experience
- 5-10
Hi all,
Hope someone can help! Am struggling with a few of the more complex elements of .Net at the moment and would like some advice. It's a bit wordy, but I hope it's clear!
In my object model for my under-development system, I have implemented collections where appropriate as generic collections, for example (not a real sample) a Customer class might have collections like this:
Public ReadOnly Property Invoices() As Collection(Of Invoice)
Public ReadOnly Property Addresses() As Collection(Of Address)
Public ReadOnly Property Users() As Collection(Of User)
Previously, cobbling systems together, I've used Arraylists, but I like the strong typing that generics gives you. This is all fine, but I'd like to be able to code certain interface elements to render these collections in a generic manner, so that I only have to write the code once. The gridview will only use a limited number of pre-defined properties. I wrote a user control, "Recordselector" containing a gridview to do this rendering (it's a simple, clean gridview that looks a bit like the outlook interface). Again, previously, using arraylists, this was easy, as I'd just pass the arraylist to the user control and it would be able to do all the binding. Then I'd be able to reuse the control for any properties of type Arraylist that held objects that implemented Id, Title, and Subtitle properties.
These days I'm trying to do things "properly". So, I thought I could start by defining an interface, IRecordSelectable, that defined Id, Title and Subtitle properties. Then, any class that might be passed to the Recordselector user control would have to implement this interface. This is all well and good, but I'm stumped trying to write the functions/properties in the user control that would allow me to pass in a generic collection of any type of object. I *thought* that all I'd have to do is specify the interface in the generic collection being passed in, like so:
(this is a property of my Recordselector user control)
Public Property Recordset() As Collection(Of IRecordselector)
But no! Even though any collection I pass to this is a collection of objects that implement the IRecordselector interface, I get a "cannot convert" error message.
A solution is to define my collections as being "Of Object", but then it's pointless to use the generics.
Have I totally misunderstood Interfaces (and, for that matter, generics)?!
If anyone can shed any light, that would be great.
Cheers
Chris
--------------------------------------------------------------------------------
Really ought to proof read *before* posting and not after, but where I've put:
"IRecordselector"
near the bottom a couple of times I of course meant
"IRecordselectable"
ie. the name of my interface.
Cheers!
Hope someone can help! Am struggling with a few of the more complex elements of .Net at the moment and would like some advice. It's a bit wordy, but I hope it's clear!
In my object model for my under-development system, I have implemented collections where appropriate as generic collections, for example (not a real sample) a Customer class might have collections like this:
Public ReadOnly Property Invoices() As Collection(Of Invoice)
Public ReadOnly Property Addresses() As Collection(Of Address)
Public ReadOnly Property Users() As Collection(Of User)
Previously, cobbling systems together, I've used Arraylists, but I like the strong typing that generics gives you. This is all fine, but I'd like to be able to code certain interface elements to render these collections in a generic manner, so that I only have to write the code once. The gridview will only use a limited number of pre-defined properties. I wrote a user control, "Recordselector" containing a gridview to do this rendering (it's a simple, clean gridview that looks a bit like the outlook interface). Again, previously, using arraylists, this was easy, as I'd just pass the arraylist to the user control and it would be able to do all the binding. Then I'd be able to reuse the control for any properties of type Arraylist that held objects that implemented Id, Title, and Subtitle properties.
These days I'm trying to do things "properly". So, I thought I could start by defining an interface, IRecordSelectable, that defined Id, Title and Subtitle properties. Then, any class that might be passed to the Recordselector user control would have to implement this interface. This is all well and good, but I'm stumped trying to write the functions/properties in the user control that would allow me to pass in a generic collection of any type of object. I *thought* that all I'd have to do is specify the interface in the generic collection being passed in, like so:
(this is a property of my Recordselector user control)
Public Property Recordset() As Collection(Of IRecordselector)
But no! Even though any collection I pass to this is a collection of objects that implement the IRecordselector interface, I get a "cannot convert" error message.
A solution is to define my collections as being "Of Object", but then it's pointless to use the generics.
Have I totally misunderstood Interfaces (and, for that matter, generics)?!
If anyone can shed any light, that would be great.
Cheers
Chris
--------------------------------------------------------------------------------
Really ought to proof read *before* posting and not after, but where I've put:
"IRecordselector"
near the bottom a couple of times I of course meant
"IRecordselectable"
ie. the name of my interface.
Cheers!