Moving code from VB 2003 to VB 2005… Accessing Shared members from Instances…

Lately I’ve been luckily enough to be able to reuse several bits of code I’ve written in Visual Basic 2002 and 2003, namely code from a few of my earlier MSDN articles (Design Surface and the Control series). Since I was planning to make a few changes, I pulled these projects into Visual Basic 2005 Beta 1 (instead of just referencing the already existing assemblies created with the .NET Framework 1.0/1.1). I also was in the midst of writing a new control for an upcoming MSDN magazine article, that I was also planning to use in my ‘real’ work, so I pulled the finished code (written in VB.NET 2003) into VB 2005… In every case, every single thing worked fine without any changes except for the same reoccuring issue;

I have a habit of accessing shared members off of instances, sometimes without even realizing it. Well, this ‘feature’ is no longer possible in VB 2005. The IDE will pick up on every place you’ve done this in your code, and it is very easy to fix, but each and every one of my projects opened from VB.NET 2002/2003 has come up with a few of these errors.

The most common culprits (for me)? Using Me.MousePosition() off of a Form instance, when this is really a Shared member of the Control class, and using the .Copy method of an array off of the array variable instead of off of System.Array. For the first issue, a quick change from Me.MousePosition to Control.MousePosition and that problem is gone, and for the second I just have to replace the array variable name with System.Array… so it isn’t a difficult fix in either case.

The best part of this experience is that just knowing about this change in VB 2005, will (hopefully) change the way I code in VB.NET 2003, which is probably a good thing considering everything Paul points out in his post on the topic.

Moving code from VB 2003 to VB 2005… Accessing Shared members from Instances…

2 Responses

  1. I remember that you used to be able to do the opposite for some of the early betas for C#, that is, accessing instance members from class reference as long as the method didn’t actually touch the instance itself (didn’t use this. or Me.). I’m pretty glad that fixed that before it was released…

    David M. Kean March 28, 2005 at 7:55 am #
  2. It also applicable, only with some warnings.

    Fan March 28, 2005 at 6:32 am #

Leave a Reply