473,657 Members | 2,413 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

OO Design Question - Strongly typed ArrayList

I'm trying to figure out the "best" way to implement a strongly typed
ArrayList. Using inheritance is one approach. It has the advantage that I
only have to write overrides for the Add method and indexer. There are
problems with this however. First the indexer "override" has a different
return type. This doesn't cause a compile time error, but using the indexer
doesn't seem to produce a properly typed expression. The Add override must
have an arg of type object so the method doesn't reflect the strong
typing -- until run time when the my hand coded type check is run. I've
added an Add overload that uses a strongly typed arg, but the original
method still appears in the command completion popup.

An alternative approach would be to use composition, but this requires that
I implement wrapper methods for all of the ArrayList methods that I want to
make available in the strongly typed ArrayList class.

I think the latter (composition) approach provides the best solution in
terms of providing a truly strongly typed object, but am bothered by the
need for the "duplicate" methods.

Is there another alternatives, suggestions?

Thanks in advance,
Bill
Jul 19 '05 #1
4 5726
Hello,

If you want a strongly-typed collection, the easiest thing to do is inherit
from CollectionBase and verify your values in the OnVerify method - but you
knew that already. ;-) However, if you want type-safe member signatures,
you'll need to use composition. Try this:

class MyCollection : IList {
...
public int Add(MyType value) {
...
}
...
int IList:Add(objec t value) {
if (value is MyType) {
return Add(value as MyType);
} else {
throw new ArgumentExcepti on();
}
}
}

Using this notation, Add(object) is only there to satisfy the interface
implementation; it will only be visible if the collection is cast to IList.
Attempting to add an instance of any type other than MyType to the
collection will cause a compile-time error (unless the type of said instance
can be implicitly converted to MyType), as long as you're working with a
MyCollection reference. If the collection is cast to IList at some point,
adding an object of an inappropriate type will throw an ArgumentExcepti on at
runtime.
Jul 19 '05 #2
Ugh, my mistake - the second method's seiganture should read:

int IList.Add(objec t value)

Note the period in place of the colin.
Jul 19 '05 #3
Erm, signature. I am not having a good night. >.<
Jul 19 '05 #4
Omni
Thanks for the suggestion. This is pretty much what I've already tried
(via inheritance). I guess that using an interface keeps the IList.Add
method from showing up in the pop up though, right? The OnVerify approach
doesn't solve the compile-time problem.

Thanks again,
Bill
"Omni" <om*****@pacbel l.net> wrote in message
news:es******** ******@tk2msftn gp13.phx.gbl...
Hello,

If you want a strongly-typed collection, the easiest thing to do is inherit from CollectionBase and verify your values in the OnVerify method - but you knew that already. ;-) However, if you want type-safe member signatures,
you'll need to use composition. Try this:

class MyCollection : IList {
...
public int Add(MyType value) {
...
}
...
int IList:Add(objec t value) {
if (value is MyType) {
return Add(value as MyType);
} else {
throw new ArgumentExcepti on();
}
}
}

Using this notation, Add(object) is only there to satisfy the interface
implementation; it will only be visible if the collection is cast to IList. Attempting to add an instance of any type other than MyType to the
collection will cause a compile-time error (unless the type of said instance can be implicitly converted to MyType), as long as you're working with a
MyCollection reference. If the collection is cast to IList at some point,
adding an object of an inappropriate type will throw an ArgumentExcepti on at runtime.

Jul 19 '05 #5

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

Similar topics

4
294
by: Bill Cohagan | last post by:
I'm trying to figure out the "best" way to implement a strongly typed ArrayList. Using inheritance is one approach. It has the advantage that I only have to write overrides for the Add method and indexer. There are problems with this however. First the indexer "override" has a different return type. This doesn't cause a compile time error, but using the indexer doesn't seem to produce a properly typed expression. The Add override must...
2
2372
by: Mark | last post by:
Just wanted to confirm that my understanding of a strongly typed language is correct: 1. .NET is a strongly typed language because each variable / field must be declared a specific type (String, Int, Float, etc...) 2. VB 6 and VBScript were not strongly typed because both allowed you to ... dim blah .... which creates a variant.
4
10277
by: Dot net work | last post by:
I thought I'd be clever and create my own strongly typed array list, because I wanted to be sure that when an object was added to the arraylist, it would *only* be an object of a class that I had made. But after I got it all working, I realised that the inheritance mechanism also allows access to the inherited Add methods, and these allow you to add objects of any type! I don't seem to have achieved what I tried to do.
2
2057
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...
0
2005
by: Uchiha Jax | last post by:
When using a strongly typed dataset (generated from the Visual Studio IDE from an XSD file) and databinding I get a really odd error when binding to both a combox and a datetimepicker. I bind like the following: //// tDs is the strongly typed dataset, //// tDs.TheTask returns a strongly typed row from one of the tables. /// GetBindingTaskTypes() returns the types I want to use as a datasource in an arraylist.
5
3179
by: Dennis | last post by:
sIs there anyway to Strongly type an arraylist. I have an arraylist where object in the arraylist are of a type Structure, say "st" structure. I would like to reference elements of the structure in the arraylist where "ar" is my array list and "element 1 is an element in "st" like: if ar(1).element1= xx then so I don't have to use Ctype each time i want to get an element of the arraylist structures. --
5
1593
by: Dennis | last post by:
NOTE: This is a COMMENT...not a QUESTION: For the newbies, I've been messing around with strongly typed ArrayLists so I don't have to type so many "DirectCast". The following works great where "myClass" is the classs that each ArrayList Element will contain: dim myarraylist as typedArrayList = new typedArrayList Public Class typedArrayList Inherits ArrayList Default Public Shadows Property Item(ByVal Index As Integer) As myClass
20
5968
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...
3
2043
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
8315
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
8734
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8508
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8608
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
5633
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4164
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
4323
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1962
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1627
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.