Reputation: 555
I'm looking for a way to implement a property change event for a object class. Whenever a certain value changes it needs to recalculate the items mass.
I have searched some topic's and may have found a solution, only problem for me is that c#
is unreadable for me ( for now ).
After doing some more digging, if also found a vb.net solution, in the answer they refer to MSDN page. But the sample there is removed.
I also found this page
How to: Implement Property Change Notification
But if I check the current framework it read: "This topic is no longer available"
So my guess something has changed for this event during changing of framework? Or is this still the active method to do this as described in the vb.net solution?
Add code of 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
and the derived 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
Upvotes: 1
Views: 5026
Reputation: 20464
An example written in VisualBasic.Net taken from 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
Sources for your study:
Upvotes: 4