Home > Software engineering >  how do I create Class modules in other class module and passing value for the classes in vba excel
how do I create Class modules in other class module and passing value for the classes in vba excel

Time:10-06

I have a trouble so I need to create a class modules. Example Condition Room include some property like width, length, height, volume, Window type, Wall type, Roof type, but window, wall, roof either are class module with other properties such as window: glass single layer or double layers, frame wood or alu and so on,... So, how do I create the class module Conditional room consist of other class modules as above Thank you for your help.,

CodePudding user response:

In excel you can not create a class inside class, parent/child or super/sub class is not supported in VBA. However you can use interface to somehow achieve similar capabilities.

CodePudding user response:

You need to create separate classes (files)

Put each of the below in a separate *.cls file:

' Windows.cls
Public Enum GlassType
    SingleLayer
    DoubleLayer
End Enum

Private m_Glass As GlassType

Public Property Get Glass As GlassType
    Glass = m_Glass
End Property
Public Property Let Glass (ByVal value As GlassType)
    m_Glass = value
End Property
' ---

' RoomDimension.cls
Private m_Height As Double
Private m_Length As Double
Private m_Width As Double

Public Property Get Height As Double
    Height = m_Height
End Property
Public Property Let Height (ByVal value As Double)
    m_Height = Abs(value)
End Property

Public Property Get Length As Double
    Length = m_Length
End Property
Public Property Let Length (ByVal value As Double)
    m_Length = Abs(value)
End Property

Public Property Get Width As Double
    Width = m_Width
End Property
Public Property Let Width (ByVal value As Double)
    m_Width = Abs(value)
End Property

Public Property Get Volume As Double
    Volume = Length * Width * Height
End Property
' ---

' ConditionRoom.cls
Private m_Window As Windows
Private m_Dimension As RoomDimension

Public Property Get Window As Windows
    Window = m_Window
End Property
Public Property Let Window (ByVal value As Windows)
    m_Window = value
End Property

Public Property Get Dimension As RoomDimension
    Dimension = m_Dimension
End Property
Public Property Let m_Dimension (ByVal value As RoomDimension)
    m_Dimension = value
End Property

Class_Initialize()
    Set Window = New Windows
    Set Dim = New RoomDimension
End Class
' ---

' Code sample on how to use it
Dim room As ConditionRoom

Set room = New ConditionRoom

' Set the window type
room.Window.Glass = GlassType.SingleLayer

' Set room dimensions
With room.Dimension
    .Height = 250
    .Length = 500
    .Width = 500
    Debug.Print "Room volume: "; .Volume
End With
  •  Tags:  
  • vba
  • Related