Help tracking class property initializations

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dvestal@gmail.com

    Help tracking class property initializations

    I have a class with public members, and I need to know whether those
    members have been explicitly initialized. I could accomplish that
    with a class like this:

    class C
    {
    private bool m_isSet = false;
    private object m_object;

    public object MyObj
    {
    get
    {
    return m_object;
    }
    set
    {
    m_object = value;
    m_isSet = true;
    }
    }
    }

    ....but for my purposes, that's way too verbose. I need a way to
    achieve that functionality, but write the class much more concisely.
    Does anybody know a way to do this while allowing a shorter class
    definition, such as the one below?

    class C
    {
    // Is there any way to know when
    // MyObj is set?
    public object MyObj;
    }

  • Ignacio Machin \( .NET/ C# MVP \)

    #2
    Re: Help tracking class property initializations

    Hi,

    <dvestal@gmail. comwrote in message
    news:1185195325 .646100.259830@ d55g2000hsg.goo glegroups.com.. .
    ...but for my purposes, that's way too verbose. I need a way to
    achieve that functionality, but write the class much more concisely.
    Does anybody know a way to do this while allowing a shorter class
    definition, such as the one below?
    >
    class C
    {
    // Is there any way to know when
    // MyObj is set?
    public object MyObj;
    }
    In general there is no way of doing it, if you only have references types
    you could check if they are not null. This fails for valued types, as they
    always have a value. Additionally the method will also fails with references
    types that the class initialize in the constructor.

    You will have to use the verbose method I think


    Comment

    • Nicholas Paldino [.NET/C# MVP]

      #3
      Re: Help tracking class property initializations

      That only applies if null is not a valid state for the reference type
      fields that he has. If you can explicitly set the value to null, then you
      still need the flag.

      On the flip side, as of .NET 2.0, you could use a nullable type for the
      backing field (assuming null is not allowed as a value in the range of
      values) for value types, and then check for null to see if the value has
      been set (while the property exposed is not nullable).

      You could get away with this by using a context bound object, and
      intercepting the calls, but that is a lot of work, and there is going to be
      a bit of overhead to intercept the calls. However, it would allow for a
      more concise implementation (the interception layer for the context bound
      object would handle whether or not a property was explicitly set).


      --
      - Nicholas Paldino [.NET/C# MVP]
      - mvp@spam.guard. caspershouse.co m

      "Ignacio Machin ( .NET/ C# MVP )" <machin TA laceupsolutions .comwrote in
      message news:%23fcSAFTz HHA.748@TK2MSFT NGP04.phx.gbl.. .
      Hi,
      >
      <dvestal@gmail. comwrote in message
      news:1185195325 .646100.259830@ d55g2000hsg.goo glegroups.com.. .
      >
      >...but for my purposes, that's way too verbose. I need a way to
      >achieve that functionality, but write the class much more concisely.
      >Does anybody know a way to do this while allowing a shorter class
      >definition, such as the one below?
      >>
      >class C
      >{
      > // Is there any way to know when
      > // MyObj is set?
      > public object MyObj;
      >}
      >
      In general there is no way of doing it, if you only have references types
      you could check if they are not null. This fails for valued types, as they
      always have a value. Additionally the method will also fails with
      references types that the class initialize in the constructor.
      >
      You will have to use the verbose method I think
      >

      Comment

      • dvestal@gmail.com

        #4
        Re: Help tracking class property initializations

        I want to use both value and reference types, and null may be a valid
        value. I will check into context-bound objects (haven't heard of them
        before).

        In the meantime, I can get a reasonably concise class definition using
        a generic MessageProperty class, as shown below. Implicit conversion
        operators allow me to use the C.MyObj member as whatever type I
        choose. I don't see a significant downside to this approach, but I
        welcome comment.

        public class MessageProperty <T>
        {
        private T m_value;
        private bool m_isSet = false;

        public T Value
        {
        get
        {
        return m_value;
        }
        set
        {
        m_value = value;
        m_isSet = true;
        }
        }

        public bool IsSet
        {
        get { return m_isSet; }
        }

        static public implicit operator T(MessageProper ty<Tproperty)
        {
        return property.Value;
        }

        static public implicit operator MessageProperty <T>(T value)
        {
        MessageProperty <Tprop = new MessageProperty <T>();
        prop.Value = value;
        return prop;
        }
        }

        public class C
        {
        public MessageProperty <object MyObj = new
        MessageProperty <object>();
        }

        Comment

        • Nicholas Paldino [.NET/C# MVP]

          #5
          Re: Help tracking class property initializations

          I actually think that's a pretty elegant way to handle it. It's much
          better than context bound objects, as you won't have the overhead of
          interception. I'd definitely use this.


          --
          - Nicholas Paldino [.NET/C# MVP]
          - mvp@spam.guard. caspershouse.co m

          <dvestal@gmail. comwrote in message
          news:1185208069 .053783.240670@ r34g2000hsd.goo glegroups.com.. .
          >I want to use both value and reference types, and null may be a valid
          value. I will check into context-bound objects (haven't heard of them
          before).
          >
          In the meantime, I can get a reasonably concise class definition using
          a generic MessageProperty class, as shown below. Implicit conversion
          operators allow me to use the C.MyObj member as whatever type I
          choose. I don't see a significant downside to this approach, but I
          welcome comment.
          >
          public class MessageProperty <T>
          {
          private T m_value;
          private bool m_isSet = false;
          >
          public T Value
          {
          get
          {
          return m_value;
          }
          set
          {
          m_value = value;
          m_isSet = true;
          }
          }
          >
          public bool IsSet
          {
          get { return m_isSet; }
          }
          >
          static public implicit operator T(MessageProper ty<Tproperty)
          {
          return property.Value;
          }
          >
          static public implicit operator MessageProperty <T>(T value)
          {
          MessageProperty <Tprop = new MessageProperty <T>();
          prop.Value = value;
          return prop;
          }
          }
          >
          public class C
          {
          public MessageProperty <object MyObj = new
          MessageProperty <object>();
          }
          >

          Comment

          Working...