testing if int property has been initialised.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • damiensawyer@yahoo.com.au

    testing if int property has been initialised.

    Hi,

    I've recently moved to c# framework 2 and have just discovered the
    comiler warning "The result of the expression is always 'false' since
    a value of type 'int' is never equal to 'null' of type 'int?'"

    Can someone please tell me best practise for determining in properties
    if an int has been initialised?

    Thanks very much in advance,


    Damien


    private _remedyFieldCou nt
    public int RemedyFieldCoun t
    {
    get
    {
    if (_remedyFieldCo unt == null) // The result of the
    expression is always 'false' since a value of type 'int' is never
    equal to 'null' of type 'int?'
    {
    _remedyFieldCou nt = ... do
    initialisation }
    }
    return _remedyFieldCou nt;
    }
  • parez

    #2
    Re: testing if int property has been initialised.

    On Jun 22, 9:18 pm, "damiensaw...@y ahoo.com.au"
    <damiensaw...@y ahoo.com.auwrot e:
    Hi,
    >
    I've recently moved to c# framework 2 and have just discovered the
    comiler warning "The result of the expression is always 'false' since
    a value of type 'int' is never equal to 'null' of type 'int?'"
    >
    Can someone please tell me best practise for determining in properties
    if an int has been initialised?
    >
    Thanks very much in advance,
    >
    Damien
    >
    private _remedyFieldCou nt
    public int RemedyFieldCoun t
    {
    get
    {
    if (_remedyFieldCo unt == null) // The result of the
    expression is always 'false' since a value of type 'int' is never
    equal to 'null' of type 'int?'
    {
    _remedyFieldCou nt = ... do
    initialisation }
    }
    return _remedyFieldCou nt;
    }

    public int? Test
    { get; set; }

    if (Test.HasValue)
    {
    }

    this could work...

    Comment

    • parez

      #3
      Re: testing if int property has been initialised.

      On Jun 22, 9:39 pm, parez <psaw...@gmail. comwrote:
      On Jun 22, 9:18 pm, "damiensaw...@y ahoo.com.au"
      >
      >
      >
      <damiensaw...@y ahoo.com.auwrot e:
      Hi,
      >
      I've recently moved to c# framework 2 and have just discovered the
      comiler warning "The result of the expression is always 'false' since
      a value of type 'int' is never equal to 'null' of type 'int?'"
      >
      Can someone please tell me best practise for determining in properties
      if an int has been initialised?
      >
      Thanks very much in advance,
      >
      Damien
      >
      private _remedyFieldCou nt
      public int RemedyFieldCoun t
      {
      get
      {
      if (_remedyFieldCo unt == null) // The result of the
      expression is always 'false' since a value of type 'int' is never
      equal to 'null' of type 'int?'
      {
      _remedyFieldCou nt = ... do
      initialisation }
      }
      return _remedyFieldCou nt;
      }
      >
      public int? Test
      { get; set; }
      >
      if (Test.HasValue)
      {
      }
      >
      this could work...
      this mite be better

      private static int? _test;
      public static int Test
      {
      get
      {
      if (_test.HasValue )
      {

      }
      }
      set
      {
      }
      }

      Comment

      • parez

        #4
        Re: testing if int property has been initialised.

        On Jun 22, 9:39 pm, parez <psaw...@gmail. comwrote:
        On Jun 22, 9:18 pm, "damiensaw...@y ahoo.com.au"
        >
        >
        >
        <damiensaw...@y ahoo.com.auwrot e:
        Hi,
        >
        I've recently moved to c# framework 2 and have just discovered the
        comiler warning "The result of the expression is always 'false' since
        a value of type 'int' is never equal to 'null' of type 'int?'"
        >
        Can someone please tell me best practise for determining in properties
        if an int has been initialised?
        >
        Thanks very much in advance,
        >
        Damien
        >
        private _remedyFieldCou nt
        public int RemedyFieldCoun t
        {
        get
        {
        if (_remedyFieldCo unt == null) // The result of the
        expression is always 'false' since a value of type 'int' is never
        equal to 'null' of type 'int?'
        {
        _remedyFieldCou nt = ... do
        initialisation }
        }
        return _remedyFieldCou nt;
        }
        >
        public int? Test
        { get; set; }
        >
        if (Test.HasValue)
        {
        }
        >
        this could work...
        I think the abouve does not compile.

        private static int? _test;
        public static int Test
        {
        get
        {
        if (!_test.HasValu e)
        {

        }
        return (int)_test;
        }
        set
        {
        _test = value;
        }
        }

        Comment

        • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

          #5
          Re: testing if int property has been initialised.

          damiensawyer@ya hoo.com.au wrote:
          I've recently moved to c# framework 2 and have just discovered the
          comiler warning "The result of the expression is always 'false' since
          a value of type 'int' is never equal to 'null' of type 'int?'"
          >
          Can someone please tell me best practise for determining in properties
          if an int has been initialised?
          You have a few options:

          1) initialize it to a magic value like -1 and consider that value
          uninitialized
          2) add a bool field that indicates whether the int field is initialized
          3) change the type from int to int? - then you can test for null
          4) restructure the logic so that you will not need to test

          Arne

          Comment

          Working...