473,511 Members | 16,888 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 5718
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(object value) {
if (value is MyType) {
return Add(value as MyType);
} else {
throw new ArgumentException();
}
}
}

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 ArgumentException at
runtime.
Jul 19 '05 #2
Ugh, my mistake - the second method's seiganture should read:

int IList.Add(object 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*****@pacbell.net> wrote in message
news:es**************@tk2msftngp13.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(object value) {
if (value is MyType) {
return Add(value as MyType);
} else {
throw new ArgumentException();
}
}
}

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 ArgumentException 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...
2
2363
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,...
4
10274
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...
2
2047
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...
0
1995
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...
5
3175
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...
5
1583
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...
20
5947
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...
3
2034
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
7242
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,...
0
7355
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,...
0
7423
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...
1
5066
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4737
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...
0
3225
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...
0
3213
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
781
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
447
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...

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.