Class Factories - Parameterized Object Initialisation (3 Viewers)

dalski

Member
Local time
Today, 21:19
Joined
Jan 5, 2025
Messages
194
Trying to learn Implementation Classes & Class Factories - Parameterized Object Initialisation from this great tutorial,but I get the pic'd error & I can't see where I'm going wrong? I've Predeclared the classes as per tutorial. I'm just before he turns it into an Abstract Factory. I also tried it before creating the cISomethingFactory & got the same error, but in my mind that should have worked.

I'm unsure what the benefit of using a Type for the Properties of the Implementation Class & why he's not using typical module level variables but that's besides the point atm & I can experiment once I get it working.

1759858670119.png


cSomething
Code:
Private Type TSomething
  Bar As Long
  Ducky As String
End Type

Private this As TSomething
Implements cISomething
Implements cISomethingFactory

Public Function Create(ByVal initialBar As Long, ByVal initialDucky As String) As cISomething
  With New cSomething
    .Bar = initialBar
    .Ducky = initialDucky
    Set Create = .Self
  End With
End Function

Public Property Get Self() As cISomething
  Set Self = Me
End Property

Public Property Get Bar() As Long
  Bar = this.Bar
End Property
Friend Property Let Bar(ByVal NewValue As Long)
  this.Bar = NewValue
End Property
Public Property Get Ducky() As String
  Ducky = this.Ducky
End Property
Friend Property Let Ducky(ByVal NewValue As String)
  this.Ducky = NewValue
End Property


Private Property Get ISomething_Bar() As Long
  ISomething_Bar = Bar
End Property
Private Property Get ISomething_Ducky() As Long
  ISomething_Ducky = Ducky
End Property


Private Function cISomethingFactory_Create(ByVal initialBar As Long, ByVal initialDuck As String) As cISomething
  Set cISomethingFactory_Create = Create(initialBar, initialDuck)
End Function

cISomething
Code:
Public Property Get Bar() As Long
End Property

Public Property Get Ducky() As String
End Property

cISomethingFactory
Code:
Public Property Get Bar() As Long
End Property

Public Property Get Ducky() As String
End Property

Public Function Create(ByVal initialBar As Long, ByVal initialDuck As String) As cISomething
End Function

ModTest
Code:
Sub testImplementClass()
  Dim obj As cSomething
  With obj.Create(5, "Quack")
    Debug.Print .Bar
  End With
End Sub
 

Attachments

Private Property Get ISomething_Bar() As Long
ISomething_Bar = Bar
End Property
Private Property Get ISomething_Ducky() As Long
ISomething_Ducky = Ducky
End Property


cISomething
Code:
Public Property Get Bar() As Long
End Property

Public Property Get Ducky() As String
End Property

Since you defined the interface class "cISomething", shouldn't the implementation functions have that name.

Code:
Private Property Get cISomething_Bar() As Long
  ISomething_Bar = Bar
End Property

Private Property Get cISomething_Ducky() As Long
  ISomething_Ducky = Ducky
End Property

IMO
The type for member variables makes it much easer to reference with each variable using one known instance variable "This".
 
Thanks Ron & for explaining the benefit of the Type in the tutorial.

Oh I see (thank you); I missed the 'c' in ISomething_Bar in cSomething should be cISomething_Bar & ditto for Ducky. Regardless after fixing that the compiler is still throwing the same error "Object Module needs to implement 'Bar' for interface cIsomethingFactory", which I do not understand why that's happening but in much feedback in the tutorial no-one else has any problems with it.
 
Oh I see (thank you); I missed the 'c' in ISomething_Bar in cSomething should be cISomething_Bar & ditto for Ducky. Regardless after fixing that the compiler is still throwing the same error "Object Module needs to implement 'Bar' for interface cIsomethingFactory", which I do not understand why that's happening but in much feedback in the tutorial no-one else has any problems with it.

In cISomethingFactory you define get properties for Bar and Ducky which are not needed, you can comment them out.

in cSomething you are retuning the wrong type.
Code:
'Private Property Get cISomething_Ducky() As Long
Private Property Get cISomething_Ducky() As String
  cISomething_Ducky = Ducky
End Property

in testImplementClass you need to define obj with new.

Code:
Sub testImplementClass()
'  Dim obj As cSomething
  Dim obj As New cSomething
  With obj.Create(5, "Quack")
    Debug.Print .Bar
  End With
End Sub
 
cSomething was missing the below; as the Implementation Class' blueprint (members of the Implementation Class) must be followed wherever it is implemented. The compiler needs to see code-stubs for them members.

Code:
Private Property Get cISomethingFactory_Bar() As Long
  cISomethingFactory_Bar = Bar
End Property
Private Property Get cISomethingFactory_Ducky() As String
  cISomethingFactory_Ducky = Ducky
End Property

I also stupidly had a Ducky property as the wrong data-type (apologies). So now she compiles. But when testing in the Testing module I get 'Object Variable or With block variable not set'; the entire purpose of the exercise :ROFLMAO:.

Code:
Sub testImplementClass()
  Dim obj As cSomething
  With obj.Create(5, "Quack")
    Debug.Print .Bar
  End With
End Sub

The testing module should be able to access the public function Create in the cSomething. Then implement cISomething to get the properties Bar & Ducky. Then create an instance with the Property Self.

Seems superfluous why duplicate properties are in the cSomething; Bar & Ducky. One would think these would be used through the Implementation class.:eek:
 

Users who are viewing this thread

Back
Top Bottom