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?
Select Edit > Refactor > Rename. Right-click the code and select Rename.
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.
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.
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.
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With