I'm beginning to work with VBA class modules more in Excel and want to make sure that no properties can be used if the class hasn't been initialized. Below is the class and my attempt to at doing so.
Private Type helper Prop1 As Boolean Prop2 As Boolean Prop3 As String Prop4 As String End Type Private m_initialized As Boolean Private this As helper Public Sub Init(ByVal Prop1 As Boolean, ByVal Prop2 As Boolean, ByVal Prop3 As String, ByVal Prop4 As String) this.Prop1 = Prop1 this.Prop2 = Prop2 this.Prop3 = Prop3 this.Prop4 = Prop4 m_initialized = True End Sub Public Property Get Prop1() As Boolean If Not m_initialized Then ThrowNotInitializedError Prop1 = this.Prop1 End Property Public Property Get Prop2() As Boolean If Not m_initialized Then ThrowNotInitializedError Prop2 = this.Prop2 End Property Public Property Get Prop3() As String If Not m_initialized Then ThrowNotInitializedError Prop3 = this.Prop3 End Property Public Property Get Prop4() As String If Not m_initialized Then ThrowNotInitializedError Prop4 = this.Prop4 End Property Public Function Self() As Foo Set Self = Me End Function Private Sub ThrowNotInitializedError() Err.Raise 5, "Init() not run.", "Values weren't initially assigned. Make sure they are assigned before use" End Sub
The repetition of
If Not m_initialized Then ThrowNotInitializedError is the part that I don't like. I can already imagine myself forgetting to put it on a property. Are there any other options?
Testing in a standard module to make sure it worked as I expected
Sub TestingFooInitializeProtection() Dim properlyInitialized As Foo Set properlyInitialized = New Foo properlyInitialized.Init True, False, "Input3", "Input4" Debug.Print properlyInitialized.Prop1 Debug.Print properlyInitialized.Prop2 Debug.Print properlyInitialized.Prop3 Debug.Print properlyInitialized.Prop4 Dim improperlyInitialized As Foo Set improperlyInitialized = New Foo 'improperlyInitialized.Init False, True, "input3", "input4" Debug.Print improperlyInitialized.Prop1 'If above line is left commented out error is thrown. Debug.Print improperlyInitialized.Prop2 Debug.Print improperlyInitialized.Prop3 Debug.Print improperlyInitialized.Prop4 End Sub