在构造函数期间/之后限定类属性(子新) [英] Qualify Class Properties During/After Constructor (Sub New)

查看:14
本文介绍了在构造函数期间/之后限定类属性(子新)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种在 New() 子程序期间或之后获取属性值的方法.一般来说,尽管我想知道是否有办法在类完全启动其所有属性后自动调用某些代码.

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

在 Sub New() 期间,属性被设置为其初始值,而不是在设计时设置的值.

During the Sub New() properties are set to their initial values, rather than the ones that are set up at design time.

基本上,我想知道是否可以设置类似于Form Shown"事件的内容,但类除外.

Essentially I'm wondering if it's possible to setup something similar to the "Form Shown" event, except for classes.

代码:

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

注意:在设计视图中Max"属性的值设置为 3.

Note: The value of the "Max" property is set to 3 in the design view.

推荐答案

使用构造函数的问题是设计器代码在创建对象后很好地设置了属性.但是,NET Framework 包含接口 ISupportInitialize,它非常适合需要执行条件限定属性等操作的控件和组件 - 例如检查 Value after MinMax 已设置.

The issue with using the constructor is that the designer code sets your properties well after it creates your object. But, the NET Framework includes the interface ISupportInitialize which is ideal for controls and components which need to do things such as qualify properties conditionally - for instance checking Value after Min and Max are set.

易于使用:

Imports System.ComponentModel

Public Class Test
    Inherits Component
    Implements ISupportInitialize

当你在最后一行按回车时,它会添加两个方法:

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

Public Sub BeginInit() Implements ISupportInitialize.BeginInit

Public Sub EndInit() Implements ISupportInitialize.EndInit

允许您这样做:

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

它的工作方式是 VS 将在控件/组件属性之后从设计器代码中调用它.如果您打开设计器代码,您将看到如下内容:

The way it works is that VS will invoke this from the designer code after the control/component properties. If you open the designer 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()

因此,您可以在 BeginInit 方法中添加在创建任何内容之前需要运行的任何代码,以及在 中初始化所有属性之后需要运行的代码code>EndInit.

So, you can add any code you need to run before anything is created in your BeginInit method, and code you need to run after all properties are initialized in EndInit.

BeginInitEndInit 将在每次运行设计器代码时运行.也就是说,每次在运行时和表单发生足够多的更改后都需要重新构建.您确实需要保持您的组件代码新鲜,因为 VS 在使用它的项目上工作时在 IDE 中使用它的编译版本.

BeginInit and EndInit will run every time the designer code is run. That is, every time at runtime and after there are enough changes to the form that it needs to be rebuilt. You do need to keep your component code fresh since VS is using a compiled version of it in the IDE when working on the project using it.

所以,当它看起来没有改变时,经常重建和清理.

So, Rebuild often and Clean when it seems like it is not picking up changes.

这篇关于在构造函数期间/之后限定类属性(子新)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆