473,788 Members | 2,893 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3733
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*****@discus sions.microsoft .com> wrote in message
news:26******** *************** ***********@mic rosoft.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 OnInsertComplet e 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 NameObjectColle ctionBase

/claes

"Ian Gore" <Ia*****@discus sions.microsoft .com> wrote in message
news:26******** *************** ***********@mic rosoft.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
3988
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)); inventory.Add(new Beer ("Budweiser", "Anheuser-Busch", 1000)); inventory.Add(new Beer("Mulholland Rain", "City Brewers", 113));
2
2060
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 class has several public properties like FirstName, LastName, and Gender. What steps would it take to allow the collection to be sorted in multiple ways when bound to a DataGrid? In the past, I used the sort property of a DataView containing a...
4
2521
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 where an object of the contained type is returned (instead of just the standard DictionaryEntry object). - only allow objects of type MyObj to be added to the collection - return a MyObj type in the For-each loop What's the best way to do this?
0
1453
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 the 2nd property is a strongly typed collection class (myCollectionProperty). The collection contains a simple class (myCustomClass) which has 1 text property (TextProperty).
20
5988
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 arraylist then I have to handle this with a sort method that uses comparer. I can reference the properties of the Arraylist directly such as dim mylist as new FrameList mylist.Add(new FrameStructure) mylist(0).first = "blabla..." mylist(0).second...
1
1081
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 based on collectionbase that are representations of table data in my database. I have no issue creating the collection as that is pretty straightforward, I am looking for more advanced topics such as data validation, event handling and and...
5
4233
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 from collection base, but use an internal collection, to hold the objects and then implement IEnumerator, see example below,
1
1272
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 create a strongly typed collection reading from an XML file. The reason I need this, is because I'm implementing a custom solution for a multilingual website and I was thinking in use this so that, I can do something like :
3
2050
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 object into a more strongly typed class which knows what properties to expect. The basic top level class is as follows: using System;
0
9656
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9498
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10366
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9967
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5399
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4070
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3674
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.