Custom List casting

G

Goran Djuranovic

Hi all,
I have a custom made list (MyList) that inherits from CollectionBase. Is it possible to prohibit the casting of that list into other objects? I would like to get an error at design-time.

For example,

Dim myList As New MyList
Dim yourList As New IList = CType(MyList, IList) <---- prohibit this

Thanks
Goran Djuranovic
 
M

Marina Levit [MVP]

No, that is not going to happen. Because MyList is an IList - that is perfectly legal. I don't see how you can make the compiler think it is not.

However, that exact line you typed in will be prohibited anyway, because you can't instantiate a variable with New, and yet assign it a value. Not to mention IList is an interface and thus has no constructor and is not something you can instantiate.
Hi all,
I have a custom made list (MyList) that inherits from CollectionBase. Is it possible to prohibit the casting of that list into other objects? I would like to get an error at design-time.

For example,

Dim myList As New MyList
Dim yourList As New IList = CType(MyList, IList) <---- prohibit this

Thanks
Goran Djuranovic
 
H

Herfried K. Wagner [MVP]

Goran Djuranovic said:
I have a custom made list (MyList) that inherits from CollectionBase. Is it
possible to prohibit the casting of that list into other objects? I would
like to get an error at design-time.

For example,

Dim myList As New MyList
Dim yourList As New IList = CType(MyList, IList) <---- prohibit this

No, you cannot avoid this. That's how subtyping and interface
implementation are intended to work and that's why interfaces are used :).
 
G

Goran Djuranovic

Thanks both for you comments. The original problem was to create a custom type-safe list of some objects (f.e., Cars), and have it be able to accept only Car objects. But, it looks like that list could be cast(ed) into IList and then passed Object type, which would make it fail at run time. I know type-safe lists are implemented in VB 8, but thought the same coulb be done in VB 7.

Goran Djuranovic
Hi all,
I have a custom made list (MyList) that inherits from CollectionBase. Is it possible to prohibit the casting of that list into other objects? I would like to get an error at design-time.

For example,

Dim myList As New MyList
Dim yourList As New IList = CType(MyList, IList) <---- prohibit this

Thanks
Goran Djuranovic
 
G

Goran Djuranovic

Well, right after I posted my previous post, I ran into this:

"You can bypass the "Typed interface" of the collection very simply.. All you need to do is use the IList interface which allows .Add, .Insert etc for type object.." As I said in my previous post.

This can be prevented by Overriding OnInsert and OnSet. Both of these methods allow you the opertunity of raising an exception if the incorrect datatype
is passed in through the IList interface...

It just makes the whole colelction a little safer...

Eddie de bear
MCSD "

Will try it tomorrow.
GD
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top