Check int

S

shapper

Hello,

I have the following class:

public class Options {
public int Minimum { get; set; }
public int Maximum { get; set; }
} // Options

And I have something like:
Options o = new Options();
o.Minimum = 20;

How can I test here if Maximum was defined or not?
Do I need to make the property nullable Int32? and check if o.Maximum
== null?

Thanks,
Miguel
 
A

Alberto Poblacion

shapper said:
Hello,

I have the following class:

public class Options {
public int Minimum { get; set; }
public int Maximum { get; set; }
} // Options

And I have something like:
Options o = new Options();
o.Minimum = 20;

How can I test here if Maximum was defined or not?
Do I need to make the property nullable Int32? and check if o.Maximum
== null?

You can use a Nullable<int> and then check "if (o.Maximum.HasValue) ...".

An alternative would be to initialize the property with a default
"impossible" value (such as int.MinValue), and then check against that value
to see if it has been assigned something valid.
 
M

Marc Gravell

It depends; if you are content to compare/contrast against the
*default* value (0 in this case), then that may suffice. But then you
won't know if they set o.Minimum = 0. And if you use Nullable<int>,
you won't know if they really meant null. One option is something
like:

private int? minimum;
public int Minimum {
get {return minimum.GetValueOrDefault();}
set {minimum = value;}
}

Now you can check if it has been assigned by looking at
minimum.HasValue. Or if you want to match PropertyGrid/serialization
standards patterns:

public bool ShouldSerializeMinimum() { return minimum.HasValue;}

(can be private for PropertyGrid, but needs to be public for
XmlSerializer)

Marc Gravell
C# MVP
 
H

Harlan Messinger

shapper said:
Hello,

I have the following class:

public class Options {
public int Minimum { get; set; }
public int Maximum { get; set; }
} // Options

And I have something like:
Options o = new Options();
o.Minimum = 20;

How can I test here if Maximum was defined or not?
Do I need to make the property nullable Int32? and check if o.Maximum
== null?

It *is* defined: its value is 0 by default. Do you mean, can you tell if
it was defined explicitly in the code? In that case, if its value is
something other than 0, obviously it was; if its value is 0, you have no
way to know whether someone set it explicitly.
 
S

shapper

   You can use a Nullable<int> and then check "if (o.Maximum.HasValue) ...".

    An alternative would be to initialize the property with a default
"impossible" value (such as int.MinValue), and then check against that value
to see if it has been assigned something valid.

Thank You both,
Miguel
 
P

proxyuser

Harlan Messinger said:
It *is* defined: its value is 0 by default. Do you mean, can you tell if
it was defined explicitly in the code?

"Defined" is being used poorly in this thread :)
 

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

Similar Threads


Top