Collection, collectioneditor and constructor

R

R.A.F.

Hi,

I'm still having my issue with my collection.
I remind you the facts :
- I have a class called "Row".
- I have a collectionbase class named "RowsCollection"
- I have a collectionEditor class called "RowsCollectionEditor".

When i add a new row into the collection class, my new row should have
its property Height, setup to default value = 20. This default value for
row height is written as private static readonly into my RowsCollection
class.

I know that Row constructor should allow such parameter like :
public Row(RowsCollection owner, int iHeight)
{
...
this.Height = iHeight
...
}

But my problem is that i do not understand from where should i call this
constructor.

1. Should i call it from RowsCollection method ? if yes, from which one
and how ?
2. Should i call it from RowsCollectionEditor method ? if yes, from
which one and how ?

i tried to find something about createnewinstance from
RowsCOllectionEditor or Add method (overriden) from RowsCollection but
nothing works...

So i'm confused. :-(

Any help would be wonderfull,
thx,

RAF
 
P

Peter Duniho

R.A.F. said:
[...]
But my problem is that i do not understand from where should i call this
constructor.

1. Should i call it from RowsCollection method ? if yes, from which one
and how ?
2. Should i call it from RowsCollectionEditor method ? if yes, from
which one and how ?

Honestly, the answer to the question is really only one that you as the
code designer can answer.

That said, a collection class doesn't generally instantiate the members.
It usually would just keep them organized according to the rules of
the collection. So that would rule out #1.

Whether the editor class instantiates the collection member is a
different question. It's certainly a possible design, and if the editor
class is generally the object that creates these members, that's where
you'd instantiate it. As for "which one and how", you'd instantiate it
in the same place you'd instantiate a member that doesn't override the
default, and you'd instantiate it in exactly the same way you
instantiate any class: with the "new" operator.

If your editor class doesn't normally instantiate these members, then it
wouldn't be an appropriate place to instantiate it in this case either.

You need to look at your overall design. Nothing significant should be
changed by the introduction of a way to override the row height. You
should still instantiate your Row instances in the same way you did
before; just use a different constructor when you want to override the
row height.

If you are having a problem with knowing that you want to override the
row height when the instance is created, then you have a more
fundamental design problem. Either you need to fix the design so that
that _is_ known at the point of instantiation, or you need to change
your row height overriding technique so that you can change the row
height after instantiating the object (perhaps by a public property on
the class).

A third possibility would be to move the instantiation itself to the
place in your design where you know whether you want to override the
default height, but that's likely to be a larger change than either of
the other two options and so you'd likely want to avoid making that
change unless you had some other good reason for doing so.

Pete
 
C

Chris Mullins [MVP - C#]

I'm not qutie sure about your collection editor, but the rest is easy
enough. The code below is using C# 3.0 syntax, but should be easy enough to
convert to any other version.

public class Row
{
public int Height { get; set; }
}

public class RowCollection : Collection<Row>
{
public static int DefaultHeight { get; set; }

public new void Add(Row r)
{
r.Height = RowCollection.DefaultHeight;
base.Add(r);
}
}

The code to use this looks like:
RowCollection.DefaultHeight = 100;

Row r = new Row();
Row r2 = new Row();
RowCollection myRows = new RowCollection();
myRows.Add(r);
myRows.Add(r2);
 
N

Nicholas Paldino [.NET/C# MVP]

Isn't your Add method going to create the new instance and then return
it? If so, then just call the constructor to the Row class which will take
the height. Mark the constructor as internal, so that classes outside of
your assembly can't access it.

For this to work though, your Add method can't allow an existing
instance to be passed to the method, it has to take properties which you
then use to create the instance, and then add that to the collection.
 
R

R.A.F.

Hi Chris,

thx...this works well as command line, but it does not solve my issue if
end user works with the property editor :-(
but i found a way to make it work with property editor, as followed :

protected override object CreateInstance(Type itemType)
{
object instance = Activator.CreateInstance(itemType, true);
((Row)instance).Height = RowsCollection.m_DefaultRowHeight;
return instance;
}

all Rows added via property editor, will have Height = 20;

RAF.
 
R

R.A.F.

Hi Nicholas,

as i wrote before (for Chris), the Add method works great when users
will write commandline code like :

Row r1 = new Row();
RowCollection myrows = new RowCollection();
myrows.Add(r1);

But not when user works with Collection Property Editor. :-(

RAF
 
N

Nicholas Paldino [.NET/C# MVP]

R.A.F.

Can you have a static internal field/property on the RowCollection class
which exposes the default height, and then in the constructor of the Row
class, get the value and set it?
 

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