is object part of CollectionBase
-
hi guys can i get Collection from object if it is part of it? code should look something like this
public class SomeCollection : CollectionBase
{
public void Add(SomeClass sc)
{
this.List.Add(sc);
}
}class SomeClass
{
public bool AmIPartOfCollection
{
get
{
//return false or true
}
}public SomeCollection MyCollection
{
get
{
//if this object is part of SomeCollection return his SomeCollection
}
}
}void Main()
{
SomeClass temp = new SomeClass();
//temp.AmIPartOfCollection - want to get false
//temp.MyCollection - want to get nullSomeCollection sc = new SomeCollection(); sc.Add(temp); //temp.AmIPartOfCollection - want to get true //temp.MyCollection - want to get reference to "sc" object
}
-
hi guys can i get Collection from object if it is part of it? code should look something like this
public class SomeCollection : CollectionBase
{
public void Add(SomeClass sc)
{
this.List.Add(sc);
}
}class SomeClass
{
public bool AmIPartOfCollection
{
get
{
//return false or true
}
}public SomeCollection MyCollection
{
get
{
//if this object is part of SomeCollection return his SomeCollection
}
}
}void Main()
{
SomeClass temp = new SomeClass();
//temp.AmIPartOfCollection - want to get false
//temp.MyCollection - want to get nullSomeCollection sc = new SomeCollection(); sc.Add(temp); //temp.AmIPartOfCollection - want to get true //temp.MyCollection - want to get reference to "sc" object
}
Not with a standard collection, no. It's pretty straightforward to write a specialised collection and interface for member objects to do this. However, in most situations you know roughly where the object must be, so you can use targetCollection.Contains.
-
hi guys can i get Collection from object if it is part of it? code should look something like this
public class SomeCollection : CollectionBase
{
public void Add(SomeClass sc)
{
this.List.Add(sc);
}
}class SomeClass
{
public bool AmIPartOfCollection
{
get
{
//return false or true
}
}public SomeCollection MyCollection
{
get
{
//if this object is part of SomeCollection return his SomeCollection
}
}
}void Main()
{
SomeClass temp = new SomeClass();
//temp.AmIPartOfCollection - want to get false
//temp.MyCollection - want to get nullSomeCollection sc = new SomeCollection(); sc.Add(temp); //temp.AmIPartOfCollection - want to get true //temp.MyCollection - want to get reference to "sc" object
}
Hanzaplast wrote:
public SomeCollection MyCollection
Hanzaplast wrote:
//if this object is part of SomeCollection return his SomeCollection
you can't do that in general as an object may be part of more than one collection. :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
-
Hanzaplast wrote:
public SomeCollection MyCollection
Hanzaplast wrote:
//if this object is part of SomeCollection return his SomeCollection
you can't do that in general as an object may be part of more than one collection. :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
Damn! Beat me to it... My 5.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
Damn! Beat me to it... My 5.
A guide to posting questions on CodeProject[^]
Dave Kreskowiakyou could have suggested a
public List MyCollections {get;}
:-D
Luc Pattyn [My Articles] Nil Volentibus Arduum
-
you could have suggested a
public List MyCollections {get;}
:-D
Luc Pattyn [My Articles] Nil Volentibus Arduum
The inference I take from your answer, Luc (the answer seems "intuitively" correct to me), is that you could then iterate MyCollections, and, for each Collection in MyCollection, test for the presence of a specifc object using 'Contains. This solution also seeming to allow for the idea that you could also, then ... in the case of an object being a member of more than one Collection ... return a List of all Collections it was a member in, as well. However, I do think the OP's code is pretty weird. If you are going to add any type of object to a collection: that would seem to indicate a need to me for a generic (even "dynamic," late-bound ?) solution. thanks, Bill
"It is the mark of an educated mind to be able to entertain a thought without accepting it." Aristotle