Type conversions question



Hi,
I hope this is the right forum for this question.

I am extending ICollection to create a Collection Type (say
MyCollection) wherein I can control the types of objects being added to the
collection. Thus, my interface now looks like this

public interface IMyCollection : ICollection
{
void Add (string toBeAdded);

void Add (IMyObject toBeAdded);
}

The implementation (MyCollection) internally stores the collection
members as types of object IMyObject, i.e. it has the ability to convert the
string (which can be added by means of the first method) to an instance of
IMyObject.

Now when, external members wish to access members of the collection
using a foreach, I wish to have the ability to return the object depending
upon the type wished by the client. In other words, if a client added a
member to a collection using the "stringized" method, she should be able to
access the member back as a string.

In other words
IMyCollection collection = <Somehow get this>;

collection.Add ("Member");
foreach (string collectionMember in collection)
{
// Process your stuff
}

The IEnumerator implementation of MyCollection cannot handle this as I
do not know the type specified for acceptance in the foreach loop. Thus, I
am always returning an object of type IMyObject as an enumerated item.

I tried using a TypeConverter implementation on the class that
implements IMyObject so that IMyObject can be converted back to a string.
However this does not seem to work.How do I make the loop check for type
convertibility?

Am I missing something here?

Thanks in advance,
Madhu


.



Relevant Pages

  • Re: Type conversions question
    ... I think your trying to invent generics through interfaces. ... > members as types of object IMyObject, i.e. it has the ability to convert ... > access the member back as a string. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: A question in the objects size.
    ... > int id; ... > String name; ... then the order in which the base and derived members occur could ... leeway in deciding how to arrange the member variables. ...
    (comp.lang.java.programmer)
  • Re: why use the sealed ?
    ... > I'll want to add functionality to something at a later date. ... > I sortof wish you could add functionality to (or replace members of) ... > exceeds past the end of the string. ... > application to be a Remoting Client. ...
    (microsoft.public.dotnet.languages.csharp)
  • Struts 1.2 - HTML:SELECT problem
    ... So the attribute members has data. ... private String staff_number; ... Session session = null; ...
    (comp.lang.java.programmer)
  • Re: Insert recordset list into e-mail message
    ... It looks like you are already setting up a SQL string to return ... you a recordset of the family members, but you are not using it. ... Then you have a list of members, one per line, that you can insert into your ... Graham Mandeno ...
    (microsoft.public.access.formscoding)

Quantcast