When an object has the same name as its class in a

2019-06-20 05:35发布

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?

2条回答
你好瞎i
2楼-- · 2019-06-20 05:51

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. =]

查看更多
Juvenile、少年°
3楼-- · 2019-06-20 05:55

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/

查看更多
登录 后发表回答