By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,691 Members | 1,152 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,691 IT Pros & Developers. It's quick & easy.

Need help understanding the Cursor

P: n/a
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.


Nov 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

Alex wrote:
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? :)
--
Larry Lard
Replies to group please

Nov 21 '05 #2

P: n/a
Thank you Larry. Your explanation makes perfect sense. I'm glad I asked.

Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.