Announcement

Collapse
No announcement yet.

Collection Class

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Collection Class

    Can anyone point out what is wrong with the following code. I add 3 container objects to the ship object. When I try to get any container object back from the ship object it always returnd the last container object that was added.

    Thanks

    Code:
    'PBWIN 9.00 - WinApi 05/2008 - XP Pro SP3
    'OOP example by Steve Pringles
    ' ======================================================================================================
    CLASS cContainer
      INSTANCE m_Width    AS SINGLE
      INSTANCE m_Length   AS SINGLE
      INSTANCE m_height   AS SINGLE
      INTERFACE iContainer
        INHERIT IDISPATCH
        PROPERTY GET WIDTH () AS SINGLE
          PROPERTY = m_Width
        END PROPERTY
        PROPERTY SET WIDTH (BYVAL Value AS SINGLE)
          m_Width = Value
        END PROPERTY
        PROPERTY GET Length() AS SINGLE
          PROPERTY = m_Length
        END PROPERTY
        PROPERTY SET Length(BYVAL Value AS SINGLE)
          m_Length = Value
        END PROPERTY
        PROPERTY GET Height() AS SINGLE
          PROPERTY = m_Height
        END PROPERTY
        PROPERTY SET Height(BYVAL Value AS SINGLE)
          m_Height = Value
        END PROPERTY
      END INTERFACE
    END CLASS
    
    CLASS cShip
      INSTANCE m_ContainerCount   AS WORD
      INSTANCE m_Volume           AS SINGLE
      INSTANCE m_VolumeUsed       AS SINGLE
      INSTANCE m_Containers()     AS iContainer
      CLASS METHOD CREATE()
        REDIM m_Containers(0) AS INSTANCE iContainer
      END METHOD
      INTERFACE iShip
        INHERIT IDISPATCH
        METHOD SetMaxSize(BYVAL nLength AS SINGLE, BYVAL nWidth AS SINGLE, BYVAL nHeight AS SINGLE)
          m_Volume = nLength * nWidth * nHeight
        END METHOD
        METHOD AddContainer(BYVAL iC AS iContainer) AS LONG
          LOCAL ContainerVolume AS SINGLE
          IF ISOBJECT(iC) THEN
            IF IC.Width AND iC.Height AND iC.Length THEN
              ContainerVolume = iC.Width * iC.Height * iC.Length
              IF ContainerVolume + m_VolumeUsed <= m_Volume THEN
                INCR m_ContainerCount
                REDIM PRESERVE m_Containers(m_ContainerCount - 1) AS INSTANCE iContainer
                m_Containers(m_ContainerCount - 1) = iC
                m_VolumeUsed = m_VolumeUsed + ContainerVolume
                METHOD = 1
              ELSE
                METHOD = 0
              END IF
            END IF
          END IF
        END METHOD
        METHOD GetVolumeUsed() AS SINGLE
          METHOD = m_VolumeUsed
        END METHOD
        METHOD GetCapacityLeft() AS SINGLE
          METHOD = m_Volume - m_VolumeUsed
        END METHOD
        METHOD GetContainerCount() AS WORD
          METHOD = m_ContainerCount
        END METHOD
        METHOD GetContainer(BYVAL nItem AS WORD) AS iContainer
          IF nItem > 0 AND nItem <= m_ContainerCount THEN
            METHOD = m_Containers(nItem - 1)
          ELSE
            METHOD = NOTHING
          END IF
        END METHOD
      END INTERFACE
    END CLASS
    
    FUNCTION PBMAIN() AS LONG
      LOCAL cCont  AS iContainer
      LOCAL cBoat  AS iShip
      LOCAL s$
      cBoat = CLASS "cShip"
      cCont = CLASS "cContainer"
      cBoat.SetMaxSize(20, 20, 40)
    
      cCont.Length = 1
      cCont.Width  = 1
      cCont.Height = 1
      cBoat.AddContainer(cCont)
    
      cCont.Length = 2
      cCont.Width  = 2
      cCont.Height = 2
      cBoat.AddContainer(cCont)
    
      cCont.Length = 3
      cCont.Width  = 3
      cCont.Height = 3
      cBoat.AddContainer(cCont)
      
      s$ =  "Str$(cBoat.GetContainerCount()) = " & STR$(cBoat.GetContainerCount()) & $CRLF
    
      cCont = cBoat.GetContainer(1)
      s$ = s$ & "Volume used  : " & FORMAT$(cBoat.GetVolumeUsed) & " square feet" & $CRLF
      s$ = s$ & "Capacity left: " & FORMAT$(cBoat.GetCapacityLeft) & " square feet" & $CRLF
      s$ = s$ & "Width of first container = " & FORMAT$(cCont.Width) & $CRLF
    
      cCont = cBoat.GetContainer(2)
      s$ = s$ & "Width of Second container = " & FORMAT$(cCont.Width) & $CRLF
    
      cCont = cBoat.GetContainer(3)
      s$ = s$ & "Width of Third container = " & FORMAT$(cCont.Width) & $CRLF
    
      ?s$
      
    END FUNCTION
    --
    Doug

  • #2
    How about this, Doug?

    Code:
    FUNCTION PBMAIN() AS LONG
      LOCAL cCont, cCont1, cCont2, cCont3  AS iContainer
      LOCAL cBoat  AS iShip
      LOCAL cContArr() AS iContainer
      LOCAL s$
      cBoat = CLASS "cShip"
      cCont = CLASS "cContainer"
      cBoat.SetMaxSize(20, 20, 40)
      
      cCont1 = CLASS "cContainer"
      cCont1.Length = 1
      cCont1.WIDTH  = 1
      cCont1.Height = 1
      cBoat.AddContainer(cCont1)
      
      cCont2 = CLASS "cContainer"
      cCont2.Length = 2
      cCont2.WIDTH  = 2
      cCont2.Height = 2
      cBoat.AddContainer(cCont2)
      
      cCont3 = CLASS "cContainer"
      cCont3.Length = 3
      cCont3.WIDTH  = 3
      cCont3.Height = 3
      cBoat.AddContainer(cCont3)
      ...
      ...

    Comment


    • #3
      The AddContainer method is BYVAL but is acting like BYREF

      Code:
          METHOD AddContainer(BYVAL iC AS iContainer) AS LONG
      Also if I recreate that container object between each add it seems to correct the problem.

      Code:
       LOCAL cCont  AS iContainer
        LOCAL cBoat  AS iShip
        LOCAL s$
        cBoat = CLASS "cShip"
        cBoat.SetMaxSize(20, 20, 40)
      
        cCont = CLASS "cContainer"
        cCont.Length = 1
        cCont.Width  = 1
        cCont.Height = 1
        cBoat.AddContainer(cCont)
        cCont = NOTHING
        
        cCont = CLASS "cContainer"
        cCont.Length = 2
        cCont.Width  = 2
        cCont.Height = 2
        cBoat.AddContainer(cCont)
        cCont = NOTHING
      
        cCont = CLASS "cContainer"
        cCont.Length = 3
        cCont.Width  = 3
        cCont.Height = 3
        cBoat.AddContainer(cCont)
        cCont = NOTHING
      
        cCont = CLASS "cContainer"
      --
      Doug

      Comment


      • #4
        Think you might be solving your issue like that, seems that as long as the "parent" object is in existence, any "siblings" or "clones" are connected to the parent, changing the properties of the parent affect the children. When you "destroy" the parent, the reference to the parent is lost by the child and the child becomes independent.
        Furcadia, an interesting online MMORPG in which you can create and program your own content.

        Comment


        • #5
          Let me also emphasize that this is not a true collection class since you can only store one particular interface type whereas a true collection doesn't bother with the interface type.
          So here we are, this is the end.
          But all that dies, is born again.
          - From The Ashes (In This Moment)

          Comment

          Working...
          X