ArrayList property: requiring type for elements

  • Thread starter Thread starter Jeroen
  • Start date Start date
J

Jeroen

I've been searching for this answer for some time now but I either
can't find the correct search in this group, or the answer's not there
yet :P Here's the problem:

I'm specifying an interface, that will have an ArrayList property (at
least, I'd very much *like* to use such a property). The only problem
is: I want the list only to accept string objects, nothing else. Can I
do this (or something alternative) in the interface? What's the
nice/'correct' solution?

This is what I have so far:

public interface ISomeInterface
{
// This list should only accept strings, but now it accepts anything.
public System.Collections.ArrayList ListWithOnlyStringObjects
{
get;
set;
}
}
 
Jeroen said:
I've been searching for this answer for some time now but I either
can't find the correct search in this group, or the answer's not there
yet :P Here's the problem:

I'm specifying an interface, that will have an ArrayList property (at
least, I'd very much *like* to use such a property). The only problem
is: I want the list only to accept string objects, nothing else. Can I
do this (or something alternative) in the interface? What's the
nice/'correct' solution?

This is what I have so far:

public interface ISomeInterface
{
// This list should only accept strings, but now it accepts anything.
public System.Collections.ArrayList ListWithOnlyStringObjects
{
get;
set;
}
}

Can you use generics? For example from System.Collections.Generic you could
use a List<string>.

List<string> myStringList = new List<string>();
 
propably you have to write one of your own arraylist by extending the
existing System.Collection.Arraylist. Also you have to override some of
existing functions.. like method "add" and "addrange" as and when it is
appropraite.

Then you may use your custom array list as a property in ur interface..

else.. you have to do a custom implimentation at the implimentation cliass
like

public System.Collections.ArrayList ListWithOnlyStringObjects
{
get
{
return the listWithOnlyStringObjects;
}
set
{
listWithOnlyStringObjects = ConvertArrayListToString(value);
}
}
private System.Collections.ArrayList listWithOnlyStringObjects = new ....

Finaly: Please note that these answers heavilly depend on your requirements,
the infomation given seems not fully explaining the objective and
requireements..

Nirosh
 
Thanks for the response.

Generics indeed seem to be what I need. Unfortunately we're still using
..NET 2003 (upgrading soon though (hopefully)).

As for the intended use: my class implementing the interface will have
a property that has a simple list of usernames involved.

The whole build-your-own-arraylist thing gets me where I need to be I
guess, I was just hoping there was a c# language construct that would
do the trick, but then again, i guess that's generics :'(.
 
Hi,


Champika Nirosh said:
propably you have to write one of your own arraylist by extending the
existing System.Collection.Arraylist. Also you have to override some of
existing functions.. like method "add" and "addrange" as and when it is
appropraite.

I dont think this is the best way to go, ArrayList is intended to be used
for loose types collections, if you extend it there is no garantee that
somebody cannot insert a non string in y our list

The way to go (excepting generics ) is a strong typed collection derived
from CollectionBase
 
since he is providing the interface, I don't think that " no garantee that
somebody cannot insert a non string in your list" is a valid statement..

but if you are talking about the bestest way.. then I think he can essily
replace System.Collections.ArrayList with a string[] since he seems not
needing that dynamic expandability and is dealing with strong typed data....
at least according to the way he has defined his interface..

Nirosh.
 
Thanks for the response.

Generics indeed seem to be what I need. Unfortunately we're still using
.NET 2003 (upgrading soon though (hopefully)).

As for the intended use: my class implementing the interface will have
a property that has a simple list of usernames involved.

The whole build-your-own-arraylist thing gets me where I need to be I
guess, I was just hoping there was a c# language construct that would
do the trick, but then again, i guess that's generics :'(.

Create a method called

public void AddToArrayList(string str)
{
theArrayList.Add(str);
}

If in your code an attempt is made to add anything other than a string the
compiler will give you an error message.

Kind of a round about way of doing what you want, but doesn't require extending
the ArrayList object.


Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
 
Hi,


Jeroen said:
Thanks for the response.

Generics indeed seem to be what I need. Unfortunately we're still using
.NET 2003 (upgrading soon though (hopefully)).


You always can extend CollectionBase and create a strong typed collection

Now, in your case you have System.Collection.Specialized.StringCollection
( as I mentioned in the other post)
 
Hi,

Simply use System.Collection.Specialized.StringCollection
 

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

Back
Top