Need help understanding the Cursor

A

Alex

In the code below, clicking on the button ButtonChangeCursor changes the form's
cursor to a WaitCursor. Clicking the button ButtonRestoreCursor changes the
form's cursor back to its original cursor. For the reasons I explain below I
wouldn't expect this code to behave as it does, so why does it work?

Public Class Form1

Dim OldCursor As Cursor

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
OldCursor = Me.Cursor
End Sub

Private Sub ButtonChangeCursor_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles ButtonChangeCursor.Click
Me.Cursor = Cursors.WaitCursor
End Sub

Private Sub ButtonRestoreCursor_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles ButtonRestoreCursor.Click
Me.Cursor = OldCursor
End Sub

End Class

My confusion derives from the statement "OldCursor = Me.Cursor" in the Form's
Load event. After this statement executes, isn't the OldCursor variable
associated with the same object instance as the one that Me.Cursor is
associated with? That's what I would expect as the Dim statement for OldCursor
simply declares an object variable, it doesn't create a Cursor object. So if
OldCursor and Me.Cursor are associated with the same object, wouldn't any
changes to Me.Cursor affect OldCursor too? That's what I would expect, but
that's not what happens.
 
L

Larry Lard

Alex said:
In the code below, clicking on the button ButtonChangeCursor changes the form's
cursor to a WaitCursor. Clicking the button ButtonRestoreCursor changes the
form's cursor back to its original cursor. For the reasons I explain below I
wouldn't expect this code to behave as it does, so why does it work?

Public Class Form1

Dim OldCursor As Cursor

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
OldCursor = Me.Cursor
End Sub

Private Sub ButtonChangeCursor_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles ButtonChangeCursor.Click
Me.Cursor = Cursors.WaitCursor
End Sub

Private Sub ButtonRestoreCursor_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles ButtonRestoreCursor.Click
Me.Cursor = OldCursor
End Sub

End Class

My confusion derives from the statement "OldCursor = Me.Cursor" in the Form's
Load event. After this statement executes, isn't the OldCursor variable
associated with the same object instance as the one that Me.Cursor is
associated with? That's what I would expect as the Dim statement for OldCursor
simply declares an object variable, it doesn't create a Cursor
object.

Right so far!
So if
OldCursor and Me.Cursor are associated with the same object, wouldn't any
changes to Me.Cursor affect OldCursor too? That's what I would expect, but
that's not what happens.

You are correct that any changes to *the object that they are both
pointing to* would affect them both. However, saying

Me.Cursor =

isn't altering a cursor object; it's point Me.Cursor (which, like
OldCursor, is an object variable) at a different cursor object. If
Cursors were mutable objects (they might well be, I dunno), and you did
something like:

Me.Cursor.SomeCursorProperty = SomeNewValue

then yes, you would find that now OldCursor.SomeCursorProperty also =
SomeNewValue. But that is not what you are doing when you say Me.Cursor
= SomeOtherCursorObject.

This kind of thing was maybe easier to understand when Set was still
around? :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top