Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When an object has the same name as its class in a VB project, how do I rename the object without also renaming any calls to shared methods?

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?

like image 901
Jason S Avatar asked Aug 25 '11 04:08

Jason S


People also ask

How do I rename a class in Visual Studio?

Select Edit > Refactor > Rename. Right-click the code and select Rename.

How do I rename a project in Visual Basic?

Right-click on the project name & click reload the project. Then rename the project name as highlighted below & save. It will automatically rename the . csproj file.

What is an object in VB explain how do you control the objects with their properties?

An object is a type of user interface element you create on a Visual Basic form by using a toolbox control. In fact, in Visual Basic, the form itself is an object. Every Visual Basic control consists of three important elements − Properties which describe the object, Methods cause an object to do something and.

What is class and object in VB net explain this through an example?

Objects are the basic run-time units of a class. Once a class is defined, we can create any number of objects related to the class to access the defined properties and methods. For example, the Car is the Class name, and the speed, mileage, and wheels are attributes of the Class that can be accessed by the Object.


1 Answers

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/

like image 56
Jim Wooley Avatar answered Nov 09 '22 09:11

Jim Wooley