6

I am maintaining some VB.NET code and the previous developer has gone and done this sort of thing. Dim frmDuplicates = New frmDuplicates or even Dim frmDuplicates = New FrmDuplicates (which makes no difference because of the case-insensitivity of VB).

I don't like that as a coding convention, since I want to distinguish my instance method calls from class ones at a glance.
This is not the same as naming public properties the same as their type. I don't have a problem with that.
Also in Visual Studio 2010 in a VB project if your object is the same name as the class name (disregarding case of course), then Intellisense does not highlight the class reference in a different colour, as it does in a C# project. In addition if you rename the object reference (in a VB project), it will also rename any class references. In C# it will correctly rename only the object references.

For the following code,

Module Module1
    Sub Main()
        Dim TestClass As TestClass = New TestClass()
        TestClass.SharedMethod()
        TestClass.InstanceMethod()
        Console.ReadKey()
    End Sub
End Module

Public Class TestClass
    Public Shared Sub SharedMethod()
        Console.WriteLine("SharedMethod()")
    End Sub
    Public Sub InstanceMethod()
        Console.WriteLine("InstanceMethod()")
    End Sub
End Class

If you rename the object reference TestClass, it will also rename the class reference TestClass.SharedMethod() So if you rename it to test for example, you'll have

test.SharedMethod() ' You will get a warning on this line
test.InstanceMethod()

VB allows you to call class methods from object references (which is not a good idea IMO), but you'll get a warning at least.

Access of shared member, constant member, enum member or nested type through an instance; qualifying expression will not be evaluated.

That can be a problem

So my question is, how do I rename object references, and not class references in a VB project, when the object name is the same as the class name? Is find / replace my only option? (regular expressions in find / replace have been useful for me in the past). Or is there something else within Visual Studio I can use?

Community
  • 1
  • 1
Jason S
  • 1,361
  • 20
  • 24
  • 1
    At least it does become a warning. If it's generating too many warnings, it makes me suspect more may be wrong with the codebase. – Damien_The_Unbeliever Aug 25 '11 at 06:42
  • @Damien Well the codebase is not pretty. I'm maintaining an app that started its life in VB6. Can't go into the other problems here, but on some forms, if I rename the object variable, I'll get 60 warnings because there are 60 calls to shared methods for that class (a custom logger). Then I'd have to go through by hand correcting them. That is made quicker by the warning suggesting a change to the class name, but still, that is just one file. Yes it shouldn't be like that, but this question is not about a complete re-engineer of the app I'm maintaining. I'd like to focus just on this issue. – Jason S Aug 25 '11 at 07:18

2 Answers2

1

I can't guarantee that it will work perfectly in your situation, but you may want to try the free CodeRush XPress which adds refactoring support for VB. See http://www.devexpress.com/Products/Visual_Studio_Add-in/CodeRushX/

Jim Wooley
  • 10,169
  • 1
  • 25
  • 43
  • @Jonathan and Jim, The CodeRush tools look good, but unfortunately the rename refactoring in CodeRush acts the same as the built-in Visual Studio rename. That is in a VB project it will rename both object and class references if the name of the object is the same as the class (not considering case in VB). This is why you should never name an object the same as its Class! Still if you do rename you will get compiler errors on the Class reference calls which helps a bit, but can be tedious if you have hundreds or thousands to go through. – Jason S Sep 12 '11 at 05:04
0

First thing's first: find that previous dev, no matter how long it takes, and slap him. I'm okay with someone naming an instance of ThisClass as thisClass, but that's as far as that goes.

Try changing the name of the class itself and then sifting through the warnings for "Access of shared member..." It's cumbersome, but that's why good programming practices exist. If there's a way to filter warnings, that'd be very helpful. I don't know of one. I'll edit if I find one.

EDIT: you can always sort the errors in the pane. =]

Yatrix
  • 13,361
  • 16
  • 48
  • 78
  • 1
    The previous dev is my current supervisor and will be my referee when I leave this job. I don't think slapping him one will do me any good. In VB giving an object the name of `thisClass` or `ThisClass` is the same since VB is case insensitive, so `Dim thisClass = New ThisClass` is as bad as `Dim ThisClass = New ThisClass`. I can rename the object reference and go through the warnings but there are going to be hundreds, maybe thousands across all variables and all files, so I'm looking for something a little less time-consuming. – Jason S Sep 01 '11 at 22:01
  • He still deserves a slap. =) I meant "thisClass" in general, is okay to name a variable imo. I do that often, but it'd never occur to me to make the exact same name as the class. I don't blame you for not wanting to sift through 1000's of anything. Makes me happy to be programming in C# now instead of VB. Some things just shouldn't be allowed. Hope you get it worked out - good luck. – Yatrix Sep 02 '11 at 12:45
  • You can still come a cropper in C# if you use the same name. From your post and comment it still appears you don't recognise that `thisObject` and `ThisObject` is considered the same name by the VB compiler. Hence you say it is ok to do `Dim thisClass As New ThisClass()` and that you do that often, but to the VB compiler that is tantamount to `Dim ThisClass As New ThisClass()`. – Jason S Sep 13 '11 at 05:10
  • No, I get it, dude. When I said "in general", I meant where it's permissible without consequence - not your case obviously. I wasn't claiming to be beating the compiler with the power of my will... I use the naming convention often in C#, which is why I said I'm happy to be using that instead of VB. Sorry to confuse you. – Yatrix Sep 13 '11 at 15:08
  • [No worries mate](http://en.wikipedia.org/wiki/No_worries). I get it now that you get it. Just that this question was only tagged VB and no C# code here, so when you said "in general" I thought you were also talking VB. One of my biggest gripes about VB is case insensitivity, oh yeah, and verbose syntax, and... oops better not start. I'd rather be doing more C# too (I do a bit). – Jason S Sep 13 '11 at 22:19