How to rename a class in VbClassic
Doing the RenameClass
refactoring involves more than just a global search and replace when you're using VbClassic
Here's a quick and easy way to do it:
- Copy the original class into a new .cls file
- Do the search-replace
- Run the tests
- Check in the new file
- Delete the old file from the project
- Check in the project file
- Delete the old file from source control and the file system.
The advantage is that once the new file is set up and working, you can do the rest without worrying about breaking anything.
This will probably work better, in the presence of labels and other advanced usage, than renaming. Few source code control products are very good at handling rename operations. OTOH, it may be a little more difficult to see the history of changes to a given class.
Oops, good point.
Here's a slightly more complex but more "air tight" way to do it -- particularly if your VersionControl
software supports "rename" operations:
- Ensure that you have write access to the class and all files that reference it. (Typically, this means that you must "check out" these files.)
- In the class' properties, change the class' name.
- Do a search and replace, changing the old class name to the new one. Typically, you'll search and replace the whole of the current project. If it's an externally visible class or interface, you'll have to search and replace in other projects.
At this point the system should compile and successfully pass all UnitTest
But the file name for the class no longer matches its class name.
When checking in your changes, a good approach is...
- Remember to check in the project (.vbp) file last.
- Check in the class whos name is changing. You can enter checkin comments that you're changing the class' name.
- Delete the class from the project.
- Rename the class in the source code control system.
- Rename the class in the file system.
- Add the class back into the project. (Add -> Class Module, Existing, open the file with the new name)
- When finished, check in the project file.
will become confused when you check out files in the middle of a project-wide search:
It will report that you've found all instances, when you really haven't.
So, you'll either have to do another round of searching, or rely on another method, such as bookmarks or open windows to keep track of what files need to be checked out.
A similar process can be used to rename projects, when project group files are being used.
In the instructions above, I've documented the "safe, sane, rational" way to do it.
To be honest, I normally open the project file in Notepad or VIM and edit it by hand.