Numeric Wrapper Objects in C#

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dylan Phillips

    #1

    Numeric Wrapper Objects in C#

    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
  • Zhanyong Wan [MSFT]

    #2
    Re: Numeric Wrapper Objects in C#

    The Nan trick is cute, but doesn't work for integer types. If you need to
    wrap an integer, you need something else.

    Do you know that you can cast any value type into an object and back? This
    is called boxing and unboxing. e.g.

    double x = 2.5;
    object xObj = x; // boxing is done automatically.
    ...
    if ( xObj == null ) ...
    x = (double) xObj; // you have to explicity unbox a boxed value.

    If you don't like the fact that all value types are boxed as object (and
    therefore cannot be told apart statically), you can define your own
    Java-style wrapper classes:

    public class DoubleObject
    {
    private double value;
    public DoubleObject( double x ) { this.value = x; }
    public double GetValue() { return this.value; }
    }

    - Zhanyong Wan

    Visual Studio and .NET Setup

    This posting is provided "AS IS" with no warranties, and confers no rights.
    Use of included samples (if any) is subject to the terms specified at
    http://www.microsoft.com/info/cpyright.htm
    --------------------
    | From: "Dylan Phillips" <mykoan@hotmail .com>
    | References: <OJfJcmwbDHA.26 84@TK2MSFTNGP11 .phx.gbl>
    <#ppd4IxbDHA.15 36@TK2MSFTNGP09 .phx.gbl>
    | Subject: Re: Numeric Wrapper Objects in C#
    | Date: Sat, 30 Aug 2003 12:25:40 -0400
    |
    | Wonderful! It's so simple it did not even occur to me! Sir, I owe you a
    | beer.
    |
    | Thanks,
    | Dylan
    | "Gomaw Beoyr" <Gomaw.Beoyr@no .spam.please.no > wrote in message
    | news:%23ppd4Ixb DHA.1536@TK2MSF TNGP09.phx.gbl. ..
    | > In article <OJfJcmwbDHA.26 84@TK2MSFTNGP11 .phx.gbl>, mykoan@hotmail. com
    | > (Dylan Phillips) writes:
    | >
    | > > In C#, I cannot test a System.Double for null. Any ideas on how
    others
    | > > have solved this?
    | >
    | > Could you use the constant Double.Nan ? ("NaN" = "not a number".)
    | >
    | > You'd still lose the distinction between null and NaN, though,
    | > but perhaps that doesn't matter?
    | >
    | > /Gomaw
    | >
    |
    |
    |

    Comment

    Working...