Skip to main content
Derpy "If"
Source Link
Blackhawk
  • 1.1k
  • 1
  • 10
  • 19

My assumption is that your goal is to mimick constructors in a language which lacks them

If I understand correctly, this is a freaking sweet idea! If the class need the default member for anything else, why WOULDN'T you want a constructor? If

I have to agree with @MatsMug that the examples you've got are somewhat confusing - the class is named Constructor and the constructor is named PieceOfMe? I'd suggest a more concrete example, like the CircleShape class below.

I'd advise the removal of the randomized parameter and the use of the static instance for anything but constructing new instances. They seem unrelated to the underlying concept, which is mimicking constructors in a language that lacks them. In fact, it seems more like writing Set myCircle = CircleShape (assigns the static, named instance of CircleShape to myCircle) when you intended to call the empty constructor Set myCircle = CircleShape() (calls Constructor with none of the optional parameters, creating a new defaulted instance) would be a pitfall to avoid.


A concrete example, the CircleShape class with a default Constructor method:

VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "CircleShape"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Compare Database

Private m_Radius As Double
Private Const PI = 3.1415926535898

Public Property Get Constructor(Optional Radius As Double = 1) As CircleShape
Attribute Constructor.VB_UserMemId = 0
'This is the default member
    Set Constructor = New CircleShape
    Constructor.Radius = Radius
End Property

Public Property Get Radius() As Double
    Radius = m_Radius
End Property

Public Property Let Radius(value As Double)
    m_Radius = value
End Property

Public Property Get Area() As Double
    Area = m_Radius * PI
End Property

And testing the class:

Public Sub Main()
    Dim myCircle As CircleShape

    Set myCircle = CircleShape(4.75)
    Debug.Print "4.75 inch circle area: " & myCircle.Area

    Set myCircle = CircleShape()
    Debug.Print "Unit circle area: " & myCircle.Area
End Sub

I implemented the other two examples as a RectangleShape(Optional Length As Double = 0, Optional Width As Double = 0 and HyperCubeShape(ParamArray arr() As Variant) with the intent of HyperCubeShape(numberOfDimensions As Long, lengthDim1 As Double, lengthDim2 As Double, ...) to test them out and they work as advertised. Can't wait to see if you can figure out a way of intellisensing the ParamArray!

My assumption is that your goal is to mimick constructors in a language which lacks them

If I understand correctly, this is a freaking sweet idea! If the class need the default member for anything else, why WOULDN'T you want a constructor? If

I have to agree with @MatsMug that the examples you've got are somewhat confusing - the class is named Constructor and the constructor is named PieceOfMe? I'd suggest a more concrete example, like the CircleShape class below.

I'd advise the removal of the randomized parameter and the use of the static instance for anything but constructing new instances. They seem unrelated to the underlying concept, which is mimicking constructors in a language that lacks them. In fact, it seems more like writing Set myCircle = CircleShape (assigns the static, named instance of CircleShape to myCircle) when you intended to call the empty constructor Set myCircle = CircleShape() (calls Constructor with none of the optional parameters, creating a new defaulted instance) would be a pitfall to avoid.


A concrete example, the CircleShape class with a default Constructor method:

VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "CircleShape"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Compare Database

Private m_Radius As Double
Private Const PI = 3.1415926535898

Public Property Get Constructor(Optional Radius As Double = 1) As CircleShape
Attribute Constructor.VB_UserMemId = 0
'This is the default member
    Set Constructor = New CircleShape
    Constructor.Radius = Radius
End Property

Public Property Get Radius() As Double
    Radius = m_Radius
End Property

Public Property Let Radius(value As Double)
    m_Radius = value
End Property

Public Property Get Area() As Double
    Area = m_Radius * PI
End Property

And testing the class:

Public Sub Main()
    Dim myCircle As CircleShape

    Set myCircle = CircleShape(4.75)
    Debug.Print "4.75 inch circle area: " & myCircle.Area

    Set myCircle = CircleShape()
    Debug.Print "Unit circle area: " & myCircle.Area
End Sub

I implemented the other two examples as a RectangleShape(Optional Length As Double = 0, Optional Width As Double = 0 and HyperCubeShape(ParamArray arr() As Variant) with the intent of HyperCubeShape(numberOfDimensions As Long, lengthDim1 As Double, lengthDim2 As Double, ...) to test them out and they work as advertised. Can't wait to see if you can figure out a way of intellisensing the ParamArray!

My assumption is that your goal is to mimick constructors in a language which lacks them

If I understand correctly, this is a freaking sweet idea! If the class need the default member for anything else, why WOULDN'T you want a constructor?

I have to agree with @MatsMug that the examples you've got are somewhat confusing - the class is named Constructor and the constructor is named PieceOfMe? I'd suggest a more concrete example, like the CircleShape class below.

I'd advise the removal of the randomized parameter and the use of the static instance for anything but constructing new instances. They seem unrelated to the underlying concept, which is mimicking constructors in a language that lacks them. In fact, it seems more like writing Set myCircle = CircleShape (assigns the static, named instance of CircleShape to myCircle) when you intended to call the empty constructor Set myCircle = CircleShape() (calls Constructor with none of the optional parameters, creating a new defaulted instance) would be a pitfall to avoid.


A concrete example, the CircleShape class with a default Constructor method:

VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "CircleShape"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Compare Database

Private m_Radius As Double
Private Const PI = 3.1415926535898

Public Property Get Constructor(Optional Radius As Double = 1) As CircleShape
Attribute Constructor.VB_UserMemId = 0
'This is the default member
    Set Constructor = New CircleShape
    Constructor.Radius = Radius
End Property

Public Property Get Radius() As Double
    Radius = m_Radius
End Property

Public Property Let Radius(value As Double)
    m_Radius = value
End Property

Public Property Get Area() As Double
    Area = m_Radius * PI
End Property

And testing the class:

Public Sub Main()
    Dim myCircle As CircleShape

    Set myCircle = CircleShape(4.75)
    Debug.Print "4.75 inch circle area: " & myCircle.Area

    Set myCircle = CircleShape()
    Debug.Print "Unit circle area: " & myCircle.Area
End Sub

I implemented the other two examples as a RectangleShape(Optional Length As Double = 0, Optional Width As Double = 0 and HyperCubeShape(ParamArray arr() As Variant) with the intent of HyperCubeShape(numberOfDimensions As Long, lengthDim1 As Double, lengthDim2 As Double, ...) to test them out and they work as advertised. Can't wait to see if you can figure out a way of intellisensing the ParamArray!

Source Link
Blackhawk
  • 1.1k
  • 1
  • 10
  • 19

My assumption is that your goal is to mimick constructors in a language which lacks them

If I understand correctly, this is a freaking sweet idea! If the class need the default member for anything else, why WOULDN'T you want a constructor? If

I have to agree with @MatsMug that the examples you've got are somewhat confusing - the class is named Constructor and the constructor is named PieceOfMe? I'd suggest a more concrete example, like the CircleShape class below.

I'd advise the removal of the randomized parameter and the use of the static instance for anything but constructing new instances. They seem unrelated to the underlying concept, which is mimicking constructors in a language that lacks them. In fact, it seems more like writing Set myCircle = CircleShape (assigns the static, named instance of CircleShape to myCircle) when you intended to call the empty constructor Set myCircle = CircleShape() (calls Constructor with none of the optional parameters, creating a new defaulted instance) would be a pitfall to avoid.


A concrete example, the CircleShape class with a default Constructor method:

VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "CircleShape"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Compare Database

Private m_Radius As Double
Private Const PI = 3.1415926535898

Public Property Get Constructor(Optional Radius As Double = 1) As CircleShape
Attribute Constructor.VB_UserMemId = 0
'This is the default member
    Set Constructor = New CircleShape
    Constructor.Radius = Radius
End Property

Public Property Get Radius() As Double
    Radius = m_Radius
End Property

Public Property Let Radius(value As Double)
    m_Radius = value
End Property

Public Property Get Area() As Double
    Area = m_Radius * PI
End Property

And testing the class:

Public Sub Main()
    Dim myCircle As CircleShape

    Set myCircle = CircleShape(4.75)
    Debug.Print "4.75 inch circle area: " & myCircle.Area

    Set myCircle = CircleShape()
    Debug.Print "Unit circle area: " & myCircle.Area
End Sub

I implemented the other two examples as a RectangleShape(Optional Length As Double = 0, Optional Width As Double = 0 and HyperCubeShape(ParamArray arr() As Variant) with the intent of HyperCubeShape(numberOfDimensions As Long, lengthDim1 As Double, lengthDim2 As Double, ...) to test them out and they work as advertised. Can't wait to see if you can figure out a way of intellisensing the ParamArray!