No offense taken. After all, this is the Beginner's forum, isn't it?
And even in another forum you can easily ask these kind of questions.
There are at least two ways to do it:
1. With a default value:
private int xCoord = -1;
Then in the set accessor of a property called XCoord
set
{
if (xCoord == -1)
{
xCoord = value;
}
}
This is a bit risky, because -1 could be a legal value as well. In .NET, nullable types were introduced so you could make it a nullable variables as well.
2. Keep a separate variable for tracking:
private int xCoord = -1;
private bool xCoordHasBeenSet = false;
Then in the set accessor of a property called XCoord
set
{
if (!xCoordHasBeenSet)
{
xCoordHasBeenSet = true;
xCoord = value;
}
}
This uses the internal xCoordHasBeenSet variable to determine whether the value has been set before. If it has been set, xCoordHasBeenSet becomes true and you can no longer set it.
I am not sure if users of your control will understand such a mechanism though. It feels a bit awkward to have a get/set property that can only be set once, although obviously it will do what you need...
Cheers,
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of
ASP.NET 2.0 Instant Results and
Beginning Dreamweaver MX / MX 2004