473,396 Members | 1,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

ArrayList property: requiring type for elements

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;
}
}

Oct 18 '06 #1
8 3325

"Jeroen" <me******@gmail.comskrev i en meddelelse
news:11*********************@m7g2000cwm.googlegrou ps.com...
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<stringmyStringList = new List<string>();
Oct 18 '06 #2
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

"Jeroen" <me******@gmail.comwrote in message
news:11*********************@m7g2000cwm.googlegrou ps.com...
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;
}
}

Oct 18 '06 #3
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 :'(.

Oct 18 '06 #4
Hi,
"Champika Nirosh" <te**@tc.comwrote in message
news:ed**************@TK2MSFTNGP03.phx.gbl...
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

--
Ignacio Machin
machin AT laceupsolutions.com
Oct 18 '06 #5
On 18 Oct 2006 03:19:23 -0700, "Jeroen" <me******@gmail.comwrote:
>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
Oct 18 '06 #6
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.

"Ignacio Machin ( .NET/ C# MVP )" <machin TA laceupsolutions.comwrote in
message news:Oq**************@TK2MSFTNGP03.phx.gbl...
Hi,
"Champika Nirosh" <te**@tc.comwrote in message
news:ed**************@TK2MSFTNGP03.phx.gbl...
>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

--
Ignacio Machin
machin AT laceupsolutions.com

Oct 18 '06 #7
Hi,
"Jeroen" <me******@gmail.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
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)
Oct 18 '06 #8
Hi,

Simply use System.Collection.Specialized.StringCollection
--
Ignacio Machin
machin AT laceupsolutions.com
"Jeroen" <me******@gmail.comwrote in message
news:11*********************@m7g2000cwm.googlegrou ps.com...
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;
}
}

Oct 18 '06 #9

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

Similar topics

1
by: Jamus Sprinson | last post by:
Before I continue, I'm going to begin by saying I'm not by any means an expert- I've been using .NET with C# for about 4 months now, and basically just learning by example and docs. A game...
3
by: JJ | last post by:
Hi, I have created an Arraylist object from an Arraylist class. I added rows to the arraylist object and I need to find a particular record in my arraylist. How do I do this? Also if I was in...
2
by: D. Shane Fowlkes | last post by:
I've been reading up on Arrays in ASP.NET. I'm going to create an two dimensional array of some type to contain 5 columns but a variable amount of rows. I read up on the ArrayList function and...
2
by: Ric | last post by:
Please forgive me if i dont explain this situation correctly. i'll try to do my best. because the arraylist.add method only accepts one argument, i created an object with several public...
6
by: rdi | last post by:
I have 3 classes: mailBox, mailFilter & mailFilterSet I then have an arraylist. Each element in the array is of type mailBox (variable name is mbox). One item of the mailbox class is an...
5
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...
3
by: Dennis | last post by:
I am trying to bind a DataGrid Control (named DataGrid1) to an array list containing elements of a sturcture type (named TestDataGrid). From what I've read, it appears possible but the following...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
0
by: Jeremy Chapman | last post by:
I have included below virtually all the code to a control I'm trying to build. My issue is that an array list property in my control does not get persisted properly to the aspx page code in design...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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...
0
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...
0
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...

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.