[quoted text, click to view] On Sun, 30 Dec 2007 20:28:14 -0800, bern11 <bern11@yahoo.com> wrote:
> [...]
> I want to call the 'set' member whenever an individual array element i=
s =
[quoted text, click to view] > called. As it is, this statement:
> myObject.Vtx[0] =3D 10;
> does not call the 'set' accessor, even though a statement such as this=
=
[quoted text, click to view] > calls the 'get' accessor:
> x =3D myObject.Vtx[0];
>
> So, how do I encapsulate access to individual members of a member arra=
y?
If I understand your goal correctly, you can't really do exactly what yo=
u =
want while preserving the PointF[] semantics. But you can get very clos=
e.
In C#, you can create an indexed property:
PointF this[int ipt]
{
get { return vtx[ipt]; }
set { vtx[ipt] =3D value; }
}
See:
http://msdn2.microsoft.com/en-us/library/2549tw02(VS.80).aspx
Using that syntax, you can either make your main class have the indexer,=
=
or more likely you'll want to create a specific class to hold the vertex=
=
array, which has an indexed property like that, and which you expose as =
a =
regular property in the main class.
The former is simpler, but creates what IMHO is an awkward syntax if the=
=
main class isn't itself something that is semantically an abstracted lis=
t =
of points. The latter is more complicated, but can more correctly =
represent the intent behind the design, assuming this list of points is =
=
but one of many things the main class contains and exposes.
Either way, it allows you to completely hide the underlying implementati=
on =
and require the client of your class to access the elements through your=
=
own indexer, giving you the opportunity to include whatever specific =
restrictions you like. Of course, even if your only goal is to hide the=
=
underlying array, it works fine for that too.
[quoted text, click to view] > In hindsight, it is clear that my declaration is screwed up, since the=
=
[quoted text, click to view] > 'set' declaration would only work if value were a 3 point array, not a=
=
[quoted text, click to view] > single value. But, how to fix it?
How to fix what? The two problems are not really related. If you are =
asking a second question, regarding how to ensure that the array being =
assigned is compatible with the intent, in your sample code you could lo=
ok =
at the Length property of "value" before assigning it:
set
{
if (value.Length !=3D 3)
{
throw new InvalidArgumentException("array must have exactly=
3 =
elements");
}
vtx =3D value;
SetBounds();
}