I've often used Java's Numeric Wrapper Objects when numeric properties can have a null state. This is very useful with optional properties for which 0 has meaning. For example:
//Java Code
class SomeObject
{
protected Double _optionalAttrib ute;
public Double getOptionalAttr ibute
{return _optionalAttrib ute;}
public Double setOptionalAttr ibute (double someValue)
{_optionalAttri bute = new Double(someValu e);}
}
class SomeCode
{
public static void main(string[] args)
{
SomeObject object = new SomeObject();
if (object.getOpti onalAttribute == null) {System.out.pri ntln("Attribute is not set");}
else
{
if (object.getOpti onAttribute.dou bleValue() == 0) {System.out.pri ntln("Attribute is set to zero");}
else {System.out.pri ntln("Attribute is set to" + object.getOptio nalAttribute.to String());}
}
}
}
In C#, I cannot test a System.Double for null. Any ideas on how others have solved this? I've been avoiding it for about 3 weeks now, and my code is getting sloppy.
Thanks,
Dylan
//Java Code
class SomeObject
{
protected Double _optionalAttrib ute;
public Double getOptionalAttr ibute
{return _optionalAttrib ute;}
public Double setOptionalAttr ibute (double someValue)
{_optionalAttri bute = new Double(someValu e);}
}
class SomeCode
{
public static void main(string[] args)
{
SomeObject object = new SomeObject();
if (object.getOpti onalAttribute == null) {System.out.pri ntln("Attribute is not set");}
else
{
if (object.getOpti onAttribute.dou bleValue() == 0) {System.out.pri ntln("Attribute is set to zero");}
else {System.out.pri ntln("Attribute is set to" + object.getOptio nalAttribute.to String());}
}
}
}
In C#, I cannot test a System.Double for null. Any ideas on how others have solved this? I've been avoiding it for about 3 weeks now, and my code is getting sloppy.
Thanks,
Dylan
Comment