Visual Studio "rename" refactoring - c#

At the moment I have to write C# code in VS2012, (Before was: Java (Eclipse, IDEA)).
May be I am strange guy, but I like when class name == class file name.
Why in VS2012 no option to rename class file same as I renaming class name? (Or I could not find it)
I know that C# compiler do not care about matching class name and class file name, but I want to do it. I can`t find class by its file name if they are not same, it is annoying me very much aaaargh.
Help me please, I'm tired finishing work after VS

If you rename the file first, Visual Studio will usually offer to rename the class to match.
I'm not sure what the exact circumstances are, but if you're sticking to a single class per file it'll work most of the time.

You could use ReSharper. This plugin has option to rename file to match the class name.

It is a feature that VS sadly does not have. You can supply with a few refactoring plugin, like Resharper or CodeRush or Refactor! Pro...
They all come at a price and with their own fan base. CodeRush as far as I remember as a free version with very limited features, but maybe the "Rename File" is among them, you would need to try.
Another option would be to find an extension (or develop one) that does that for you, just head to the Visual Studio Gallery to find one. =)

You just point to the object you want to change, right click and use Refactor -> Rename. A popup refactoring window will appear. Choose your options and click "ok".

To address this line from Rawling's answer (since I don't have enough rep to just make a comment):
I'm not sure what the exact circumstances are, but if you're sticking to a single class per file it'll work most of the time
To clarify, Visual Studio will offer to rename and refactor your class when you change the file name as long as the class is defined within a namespace. If you've chosen not to use namespaces, you won't have access to this IDE nicety. This is true up to at least VS 2017 Pro (the newest as of this answer).

Related

How to refactor a file namespace?

I have a N-Layer project and when im defining the ContextBounderies i use to change it during the needs of the development. For example i have this class:
namespace Domain.Entities.Item
public class ItemForSale
{
}
This class is now inside the Entites/Item folder in the solution. But during the development I realize that this class will make more sense inside the Sell folder, so the namespace should be namespace Domain.Entities.Sell. Well, the problem is that i have lots of references of this class in a number of layers projects, so its always a pain to refactor it by hand.
Is there a way to configure it, or some free extension (resharper do that but its not for free) wich do this job?
Im using Visual Studio 2015
Thanks!
Yes, this is just a feature of VS2015. You can right click on the namespace you want to change, and then select the "Rename" options under the drop-down. It will update all of the references inside of the solution. So, if you have multiple projects inside of your solution, they will all get updated with the new name.
(Also, there is a keyboard shortcut as well. You can click the namespace you want to change, and then press F2 on your keyboard to bring up the Rename menu.)
If the problem is not frequent then you can use Replace in Files option to replace all old namespace occurences to new ones. Just use Crtl+Shift+H, fill appropriate fields and select Look in option to look in Entire solution.

Visual Studio 2010 renaming project and main forms

I guess a lot of people start working on something. And then later they find-out the names don't really cover the end product. Or well at least, I do choose often poor names for stuff, I start programming faster then thinking about things like that (in the end I often rename my buttons too so my code becomes more clear too).
What is the best way of a current work in progress (not finished) to rename it? Without breaking the code, or do a lot after editing.
The reason I ask it here is I've seen it gone wrong a few times, leaving me code that didn't work any more so I better as how to do this and where in which pane, it's best to rename.
So what's wrong? Well in the upper right pane of Visual Studio 2010 C# (solution explorer):
The solution itself has a wrong name
The next line of it has the wrong name
And the main form could be improved too
If you were to rename the main form it would ask you if you want to change all references to it, you say yes and that problem is solved.
Renaming the solution is equally simple, just right click in the Solution Explorer and rename. It won't break your local build or anything.
I don't know what "The next line of it has the wrong name" is referring to (the project maybe?) but the same probably goes for it.
Well usually you should change the file name of your class in the solution explorer and it should ask if you want to change all references, though sometimes it can go wrong so to double check you can replace all command to find all references of old name and replace with the new one..
http://msdn.microsoft.com/en-us/library/139eef4h.aspx
Also if you change in code, the name of a class or variable or anything and then you should see small line under the changed name, stand with the mouse pointer on the variable and press ctrl+. (ctrl dot) And that shoul pop up small options and the first one should let you change all references of that variable new name also:
http://www.dofactory.com/ShortCutKeys/ShortCutKeys.aspx

Changing CR/LF output type in Visual Studio

I have a c# project in my Visual studio 2008 which when built, outputs a bunch of files in a temp directory for other programs to use i.e. JsTestDriver, JsCoverage, etc (all a bit Unix-related).
Trouble is, everytime when the files were generated, they seem to contain weird return carriage/line feed which upsets the programs that would use them next.
What I'm currently doing is manually create a notepad file, rename it to a different name, then copy and paste the entire content of the generated file (and save) to solve this problem. This is, of course, tedious and not something I enjoy doing (ultimately I want this whole process to be automated).
So my question is: how do I get Visual Studio to output the correct/proper CR/LF so I no longer need to go through the manual process?
If the above is too vague, I'll be happy to clarify. Thanks in advance.
Yes, it's a bit too vague at the moment - you should provide:
More details about how you're writing out the files
More details about exactly what's in the files afterwards. Use a hex editor to find this out.
The simplest way of changing what happens in terms of line endings is probably to just be explicit about it in the code. For example, instead of:
output.WriteLine(foo);
write
output.Write(foo);
// Here LineTerminator would be a constant - or you could make
// it a variable somewhere
output.Write(LineTerminator);
... possibly encapsulating this in your own class to make it easier (so you can have your own WriteLine method which does the right thing, perhaps).
EDIT: I've been assuming that it's your own code writing out the files. If that's not the case, the easiest solution is probably to find or write a tool to convert the files, and put it as a postbuild step. I'm sure such tools exist (I think there was one called dos2unix a while ago) but it may be as easy to write your own as to find one which does exactly what you want.

Navigate files with VisualStudio 2008

I am currently developing with VisualStudio 2008. I find it increasingly difficult to find files when the number of files and projects increase. The situation become so bad that I literally have to spend 10-20 seconds to find code I want to work on when switch between files. Sounds like not a lot of time, but this can be very annoying because it breaks the "coding flow".
I believe this is a general problem with all IDEs. But I still find it easier to work with Eclipse. One particular example is that in Eclipse, I could apply some key shortcut to find implementations defined in an Interface.(I have forgotten which key shortcut though, after not working with it for about 1 year.) Another example is that in VS 2008, rightclick->Find all references will find all occurrences containing the selected text(Update: This accusation is incorrect. Thanks for pointing it out); while similar operation in Eclipse will find all occurrences where the selected class/variable are referenced. Personally, I find the Eclipse way is better for file navigation.
This could be due to my inexperience with VS IDE. So, I am looking for suggestions to improve the file navigation experience so that I can get some more time for actual coding.
I have used the navigate buttons but they are only useful for small number of files.
Thanks.
I agree wholeheartedly about ReSharper. If you don't want a "paid" solution though, a useful Visual Studio shortcut is
CTRL + ,
it gives a list of recently edited files.
CodeRush is a free alternative to ReSharper which you might find useful too.
Kindness,
Dan
Have a look at Jetbrains Resharper. Ctrl N or Ctrl Shift N. To jump quickly to files by type name or by file name. Also you can control Click Classes to navigate to them instantly.
I can't even work in visual studio without it anymore. It virtually eliminates the need for the solution explorer
SamTools for Visual Studio 2005/2008 (and I have a build for 2010 if someone needs it). The particular feature that's handy here is the Solution File List.
You can right click the tab of any open document and Find In Solution Explorer or Show In Windows Explorer
You can right click in the Solution File List for the same
Unlike other solutions to this problem, this is a properly docking tool pane so it fits in great. Ctrl+Alt+F places you in the Filter box with the current text selected, so it's very easy to work with.
Oh and it's free/GPL. :)
Solution File List http://wiki.pixelminegames.com/images/8/88/SamTools_SlnFileList.png
Dpack is also a free alternative with some nice file navigation shortcuts, but Resharper is the bomb.
BTW, they should have some more of this stuff in vs2010, which is in beta right now (I think)

Programmatically changing file icons in Visual Studio 2005's solution explorer

I'm writing an add-in for Visual Studio, which must be compatible with VS 2005. This add-in involves marking certain files as "special", and I'd really like to make their specialness visually apparent in the Solution Explorer - currently my users either have to manually keep a note of which files are special, or right-click on each potentially special file and see what menu items are there.
Ideally I'd like to change the icon, but I'd be happy to (for instance) make the file's name bold or something.
Unfortunately, all the appearance-related methods seem to live on Microsoft.VisualStudio.Package.FileNode, and I only have access to UIHierarchyItems and ProjectItems. I've spent several days grovelling through MSDN, and I can't find any way of constructing a FileNode given a ProjectItem or UIHierarchyItem. There's a suggested solution on the MSDN forums, but it assumes you're starting with a FileNode, and as far as I can tell this implies creating a custom project type (and presumably some sort of project-conversion system). I don't think my users would be too happy with this.
Or is there something I'm missing?

Categories

Resources