passing object as parameter changes behavior

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?QmV0aA==?=

    passing object as parameter changes behavior

    Hello.

    I had a class that worked, but I knew wasn't designed correctly from an OO
    point of view, so I changed it so it was more like I thought it 'should' be,
    and now it doesn't always work. Works most of the time, but not always.

    At first, I had an object variable declared in a class, and everything
    worked fine.
    Then, I figured I should really have 2 classes- one wrapping the other. I
    need my object in both classes, so I passed it as a parameter to the
    constructor in the new class, and that changed the behavior of the object.

    Ideally, I'd like to retain the instance of the object in the outer class
    and refer to it in the inner class instead of passing it as a parameter, but
    I'm not sure how to do that (refer to an object or property in the class
    holding an instance of me.)

    What I want to do is:
    Friend class clsOuter
    Private m_objShared as clsShared
    Private m_objInner as clsInner

    Friend sub init()
    m_objShared = new clsShared
    m_objInner = new clsInner
    end sub

    friend readonly property sharedObj() as clsShared
    get
    return m_objShared
    end get
    end property
    end class

    Friend class clsInner
    Private m_objShared as clsShared

    Friend sub new()
    m_objShared = myParents.objSh ared
    m_objShared.doM ethod

    Is there another way I'm supposed to share a single instance of a class
    across multiple classes?

    Thanks for the help,

    -Beth
  • Family Tree Mike

    #2
    Re: passing object as parameter changes behavior

    Your approach seems fine to me. If the class clsShared is truely a single
    instance object, then another approach would be to follow the Singleton
    class design pattern. You have not made it clear if that would work for you
    or not though.


    "Beth" <Beth@discussio ns.microsoft.co mwrote in message
    news:26C789FD-7E57-40AB-814C-60B59A15A43E@mi crosoft.com...
    Hello.
    >
    I had a class that worked, but I knew wasn't designed correctly from an OO
    point of view, so I changed it so it was more like I thought it 'should'
    be,
    and now it doesn't always work. Works most of the time, but not always.
    >
    At first, I had an object variable declared in a class, and everything
    worked fine.
    Then, I figured I should really have 2 classes- one wrapping the other. I
    need my object in both classes, so I passed it as a parameter to the
    constructor in the new class, and that changed the behavior of the object.
    >
    Ideally, I'd like to retain the instance of the object in the outer class
    and refer to it in the inner class instead of passing it as a parameter,
    but
    I'm not sure how to do that (refer to an object or property in the class
    holding an instance of me.)
    >
    What I want to do is:
    Friend class clsOuter
    Private m_objShared as clsShared
    Private m_objInner as clsInner
    >
    Friend sub init()
    m_objShared = new clsShared
    m_objInner = new clsInner
    end sub
    >
    friend readonly property sharedObj() as clsShared
    get
    return m_objShared
    end get
    end property
    end class
    >
    Friend class clsInner
    Private m_objShared as clsShared
    >
    Friend sub new()
    m_objShared = myParents.objSh ared
    m_objShared.doM ethod
    >
    Is there another way I'm supposed to share a single instance of a class
    across multiple classes?
    >
    Thanks for the help,
    >
    -Beth

    Comment

    • Tom Shelton

      #3
      Re: passing object as parameter changes behavior

      On 2008-10-15, Beth <Beth@discussio ns.microsoft.co mwrote:
      Hello.
      >
      I had a class that worked, but I knew wasn't designed correctly from an OO
      point of view, so I changed it so it was more like I thought it 'should' be,
      and now it doesn't always work. Works most of the time, but not always.
      >
      At first, I had an object variable declared in a class, and everything
      worked fine.
      Then, I figured I should really have 2 classes- one wrapping the other. I
      need my object in both classes, so I passed it as a parameter to the
      constructor in the new class, and that changed the behavior of the object.
      >
      Ideally, I'd like to retain the instance of the object in the outer class
      and refer to it in the inner class instead of passing it as a parameter, but
      I'm not sure how to do that (refer to an object or property in the class
      holding an instance of me.)
      >
      What I want to do is:
      Friend class clsOuter
      Private m_objShared as clsShared
      Private m_objInner as clsInner
      >
      Friend sub init()
      m_objShared = new clsShared
      m_objInner = new clsInner
      end sub
      >
      friend readonly property sharedObj() as clsShared
      get
      return m_objShared
      end get
      end property
      end class
      >
      Friend class clsInner
      Private m_objShared as clsShared
      >
      Friend sub new()
      m_objShared = myParents.objSh ared
      m_objShared.doM ethod
      >
      Is there another way I'm supposed to share a single instance of a class
      across multiple classes?
      >
      Thanks for the help,
      >
      -Beth
      (Air Code)

      Class Outer
      Private _inner As InnerClass
      Private _shared As SharedClass

      Public Sub New()
      _shared = new SharedClass()
      _inner = new InnerClass (Me)
      End Sub



      Class Inner
      Private _parent As Outer

      Sub New (ByVal parent As Outer)
      _parent = parent
      _parent._shared .DoMethod()
      End Sub
      End Class
      End Class

      Anyway... something like that? Or do you need the shared instance accross
      multiple instances of outer/inner. I'm a little unclear of what your trying
      to accomplish.

      --
      Tom Shelton

      Comment

      • =?Utf-8?B?QmV0aA==?=

        #4
        Re: passing object as parameter changes behavior

        It took me awhile to think of an example, but what I want is something like
        the relationship between a form class and the application object.

        The application object contains a collection of form objects, and yet within
        a form class, you're able to reference the containing application object's
        properties. There's also only one instance of the application object.

        I'm not sure what a 'singleton' is, but I can look into that, too.

        Thanks for the replies.

        -Beth

        "Tom Shelton" wrote:
        On 2008-10-15, Beth <Beth@discussio ns.microsoft.co mwrote:
        Hello.

        I had a class that worked, but I knew wasn't designed correctly from an OO
        point of view, so I changed it so it was more like I thought it 'should' be,
        and now it doesn't always work. Works most of the time, but not always.

        At first, I had an object variable declared in a class, and everything
        worked fine.
        Then, I figured I should really have 2 classes- one wrapping the other. I
        need my object in both classes, so I passed it as a parameter to the
        constructor in the new class, and that changed the behavior of the object.

        Ideally, I'd like to retain the instance of the object in the outer class
        and refer to it in the inner class instead of passing it as a parameter, but
        I'm not sure how to do that (refer to an object or property in the class
        holding an instance of me.)

        What I want to do is:
        Friend class clsOuter
        Private m_objShared as clsShared
        Private m_objInner as clsInner

        Friend sub init()
        m_objShared = new clsShared
        m_objInner = new clsInner
        end sub

        friend readonly property sharedObj() as clsShared
        get
        return m_objShared
        end get
        end property
        end class

        Friend class clsInner
        Private m_objShared as clsShared

        Friend sub new()
        m_objShared = myParents.objSh ared
        m_objShared.doM ethod

        Is there another way I'm supposed to share a single instance of a class
        across multiple classes?

        Thanks for the help,

        -Beth
        >
        (Air Code)
        >
        Class Outer
        Private _inner As InnerClass
        Private _shared As SharedClass
        >
        Public Sub New()
        _shared = new SharedClass()
        _inner = new InnerClass (Me)
        End Sub
        >
        >
        >
        Class Inner
        Private _parent As Outer
        >
        Sub New (ByVal parent As Outer)
        _parent = parent
        _parent._shared .DoMethod()
        End Sub
        End Class
        End Class
        >
        Anyway... something like that? Or do you need the shared instance accross
        multiple instances of outer/inner. I'm a little unclear of what your trying
        to accomplish.
        >
        --
        Tom Shelton
        >

        Comment

        • Family Tree Mike

          #5
          Re: passing object as parameter changes behavior

          Here is a definition and example page for the singleton pattern.

          Learn how to use the C# Singleton design pattern to ensure that only one instance of a class is ever created, with quick and easy examples. 100% Source code.


          "Beth" <Beth@discussio ns.microsoft.co mwrote in message
          news:96D9A02C-BDCF-4008-950F-F079720BD35D@mi crosoft.com...
          It took me awhile to think of an example, but what I want is something
          like
          the relationship between a form class and the application object.
          >
          The application object contains a collection of form objects, and yet
          within
          a form class, you're able to reference the containing application object's
          properties. There's also only one instance of the application object.
          >
          I'm not sure what a 'singleton' is, but I can look into that, too.
          >
          Thanks for the replies.
          >
          -Beth
          >
          "Tom Shelton" wrote:
          >
          >On 2008-10-15, Beth <Beth@discussio ns.microsoft.co mwrote:
          Hello.
          >
          I had a class that worked, but I knew wasn't designed correctly from an
          OO
          point of view, so I changed it so it was more like I thought it
          'should' be,
          and now it doesn't always work. Works most of the time, but not always.
          >
          At first, I had an object variable declared in a class, and everything
          worked fine.
          Then, I figured I should really have 2 classes- one wrapping the other.
          I
          need my object in both classes, so I passed it as a parameter to the
          constructor in the new class, and that changed the behavior of the
          object.
          >
          Ideally, I'd like to retain the instance of the object in the outer
          class
          and refer to it in the inner class instead of passing it as a
          parameter, but
          I'm not sure how to do that (refer to an object or property in the
          class
          holding an instance of me.)
          >
          What I want to do is:
          Friend class clsOuter
          Private m_objShared as clsShared
          Private m_objInner as clsInner
          >
          Friend sub init()
          m_objShared = new clsShared
          m_objInner = new clsInner
          end sub
          >
          friend readonly property sharedObj() as clsShared
          get
          return m_objShared
          end get
          end property
          end class
          >
          Friend class clsInner
          Private m_objShared as clsShared
          >
          Friend sub new()
          m_objShared = myParents.objSh ared
          m_objShared.doM ethod
          >
          Is there another way I'm supposed to share a single instance of a class
          across multiple classes?
          >
          Thanks for the help,
          >
          -Beth
          >>
          >(Air Code)
          >>
          >Class Outer
          >Private _inner As InnerClass
          >Private _shared As SharedClass
          >>
          >Public Sub New()
          >_shared = new SharedClass()
          >_inner = new InnerClass (Me)
          >End Sub
          >>
          >>
          >>
          >Class Inner
          >Private _parent As Outer
          >>
          >Sub New (ByVal parent As Outer)
          >_parent = parent
          >_parent._share d.DoMethod()
          >End Sub
          >End Class
          >End Class
          >>
          >Anyway... something like that? Or do you need the shared instance
          >accross
          >multiple instances of outer/inner. I'm a little unclear of what your
          >trying
          >to accomplish.
          >>
          >--
          >Tom Shelton
          >>

          Comment

          Working...