Question Classes and Properties

elBee

New member
Joined
May 17, 2021
Messages
2
Programming Experience
3-5
Hello together,

I have just started to work with classes and properties and now I am at a point where I don't know if it works or how it works,

I have 2 classes.
Klasse 1 wird einmal instanziert, Klasse 2 kann mehrmals instanziert werden:

VB.NET:
Dim x as New Class 2
Dim y as New Class 2
Dim z as New Class 2

My two classes would look like this:

VB.NET:
Imports System.ComponentModel

' +++ CLASS 1 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Public Class Class1
    'INotifyPropertyChanged
    Implements INotifyPropertyChanged
    Public Shadows Event PropertyChanged(ByVal sender As Object, ByVal e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged

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

    Private _Test1 As String
    Public Property Test1() As Integer
        Get
            Return _Test1
        End Get
        Set(value As Integer)
            _Test1 = value
            NotifyPropertyChanged("Test1")
        End Set
    End Property
End Class

' +++ CLASS 2 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Public Class Class2
    'INotifyPropertyChanged
    Implements INotifyPropertyChanged
    Public Shadows Event PropertyChanged(ByVal sender As Object, ByVal e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged

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

    Private _Test2 As String
    Public Property Test2() As Integer
        Get
            Return _Test2
        End Get
        Set(value As Integer)
            Dim xyz As Class1
            _Test2 = value * xyz.Test1              'HERE IS THE PROBLEM
            NotifyPropertyChanged("Test2")
        End Set
    End Property
End Class

If now in the 1st class the property "Test1" changes, then all instances of the class 2 (in this case the property "Test2" should update.
Is this possible and if so, would someone give me an example of how this works?

It would be very nice if someone could shed some light on this. As I said, I am quite new in this area.

Kind regards,
Sven
 
Last edited by a moderator:
Firstly, please use the Inline code button only for inline code, e.g.
I have a Button on my form.
For blocks of code, please use the Code button. I have edited your post accordingly.
 
As for the question, it really doesn't make much sense. If you expect every instance of Class2 to be effected by a change to a single instance of Class1 then every Class2 instance would have to have a reference to that same one Class1 object. If that's OK then you need to do that, i.e. pass in that Class1 object to each Class2. If you want to avoid that dependency on a single instance of that type then the property would have to be declared Shared. I would suggest that, rather than dealing in hypotheticals, you provide actual types where you see this relationship being manifested because, as it stands, what you're asking for doesn't seem useful in any sort of real-world situation.
 
Hi, first of all thanks for the feedback... as I said, I'm quite new to this.

My question is based on this:
Suppose I have in Class1 a property "Depreciation" with value "3" years and another property "Internal Interest Rate" with 2%.

In Class2 I define individual objects, e.g. printer, monitor and PC, as well as their purchase price. My annual costs should then be calculated in the property "CostsPerYear" as follows:

annual costs = purchase price / depreciation + purchase price * internal interest rate

I bind this annual cost to a textbox via
TextBox1.Databinding.Add("Text", Class2, "CostsPerYear")

Now it should be possible that if I change assumed depreciation or internal interest rate from Class1, it should be recalculated automatically.
For illustration my real code:

Form:
VB.NET:
Public Class frm_mainform
    Private Sub frm_LogiCal_Load(sender As Object, e As EventArgs) Handles MyBase.Load

            'Actually, the values from the SQL database are taken over here.
            Dim CostBase_Hardware As New MasterData_BaseCosts(3, 0.015)
            Dim CostBase_Building As New MasterData_BaseCosts(12, 0.022)
            Dim CostBase_IndTrucks As New MasterData_BaseCosts(6, 0.015)

            'DataBinding'
            Me.tbx_AnnualCost.DataBindings.Add("Text", New AnnualCosts(Me.tbx_price.Text, CostBase_Hardware.AfA, CostBase_Hardware.Interests), "AnnualCostsObj")
    End Sub
End Class

Class 1:
VB.NET:
Imports System.ComponentModel

Public Class MasterData_BaseCosts
    'INotifyPropertyChanged
    Implements INotifyPropertyChanged
    Public Shadows Event PropertyChanged(ByVal sender As Object, ByVal e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged

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

    Private _AfA As Integer
    Private _Interests As Double

    Public Sub New(x As Integer, y As Double)
        _AfA = x
        _Interests = y
    End Sub

    Public Property AfA As Integer
        Get
            Return _AfA
        End Get
        Set(value As Integer)
            _AfA = value
            NotifyPropertyChanged("AfA")
        End Set
    End Property

    Public Property Interests As Double
        Get
            Return _Interests
        End Get
        Set(value As Double)
            _Interests = value
            NotifyPropertyChanged("Interests")
        End Set
    End Property
End Class

Class 2:
VB.NET:
Public Class AnnualCosts
    'INotifyPropertyChanged
    Implements INotifyPropertyChanged
    Public Shadows Event PropertyChanged(ByVal sender As Object, ByVal e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged

    Dim _price As Double
    Dim _AfA As Integer
    Dim _Interests As Double
    Dim _AnnualCostsObj As Double

    Public Sub New(price As Double, AfA As Integer, Interests As Double)
        _price = price
        _AfA = AfA
        _Interests = Interests
    End Sub

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

    Public Property AnnualCostsObj As Double
        Get
            Return _AnnualCostsObj
        End Get
        Set(value As Double)
            _AnnualCostsObj = _price * _AfA + _price * _Interests
        End Set
    End Property
End Class
 

Latest posts

Back
Top