2 minutes
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.
Thoughts on this post? Feel free to reach out on Bluesky!