0

In my Module1.vb code, if I assign a Watch to Form1.Width, I get the error "Reference to a non-shared member requires an object reference", and if I assign a Watch to Form1, I get the error "Form1 is a type and cannot be used as an expression"

However, Debug.Print Form1.Width works.

I read here (https://msdn.microsoft.com/en-us/library/aa262343(v=vs.60).aspx) that

Visual Basic creates a hidden global object variable for every form class. It's as if Visual Basic had added the following declaration to your project: Public Form1 As New Form1

Is Visual Studio Watch insisting on accessing Form1 as a Class rather than as an instance (as in Debug.Pring)?

Am I missing something obvious?

Zingapuro
  • 51
  • 8
  • Note that the article you linked is for VB6 not VB.NET. See http://stackoverflow.com/a/20077246/1070452 – Ňɏssa Pøngjǣrdenlarp Jan 17 '17 at 19:13
  • Not for nothing but you have 6 questions with 6 answers, but have accepted none of them. Accepting answers helps other users find good answers and says "thanks" to those who took the time to solve your problem. Upvoting *any* post answers you find helpful does the same. – Ňɏssa Pøngjǣrdenlarp Jan 17 '17 at 19:18
  • Hey Plutonix, thanks for that. Since I'm still relatively new to the site, I wasn't aware I could/should "accept" answers... now I see the little checkmark, I've accepted an answer for each question. Your link is also informative, I think I'd have to play around a bit more to really get my head around it and the scoping issues I have. – Zingapuro Jan 19 '17 at 21:48
  • If the [answer linked](http://stackoverflow.com/a/20077246/1070452) was helpful, click the up arrow next to it to upvote it. Upvoting also helps others find good answers. They dont have to be answers to *your* posts. The brief [Tour] explains it. – Ňɏssa Pøngjǣrdenlarp Jan 19 '17 at 21:52

1 Answers1

1

When you write your own code, using Form1 in that way references the default instance. The debugger doesn't support default instances. You would need to assign the default instance to a field or property and then watch that.

One reason that the debugger doesn't support default instances may be that they are thread-specific. If you break multiple threads and then wanted to watch a default instance, for which thread would you watch it? I guess that you could default to the UI thread but then you are precluded from watching any other and it may also not be considered obvious enough.

jmcilhinney
  • 50,448
  • 5
  • 26
  • 46