how to make a collection cl

M

mp

I've been reading all the help files and things i can find on collection
classes.
Can't find a simple intro for beginner level example in c# that's basic
enough i can understand
any sites or tutorials you know of would be appreciated.

If I have a class MoldPart
And I have a class Mold
Mold contains (has) MoldParts

one question in my mind:
(i'm not sure if it should be aggregation or composition - i think
composition)
i don't know how that idea affects implementation in C#

I'm not sure how to give Mold a property of a "group"(collection) of
MoldParts
such that i could do things like:
For each MoldPart part1 in Mold1.MoldParts
// part1.propertysomething ...
Next

-or-

Mold1.MoldParts.Add New MoldPart part1(name)

etc

i found something on implementing IEnumerable but it was quite complex and I
don't know if I need that for the above.

thanks for any pointers
mark
 
F

Family Tree Mike

I've been reading all the help files and things i can find on collection
classes.
Can't find a simple intro for beginner level example in c# that's basic
enough i can understand
any sites or tutorials you know of would be appreciated.

If I have a class MoldPart
And I have a class Mold
Mold contains (has) MoldParts

one question in my mind:
(i'm not sure if it should be aggregation or composition - i think
composition)
i don't know how that idea affects implementation in C#

I'm not sure how to give Mold a property of a "group"(collection) of
MoldParts
such that i could do things like:
For each MoldPart part1 in Mold1.MoldParts
// part1.propertysomething ...
Next

-or-

Mold1.MoldParts.Add New MoldPart part1(name)

etc

i found something on implementing IEnumerable but it was quite complex and I
don't know if I need that for the above.

thanks for any pointers
mark

If you have a collection class, you might do it like this:

public class Mold : List<MoldPart> {}

That would allow syntax like:
foreach (MoldPart p in MyMold)
// do something with "p"

That, however, simply means that a mold can be made up of any set of
0..N MoldParts, as a user could do this:

public Mold m = new Mold();
m.Add(new MoldPart());

Presumably you need to validate the MoldPart on adding it to the Mold,
to make sure your machine still works. I guess you could override the
Add method, but it's unusual to do that and not respect the add of an
item to the list.

Perhaps the more likely scenario is to have your list contain the list
of MoldParts as such:

public class Mold
{
List<MoldPart> Parts {get; set;}
public void AddPart(MoldPart p)
{
if (p is valid...) Parts.Add(p);
}
}
 
M

mp

great, that's much simpler than what i found.
i'd like to ask a few beginner questions inline below

Family Tree Mike said:
snip

If you have a collection class, you might do it like this:

public class Mold : List<MoldPart> {}

does that signature indicated that List<> is the base class of Mold?
so Mold is a 'kind of a ' List? or derived from List?

could Mold still have other properties and methods that a List may not?
in other words it would be based on List but not limited to List properties?
That would allow syntax like:
foreach (MoldPart p in MyMold)
// do something with "p"

That, however, simply means that a mold can be made up of any set of 0..N
MoldParts, as a user could do this:

public Mold m = new Mold();
m.Add(new MoldPart());

Presumably you need to validate the MoldPart on adding it to the Mold, to
make sure your machine still works. I guess you could override the Add
method, but it's unusual to do that and not respect the add of an item to
the list.

Perhaps the more likely scenario is to have your list contain the list of
MoldParts as such:

this looks more like what i imagined
public class Mold
{
List<MoldPart> Parts {get; set;}
public void AddPart(MoldPart p)
{
if (p is valid...) Parts.Add(p);
}
}

with that can i still do the following?
foreach (MoldPart p in MyMold.Parts)
// do something with "p"


apologies for the beginner questions
thanks
mark
 
F

Family Tree Mike

See inline...

great, that's much simpler than what i found.
i'd like to ask a few beginner questions inline below



does that signature indicated that List<> is the base class of Mold?
so Mold is a 'kind of a ' List? or derived from List?

could Mold still have other properties and methods that a List may not?
in other words it would be based on List but not limited to List properties?


Yes, Mold could be defined like this:

public class Mold : List<MoldPart>
{
public double Volume
{
get
{
// return some calculated value
}
}

public void RunMold()
{
// does some function...
}
}

this looks more like what i imagined

with that can i still do the following?

Well, you could do that so long as you make the Parts property public.
If you do that, you would not be prohibited from doing:

Mold m = new Mold();
m.Parts.Add(new MoldPart());

You would have to decide if you want to allow that. But yes, the
foreach code you posted is fine then.
apologies for the beginner questions
thanks
mark

It's always good to ask questions.
 
M

mp

Mike, thank you for your clear explanations.

Family Tree Mike said:
See inline...



Well, you could do that so long as you make the Parts property public. If
you do that, you would not be prohibited from doing:

Mold m = new Mold();
m.Parts.Add(new MoldPart());

You would have to decide if you want to allow that. But yes, the foreach
code you posted is fine then.


It's always good to ask questions.

Thanks, I do try and search the help and online first but sometimes it's
hard to find simple enough explanations or specific to my question info
I see what you mean about making Parts public and that not protecting the
insertion of parts.
in my case, i don't know yet if that would be a problem

i'm thinking of the Mold object as a container for parts whose size i will
calculate from an intitial object..

the initial object is the shape that will be "cast" in the mold.

the sizes of the parts will derive from the size and shape of the initial
object

public class Mold
{however a constructor should look like...i have to review that...
Mold (InitialObject) <--- create a new mold and pass it the initial
object to be made as arg to constructor
}

then internally each part will be created and size calculated based on
properties of InitialObject
so the access to the parts collection and iterating through it could just be
private to the code inside Mold class...

m_privateObj.Parts.Add( CreateNewSidePart (InitialObject));
m_privateObj.Parts.Add( CreateNewEndPart (InitialObject));
m_privateObj.Parts.Add( CreateNewBasePart (InitialObject));

{
List<MoldPart> Parts {get; set;}
public void AddPart(MoldPart p)
{
if (p is valid...) Parts.Add(p);
}
}

so the CreateNewSidePart (InitialObject)); function could look at the object
and figure out what size the sidepart has to be...etc

then the client code can just do something like
Mold thisMold = New Mold(initialObject);
//internal calculations
//then write out list of parts in some form(create series of files that
define each part)
thisMold.CreatePartsList;

so the CreatePartsList method would be the one to iterate the Parts
collection
For Each Part thisPart in privatePartCollection
WritePartFile(thisPart)
Next

does that make sense?
i need to find some basic syntax tutorials too.

thanks for your help
mark
 
F

Family Tree Mike

Mike, thank you for your clear explanations.




Thanks, I do try and search the help and online first but sometimes it's
hard to find simple enough explanations or specific to my question info
I see what you mean about making Parts public and that not protecting the
insertion of parts.
in my case, i don't know yet if that would be a problem

i'm thinking of the Mold object as a container for parts whose size i will
calculate from an intitial object..

the initial object is the shape that will be "cast" in the mold.

the sizes of the parts will derive from the size and shape of the initial
object

public class Mold
{however a constructor should look like...i have to review that...
Mold (InitialObject)<--- create a new mold and pass it the initial
object to be made as arg to constructor
}

then internally each part will be created and size calculated based on
properties of InitialObject
so the access to the parts collection and iterating through it could just be
private to the code inside Mold class...

m_privateObj.Parts.Add( CreateNewSidePart (InitialObject));
m_privateObj.Parts.Add( CreateNewEndPart (InitialObject));
m_privateObj.Parts.Add( CreateNewBasePart (InitialObject));

{
List<MoldPart> Parts {get; set;}
public void AddPart(MoldPart p)
{
if (p is valid...) Parts.Add(p);
}
}

so the CreateNewSidePart (InitialObject)); function could look at the object
and figure out what size the sidepart has to be...etc

then the client code can just do something like
Mold thisMold = New Mold(initialObject);
//internal calculations
//then write out list of parts in some form(create series of files that
define each part)
thisMold.CreatePartsList;

so the CreatePartsList method would be the one to iterate the Parts
collection
For Each Part thisPart in privatePartCollection
WritePartFile(thisPart)
Next

does that make sense?
i need to find some basic syntax tutorials too.

thanks for your help
mark

I think your approach does make sense. It keeps the problem of
validating the parts that comprise the mode out by having the
initialization occur in the mold itself.

As far as syntax tutorials, asking questions here, or even just
observing other message traffic is very good. Scan through some of the
walk-throughs in MSDN (http://msdn.microsoft.com), as that can help too.
 
A

Arne Vajhøj

If you have a collection class, you might do it like this:

public class Mold : List<MoldPart> {}

That would allow syntax like:
foreach (MoldPart p in MyMold)
// do something with "p"

That, however, simply means that a mold can be made up of any set of
0..N MoldParts, as a user could do this:

public Mold m = new Mold();
m.Add(new MoldPart());

Presumably you need to validate the MoldPart on adding it to the Mold,
to make sure your machine still works. I guess you could override the
Add method, but it's unusual to do that and not respect the add of an
item to the list.

Perhaps the more likely scenario is to have your list contain the list
of MoldParts as such:

public class Mold
{
List<MoldPart> Parts {get; set;}
public void AddPart(MoldPart p)
{
if (p is valid...) Parts.Add(p);
}
}

I will strongly recommend the second approach over the
first approach.

The first causes both OO way of thinking problems
(it is not a real "is a") and practical problems
(it inherit all the methods from List).

Arne
 
M

mp

Arne Vajhøj said:
I will strongly recommend the second approach over the
first approach.

The first causes both OO way of thinking problems
(it is not a real "is a") and practical problems
(it inherit all the methods from List).

Arne

yep, I agree
Thanks
Mark
 
P

Peter Duniho

mp said:
[...]
then the client code can just do something like
Mold thisMold = New Mold(initialObject);
//internal calculations
//then write out list of parts in some form(create series of files that
define each part)
thisMold.CreatePartsList;

so the CreatePartsList method would be the one to iterate the Parts
collection
For Each Part thisPart in privatePartCollection
WritePartFile(thisPart)
Next

Note that if you would like for code outside the Mold class to be able
to enumerate the individual parts, even as you protect that collection
from modification by outside code, you can accomplish that without
exposing the collection itself.

If you would random access to the collection, you'll probably want
something like this:

using System.Collections.ObjectModel;

class Mold
{
private List<MoldPart> _partsList;

public IList<MoldPart> Parts
{
get { return new ReadOnlyCollection<MoldPart>(_partsList); }
}
}

The ReadOnlyCollection<T> class simply wraps an existing IList<T>
implementation, hiding the original collection from client code, but
allowing any read-only operations on it.

Another approach, if you expect only ever to have the parts list
enumerated, is simply to return an IEnumerable<MoldPart>:

class Mold
{
private List<MoldPart> _partsList;

public IEnumerable<MoldPart> Parts
{
get
{
foreach (MoldPart part in _partsList)
{
yield return part;
}
}
}
}

This uses the iterator method syntax for the property getter; it's
essentially another way to wrap the original collection, this time in
the IEnumerable<T> implementation implicitly created by the compiler on
your behalf when you use the iterator syntax.

Pete
 
M

mp

thanks for that info, very nice
mark

Peter Duniho said:
mp said:
[...]
then the client code can just do something like
Mold thisMold = New Mold(initialObject);
//internal calculations
//then write out list of parts in some form(create series of files that
define each part)
thisMold.CreatePartsList;

so the CreatePartsList method would be the one to iterate the Parts
collection
For Each Part thisPart in privatePartCollection
WritePartFile(thisPart)
Next

Note that if you would like for code outside the Mold class to be able to
enumerate the individual parts, even as you protect that collection from
modification by outside code, you can accomplish that without exposing the
collection itself.

If you would random access to the collection, you'll probably want
something like this:

using System.Collections.ObjectModel;

class Mold
{
private List<MoldPart> _partsList;

public IList<MoldPart> Parts
{
get { return new ReadOnlyCollection<MoldPart>(_partsList); }
}
}

The ReadOnlyCollection<T> class simply wraps an existing IList<T>
implementation, hiding the original collection from client code, but
allowing any read-only operations on it.

Another approach, if you expect only ever to have the parts list
enumerated, is simply to return an IEnumerable<MoldPart>:

class Mold
{
private List<MoldPart> _partsList;

public IEnumerable<MoldPart> Parts
{
get
{
foreach (MoldPart part in _partsList)
{
yield return part;
}
}
}
}

This uses the iterator method syntax for the property getter; it's
essentially another way to wrap the original collection, this time in the
IEnumerable<T> implementation implicitly created by the compiler on your
behalf when you use the iterator syntax.

Pete
 

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

Top