Qualify class properties during / after constructor (Sub New)

I'm looking for a way to get the value of a property during or after the substring New (). In general, though I would like to know if there is a way to call some code automatically after the class has all of its properties fully initiated.

The Sub New () properties are set to their initial values, not those set at design time.

Basically I am wondering if it is possible to set something similar to the "Form Display" event except for the classes.

code:

Public Class Test
inherits Component

Public Event Initiated()

Public Sub New()
    MyBase.New()
    RaiseEvent Initiated()
End Sub

Private Sub OnInitiated() Handles Me.Initiated
    Debug.WriteLine(Max)
End Sub

Private _Max As Integer = 5
Public Property Max() As Integer
    Get
        Return _Max
    End Get
    Set(ByVal Value As Integer)
        _Max = Value
    End Set
End Property
End Class

      

Note. The value for the Max property in Design View is 3.

+1


source to share


2 answers


The problem with using the constructor is that the developer code sets your properties after your object is created. But the NET Framework includes an interface ISupportInitialize

that's perfect for controls and components that need to do things like conditionally defining properties like checking Value

after Min

and Max

.

Easy to use:

Imports System.ComponentModel

Public Class Test
    Inherits Component
    Implements ISupportInitialize

      

When you hit enter on the last line, it will add 2 methods:

Public Sub BeginInit() Implements ISupportInitialize.BeginInit

Public Sub EndInit() Implements ISupportInitialize.EndInit

      

What allows you to do this:

Public Sub New()
    MyBase.New()
End Sub

Public Sub EndInit() Implements ISupportInitialize.EndInit
    ' do whatever you want to do
    ' all properties will be initialized at this time
    ' e.g. Max will be the IDE value, not 5
    ...
End Sub

      



The way it works, VS will refer to this from the constructor code after the control / component properties. If you open the designer's code, you will see something like this:

 ' ctl declarations
 CType(Me.Test1, System.ComponentModel.ISupportInitialize).BeginInit()

 ' lots of code initializing controls
 Me.Label1.Name = "Label1"
 ...
 Me.Button1.Location = ...
 ...
 Me.Test1.Max = 3         ' yours will be there somewhere

 ' then at the end:
 CType(Me.Test1, System.ComponentModel.ISupportInitialize).EndInit()

      

So, you can add any code that needs to be run before anything is created in your method BeginInit

, and code that needs to be run after all the properties are initialized to EndInit

.


BeginInit

and EndInit

will be executed every time the constructor code is run. That is, every time at runtime and after there have been enough changes in the form that needs to be rebuilt. You need to keep the component code fresh as VS uses the compiled version of it in the IDE when working on a project using it.

So, rebuild often and clean up when it doesn't seem to pick up changes.

+1


source


The only thing I could see to answer your question is to set up a custom event in my class and fire it at the end of the constructor



0


source







All Articles