vb.net 个对象 属性 个值更改事件

vb.net objects property value changed event

我正在寻找一种方法来实现对象 class 的 属性 更改事件。每当某个值发生变化时,它需要重新计算项目质量。

我搜索了一些主题并可能找到了解决方案,对我来说唯一的问题是 c# 对我来说不可读(目前)。

Link to topic c#

在进行更多挖掘后,如果还找到 vb.net 解决方案,请在答案中参考 MSDN 页面。但是那里的样本被删除了。

Link to topic vb.net

我也找到了这个page

How to: Implement Property Change Notification

但如果我检查当前框架,它会显示:"This topic is no longer available"

所以我猜测在更改框架期间此事件发生了一些变化?或者这仍然是 vb.net 解决方案中描述的执行此操作的有效方法吗?

添加代码 class

Public Class BodyComponent

' Basic properties that are required for all of the bodycompenents
' regardless of the type.

<Browsable(True)> Public Property Type()
<Browsable(True)> Public Property Height() As Double
<Browsable(True)> Public Property Thickness() As Double
<Browsable(True)> Public Property Elevation() As Double
<Browsable(True)> Public Property Auto_Diameter() As Boolean
<Browsable(True)> Public Property Diameter() As Double
<Browsable(True)> Public Property Mass() As Double


' Type Enum that defines what kind of body component is created.
Public Enum BodyComponentType
    Cylinder = 0001
    Cone_reduction = 0002
End Enum

和派生的class

Public Class Body_Cylinder

' Get the base properties
Inherits BodyComponent

' Set new properties that are only required for cylinders
Public Property Segments() As Integer
Public Property LW_Orientation() As Double

Public Shared Count As Integer = 0

Public Sub New()
    count = count + 1
End Sub

' Remove Cylinder from collection and change the count
Public Shared Sub delete_cylinder(ByVal oItem As Integer, ByRef oBinding As BindingSource)

    oBinding.RemoveAt(oItem)
    Count = Count - 1

End Sub

' Calculate mass
Private Sub Calc_mass()

    ' HACK: create material list where rho is defined for other
    ' material types
    Dim rho As Double
    rho = 8

    Dim oVolume As Double
    oVolume = Math.PI / 4 * ((Diameter + 2 * Thickness) ^ 2 - Diameter ^ 2) * Height

    Mass = oVolume * rho
End Sub

VisualBasic.Net 编写的示例取自 this MSDN article.

' This class implements a simple customer type 
' that implements the IPropertyChange interface.

Public Class DemoCustomer
    Implements INotifyPropertyChanged

    ' These fields hold the values for the public properties.
    Private idValue As Guid = Guid.NewGuid()
    Private customerName As String = String.Empty
    Private companyNameValue As String = String.Empty
    Private phoneNumberValue As String = String.Empty

    Public Event PropertyChanged As PropertyChangedEventHandler _
      Implements INotifyPropertyChanged.PropertyChanged

    Private Sub NotifyPropertyChanged(ByVal info As String)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
    End Sub


    ' The constructor is private to enforce the factory pattern.
    Private Sub New() 
        customerName = "no data"
        companyNameValue = "no data"
        phoneNumberValue = "no data"

    End Sub 'New


    ' This is the public factory method.
    Public Shared Function CreateNewCustomer() As DemoCustomer 
        Return New DemoCustomer()

    End Function

    ' This property represents an ID, suitable
    ' for use as a primary key in a database.
    Public ReadOnly Property ID() As Guid
        Get
            Return Me.idValue
        End Get
    End Property


    Public Property CompanyName() As String 
        Get
            Return Me.companyNameValue
        End Get 
        Set
            If value <> Me.companyNameValue Then
                Me.companyNameValue = value
                NotifyPropertyChanged("CompanyName")
            End If
        End Set
    End Property

    Public Property PhoneNumber() As String 
        Get
            Return Me.phoneNumberValue
        End Get 
        Set
            If value <> Me.phoneNumberValue Then
                Me.phoneNumberValue = value
                NotifyPropertyChanged("PhoneNumber")
            End If
        End Set
    End Property
End Class

您的研究来源: