By demand of a simple example of using the object oriented features of PowerBasic 9 !! Here it is.
This example has two classes cShip and cContainer
It show the use of collections through the AddContainer and GetContainer
methods.
This example has two classes cShip and cContainer
It show the use of collections through the AddContainer and GetContainer
methods.
Code:
' ====================================================================================================== 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 cBoat = Class "cShip" cCont = Class "cContainer" cBoat.SetMaxSize(20, 20, 40) cCont.Length = 20 cCont.Width = 20 cCont.Height = 20 cBoat.AddContainer(cCont) Print cBoat.GetContainerCount() Local cOneContainer As iContainer cOneContainer = Class "cContainer" cOneContainer = cBoat.GetContainer(1) Print "Width of first container = " & Format$(cOneContainer.Width) Print "Volume used : " & Format$(cBoat.GetVolumeUsed) & " square feet" Print "Capacity left: " & Format$(cBoat.GetCapacityLeft) & " square feet" End Function
Comment