How to create dependant properties???????

  • Thread starter Thread starter Paul Loveless
  • Start date Start date
P

Paul Loveless

Hi all, I'm creating a simple user control that has two properties that
represent a range of valid integer values.
For example:

int minimum, maximum;

public int Minimum
{
get { return mimimum; }
set { minimum = value; }
}

public int Maximum
{
get { return maximum; }
set { maximum= value; }
}

// P.S. I've deliberately made this simple to avoid cluttering code.

Using the properties window, I would like the programmer to be able to set
the minimum and maximum values to a proper
range - for example Minimum = 400 and Maximum = 800.

The rules are that the minimum value must be <= to the maximum value and the
maximum value must be >= to the minimum
value (a circular dependance on each other). Thus, setting the properties to
Minimum = 800 and Maximum = 400 would be illegal and the code would prevent
this.

Does anyone know of a way of doing this using two separate properties or can
it be done with one property or some other method I may have overlooked? Any
ideas would be appreciated.

Regards,
Paul
 
Paul,

When checking the value that is passed in, why not check against the
private properties that you are returning, and not the actual property
itself (which causes the callback).

Hope this helps.
 
Paul said:
int minimum, maximum;

public int Minimum
{
get { return mimimum; }
set { minimum = value; }
}

public int Maximum
{
get { return maximum; }
set { maximum= value; }
}

The rules are that the minimum value must be <= to the maximum value
and the maximum value must be >= to the minimum
value (a circular dependance on each other). Thus, setting the
properties to Minimum = 800 and Maximum = 400 would be illegal and the
code would prevent this.

Does anyone know of a way of doing this using two separate properties
or can it be done with one property or some other method I may have
overlooked? Any ideas would be appreciated.

int minimum = 0;
int maximum = 0;

[DefValue((int) 0)]
public int Minimum
{
get { return mimimum; }
set
{
if (value >= maximum)
throw System.ApplicationExcetion("Min must be less or equal max";
minimum = value;
}
}

[DefValue((int) 0)]
public int Maximum
{
get { return maximum; }
set
{
if (value <= minumum)
throw System.ApplicationExcetion("Max must be greater or equal
min";
maximum = value;
}
}



--
Greetings
Jochen

Do you need a memory-leak finder ?
http://www.codeproject.com/tools/leakfinder.asp

Do you need daily reports from your server?
http://sourceforge.net/projects/srvreport/
 
if (value >= maximum)
throw System.ApplicationExcetion("Min must be less or equal max";
minimum = value;

or an ArgumentOutOfRangeException

Pedantic I know.
JB
 
Sorry for the late reply. I'll have to try that.

Paul

Nicholas Paldino said:
Paul,

When checking the value that is passed in, why not check against the
private properties that you are returning, and not the actual property
itself (which causes the callback).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Paul Loveless said:
Hi all, I'm creating a simple user control that has two properties that
represent a range of valid integer values.
For example:

int minimum, maximum;

public int Minimum
{
get { return mimimum; }
set { minimum = value; }
}

public int Maximum
{
get { return maximum; }
set { maximum= value; }
}

// P.S. I've deliberately made this simple to avoid cluttering code.

Using the properties window, I would like the programmer to be able to set
the minimum and maximum values to a proper
range - for example Minimum = 400 and Maximum = 800.

The rules are that the minimum value must be <= to the maximum value and the
maximum value must be >= to the minimum
value (a circular dependance on each other). Thus, setting the
properties
to
Minimum = 800 and Maximum = 400 would be illegal and the code would prevent
this.

Does anyone know of a way of doing this using two separate properties or can
it be done with one property or some other method I may have overlooked? Any
ideas would be appreciated.

Regards,
Paul
 
Back
Top