472,799 Members | 1,623 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,799 software developers and data experts.

Strongly Typed Collections and the Collection Object

Hi,

I'm relatively new to VB.NET so I'd be grateful if someone could point out what I don't understand here...

1) Creating a strongly typed collection by inheriting CollectionBase.

This is covered in most of the books I've read. Basically you inherit CollectionBase and then add methods (say Add) that takes your types as a parameter and then calls List.Add from the base class. OK.

However CollectionBase exposes the IList interface as an explict interface. I want to be able to override the Add method in the IList interface. Why? Because if someone casts my collection to an IList, they can bypass my strongly typed implementation. (My implementation of IList.Add would check the type of the passed object and throw an exception if it was wrong).

If I try saying "Implements IList" in my subclass it's rejected since CollectionBase implements it. If I try to override or set my own method to Implement IList.Add then it's rejected because my subclass doesn't implement IList!!!

What have I missed? How can I override the implementation of the IList interface in my subclass

2) Creating a strongly typed collection using the Collection Object.

CollectionBase does not support the "key" type lookup for collections (like VB6 collections). In order to have a key-based collection I need to use the Collection object (I think).

I want to have a strongly typed collection by overriding the Collection object methods with my own typed methods. However, I can't inherit the Collection object since it's marked as NotInheritable.

So I have to implement my own class that "front-ends" the Collection object in the same way as was done in VB6.

Again, have I missed something? Is there a key-based collection class that I can inherit?

Thanks.
Ian Gore
Nov 20 '05 #1
2 3656
As For point 1.
You could implement empty stubs in the SubClass, and then from each sub call
the base function passing the parameters on, this should work tho I have not
tried it.

As For Point2.
You should check out the Specialized Collection namespace, these may be
inheritable and you could make use of them.

HTH - OHM

"Ian Gore" <Ia*****@discussions.microsoft.com> wrote in message
news:26**********************************@microsof t.com...
Hi,

I'm relatively new to VB.NET so I'd be grateful if someone could point out what I don't understand here...
1) Creating a strongly typed collection by inheriting CollectionBase.

This is covered in most of the books I've read. Basically you inherit CollectionBase and then add methods (say Add) that takes your types as a
parameter and then calls List.Add from the base class. OK.
However CollectionBase exposes the IList interface as an explict interface. I want to be able to override the Add method in the IList
interface. Why? Because if someone casts my collection to an IList, they
can bypass my strongly typed implementation. (My implementation of IList.Add
would check the type of the passed object and throw an exception if it was
wrong).
If I try saying "Implements IList" in my subclass it's rejected since CollectionBase implements it. If I try to override or set my own method to
Implement IList.Add then it's rejected because my subclass doesn't implement
IList!!!
What have I missed? How can I override the implementation of the IList interface in my subclass
2) Creating a strongly typed collection using the Collection Object.

CollectionBase does not support the "key" type lookup for collections (like VB6 collections). In order to have a key-based collection I need to
use the Collection object (I think).
I want to have a strongly typed collection by overriding the Collection object methods with my own typed methods. However, I can't inherit the
Collection object since it's marked as NotInheritable.
So I have to implement my own class that "front-ends" the Collection object in the same way as was done in VB6.
Again, have I missed something? Is there a key-based collection class that I can inherit?
Thanks.
Ian Gore

Nov 20 '05 #2
1. Override OnInsertComplete and throw an exception from
there if the type is wrong. This method is called from inside
the internal implementation of IList.Add and IList.Insert
2. Try NameObjectCollectionBase

/claes

"Ian Gore" <Ia*****@discussions.microsoft.com> wrote in message
news:26**********************************@microsof t.com...
Hi,

I'm relatively new to VB.NET so I'd be grateful if someone could point out what I don't understand here...
1) Creating a strongly typed collection by inheriting CollectionBase.

This is covered in most of the books I've read. Basically you inherit CollectionBase and then add methods (say Add) that takes your types as a
parameter and then calls List.Add from the base class. OK.
However CollectionBase exposes the IList interface as an explict interface. I want to be able to override the Add method in the IList
interface. Why? Because if someone casts my collection to an IList, they
can bypass my strongly typed implementation. (My implementation of IList.Add
would check the type of the passed object and throw an exception if it was
wrong).
If I try saying "Implements IList" in my subclass it's rejected since CollectionBase implements it. If I try to override or set my own method to
Implement IList.Add then it's rejected because my subclass doesn't implement
IList!!!
What have I missed? How can I override the implementation of the IList interface in my subclass
2) Creating a strongly typed collection using the Collection Object.

CollectionBase does not support the "key" type lookup for collections (like VB6 collections). In order to have a key-based collection I need to
use the Collection object (I think).
I want to have a strongly typed collection by overriding the Collection object methods with my own typed methods. However, I can't inherit the
Collection object since it's marked as NotInheritable.
So I have to implement my own class that "front-ends" the Collection object in the same way as was done in VB6.
Again, have I missed something? Is there a key-based collection class that I can inherit?
Thanks.
Ian Gore

Nov 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Sefa Sevtekin | last post by:
I am trying to bind a strongly-typed collection to a data grid on a windows form like: BeerInventory inventory = new BeerInventory(); inventory.Add(new Beer ("Hamms", "Hamms Brewing Co.", 3));...
2
by: Mark | last post by:
Assume you have a strongly typed collection of a class called Person. The strongly typed collection implements IEnumerable so it can be databound to a server control like a DataGrid. The Person...
4
by: Michael K. Walter | last post by:
I'd like to create a strongly-typed collection of objects that are indexed by a string value (key is a string). I'd also like to allow users to iterate over the collection using a For-each loop...
0
by: Jordan Bowness | last post by:
I make a similar post in another newsgroup, but this example is simplified somewhat. I have a component (cmpMyComponent) with 2 properties. The 1st property is a string value (Description) and...
20
by: Dennis | last post by:
I use the following code for a strongly typed arraylist and it works great. However, I was wondering if this is the proper way to do it. I realize that if I want to implement sorting of the...
1
by: ECathell | last post by:
Does anyone have links to any good tutorials on creating strongly typed collections? Especially on manipulating the data that is in the collection? I am using strongly-typed collection objects...
5
by: Simon | last post by:
Hi all, I am writing a windows application using vb.net on the 1.1 framework. We have in the application, some strongly typed collections that have been written as classes that do not inherit...
1
by: craigkenisston | last post by:
Is that possible ? Can I create a strongly typed collection from an XML file? Sorry, if this is too dumb, but since .net does it when you use certain data objects, I was wondering whether I could...
3
by: Simon Hart | last post by:
Hi, I am trying to implement some functionality as seen in MS CRM 3.0 whereby a basic Xml is deserialized into an object which contains properties. What I want to do from here is; cast the basic...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.