Question

VB NET 2010, Framework 3.5

Trying to understand why this works. I create two objects from the the same Class1. GlobalClass1 with Global Scope and LocalClass1 with Module Scope.

In UControl's Load event I set LocalClass1 = GlobalClass1 From this point on anytime I change value of GlobalClass1.TestProperty the property value is also updated in LocalClass1. The Events in LocalClass1 are triggered when GlobalClass1 events fire.

This is the result I was looking for => being able to have a Global Object's events fire in several other Class Modules and User Controls.

I don't quite understand why simply setting the Local Object = Global Object causes the Local Object to automatically update it's property values when the Global Object properties are updated or why the Local Events automatically fire when the Global Object is raising the event?

Module Module1
    Public WithEvents frm As New MainForm
    Public WithEvents GlobalClass1 As New Class1
    Public Sub Main()
        frm.Init()
       frm.ShowDialog()
   End Sub
End Module

Public Class MainForm
    Private uiUserControl As UControl
    Public Function Init() As Boolean
        uiUserControl = New UControl
        uiUserControl.Location = New System.Drawing.Point(60, 80)
        Me.Controls.Add(uiUserControl)
        Return True
    End Function
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Static count As Integer
        GlobalClass1.TestProperty = count.ToString ' This line propagates to the Private Local LocalClass1 and cause it's event to fire??
        count += 1
    End Sub
End Class

Public Class UControl
    Private WithEvents LocalClass1 As New Class1
    Private Sub UControl_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        LocalClass1 = GlobalClass1
    End Sub
    Private Sub LocalClass1_TestPropertyChanged() Handles LocalClass1.TestEvent
        Me.TextBox1.Text = LocalClass1.TestProperty  'This Event fires when events are raised in the other object => GlobalClass1??
    End Sub
End Class

Public Class Class1
    Public Event TestEvent()

    Private _testProperty As String
    Public Property TestProperty() As String
        Get
            Return _testProperty
        End Get
        Set(ByVal value As String)
            _testProperty = value
            RaiseEvent TestEvent()
        End Set
    End Property
End Class

Was it helpful?

Solution

The class instances (objects) are both referencing the same memory space allocated to them on the heap.

OTHER TIPS

from comments:

The problem is with this line: LocalClass1 = GlobalClass1

They started out as different objects but then you made them refer to the same thing. Object references - or reference types - work differently than value types like integer:

Dim x As Int32
Dim y As Int32 = 42

x = y

As value types, the value of y is assigned to x. A reference type is essentially a wrapper or alias for a pointer. Your assignment code therefore replaced the original pointer to a New Class1, with the pointer already assigned to GlobalClass1

Read more about Value vs Reference types at MSDN

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top