Refactoring/renaming within comments in Visual C# Express - c#

When refactor->renaming MyMethod in the following example in Visual C# 2010 Express, the method name 'MyMethod' in the comment won't be changed.
Usually that a comment isn't touched by refactor/rename is for good reasons, since how should the IDE know that it's not just some word but actually references the very method that it is refactoring.
So is there a way in which a word in a comment can be bound to a class/method/variable name within Visual C# 2010 Express, so that it's changed as well during refactoring/renaming?
If there are several ways what would be the easiest/cheapest way?
(cheapest - in case of plugins or commercial versions)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
/// text for class MyClass
public class MyClass
{
/// <summary><c>MyMethod</c> is a method in the <c>MyClass</c> class.
/// </summary>
public static void MyMethod(int Int1)
{
}
/// text for Main
public static void Main()
{
Console.ReadLine();
}
}
}

At least in the commercial editions, the rename dialog (press F2) has an Search in Comments checkbox.

Not Sure about the Express edition, but VS 2008 professional edition we can do it:
http://www.thereforesystems.com/refactoring-in-visual-studio-2008/

According to wikipedia, the Renaming refactoring mentioned in other answers should be available in the Express edition. So try Ctrl-R-R, F2 or right-click/Refactor/Rename.

Related

Avoid auto generated namespace in Visual Studio. How to turn off automatic insertion of "using System;"

I am using VS Community 2017 Version 15.2
When I use the light-bulb suggestions, or manually press (Ctrl + .), it generates a method like this:
using System;
public void f() {
throw new NotImplementedException();
}
I want it to be this way:
public void f() {
throw new System.NotImplementedException();
}
Without the:
using System;
I looked through the settings and also online but could not find the option.
I want this because I like keeping my namespace clean. I don't want to manually delete the using statement every time I use this handy feature.
It is not a common behaviour, and Visual studio does not provide it. If you use Resharper, you can set it manually.
Resharper -> Option
(in option window) Code Editing -> C# -> Code Style
Find Reference qualification and check prefer fully qualified references

Detect c# version at compile time

I have an old line of c# code that looks basically like this:
foo.set_Parent(parent);
It has compiled fine for years. Now in VS2015 I get the error:
CS0571 'Foo.Parent.set': cannot explicitly call operator or accessor
So I can rewrite the line as:
foo.Parent=parent;
This builds fine in VS2015, but in VS2013 it gives the error:
'Foo.Parent' is not supported by the language; try directly calling
accessor methods 'Foo.get_Parent()' or Foo.set_Parent(Foo)'
So the simple fix is to simply ifdef these two lines based upon which version of the compiler is running. But how do you detect which version of the compiler is executing?
And for the record, no, I can't just dictate that everyone on the team simultaneously upgrades to VS2015.
Additional info -
For everyone smelling a rat, I'll go ahead and drag out the ugly truth, although I don't think it will change much of anything. The class Foo is from an ancient Borland assembly that is all bound up in Delphi (and yes, we're migrating away but not there yet). So the actual code, that compiles up to VS2013, looks like this:
using Borland.Vcl;
using RepGen;
using SnapReportsForm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
namespace MigrantCOM {
[ComVisible(true)]
[Guid("48245BA3-736B-4F98-BDC5-AD86F77E39F4")]
[ProgId("MigrantCOM.Exports")]
[ClassInterface(ClassInterfaceType.AutoDual)]
public class MigrantCLRExports { // : MarshalByRefObject
public string Test(string s) { return s+s; }
}
[ComVisible(true)]
[Guid("1154D364-B588-4C31-88B9-141072303117")]
[ProgId("MigrantCOM.SnapRepCOM")]
[ClassInterface(ClassInterfaceType.AutoDual)]
public class SnapRepCOM {
TRepGen repGen;
TStringList snapRefs=new TStringList();
TForm parent=new TForm(null);
TMemo designerMemo;
List<TReference> references=new List<TReference>();
TRunAsSnapContext runAsSnapContext=new TRunAsSnapContext();
public SnapRepCOM() {
designerMemo=new TMemo(parent); designerMemo.set_Parent(parent);
...
}
So the class being instantiated is Borland.Vcl.TMemo which is part of the old Delphi assembly.
I'm leaving this as an answer, linking an image will fit better here than in a comment.
So if you want to use VS 2015 but still use the same good ol' version of the C# language that worked for years, you can configure your project to target a specific version:
This adds <LangVersion>5</LangVersion> in the csproj.

How do I disable namespace abbreviation in Visual Studio 2015?

If I'm not mistaken, since Visual Studio 2015 there's some new feature that grays out redundant parts of namespace usings. Also, when you automatically add an using to some assembly member using quick actions, the grayed part is omitted.
For example, the whole Whatever.Framework.Shared.Data.Mongo would be added as just Shared.Data.Mongo when using quick actions.
Is it possible to completely disable this refactoring feature?
You will see this even going back to Visual Studio 2012 (and possibly older) under the following circumstances:
namespace Test.Foo {
using Test.Foo.Bar; // can write using Bar
public class Class1 {
}
}
vs
using Test.Foo.Bar;
namespace Test.Foo {
public class Class1 {
}
}
The first will grey out the Test.Foo. part in the using statement, as you are inside the namespace declaration. The second will not.

Can someone provide an example for the ImageCompare methods?

I'm attempting to compare two images using Visual Studio 2013 Pro. The MSDN provides information (http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.uitesting.imagecomparer.compare.aspx) on ImageComparer.Compare, alas I'm failing to get it implemented in my code. On the last line of my code I'm told that "The name 'Compare' does not exist in the current context". Can someone please help? Thanks!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using Microsoft.VisualStudio.TestTools.UITesting;
namespace Intranet.SmokeTests
{
public class Intranet_Login : Intranet_Setup
{
public List<string> IntranetLoginTest(string BrowserURL, string Host, int Port)
{
Image expected = Image.FromFile(#"\\webdriver\ImageVerification\Expected\IntranetHome.png");
Image actual = Image.FromFile(#"\\webdriver\ImageVerification\Actual\IntranetHome.png");
bool equal = Compare(actual, expected);
}
}
}
You must do it like this:
bool equal = ImageComparer.Compare(actual, expected);
When you want to use a class's static member in c# you must always qualify it with the class first. Otherwise the compiler will try to locate the member on the current class.
Another problem you might be having with your IntranetLoginTest is that it's supposed to return an instance of List<string>, but it doesn't. I must also say I find it strange that you are making an image comparison test in a method that would suggest it performs authentication mechanisms testing.
1- Using nuget Install System.Drawing.Common
2- Reference Microsoft.VisualStudio.TestTools.UITesting.dll from C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\IDE\Extensions\TestPlatform
Image expected = Image.FromFile(#"2020-09-01_15h31_24.png");
Image actual = Image.FromFile(#"2020-09-01_15h31_30.png");
Image difference = null;
var isTestPass = ImageComparer.Compare(actual, expected, out difference);
if (!isTestPass)
difference.Save("diff.png");
Console.ReadLine();
Expected
Actual
Difference
It turns out that the correct version of the referenced dll was not being added. The complete answer is here: How can I make the namespace locally match what is listed on MSDN?

How can I change the default Visual Studio C# new class file template? [duplicate]

This question already has answers here:
How do you default a new class to public when creating it in Visual Studio?
(9 answers)
Closed 9 years ago.
Is it possible to change the template in Visual Studio 2010 so that the class definition is changed from:
class Class1
{
}
to:
public class Class1
{
}
When creating a new class via Add->Class in the context menu.
I would also ideally like to be able to create a class in one context menu click. I copy+paste existing class files to avoid the file dialog.
You could modify the following file:
c:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class.zip
It contains the template used when you add a new class. Inside the same folder you also have the template for interfaces: Interface.zip so that they are public by default. IIRC a restart of VS is necessary to pick the changes.
You can create your own template by putting a file in C:\Users\you\Documents\Visual Studio 2010\Templates\ItemTemplates\Visual C#.
For example, you can put "publicclass.cs" with this content :
using System;
using System.Collections.Generic;
$if$ ($targetframeworkversion$ >= 3.5)using System.Linq;
$endif$using System.Text;
namespace $rootnamespace$
{
public class $safeitemrootname$
{
}
}
In order to avoid the class dialog, you can use the smart tag. Anywhere you would to use an inexisting class, simply type the class name, and press AltShiftF10 to popout the "generate class" menu.
You have to manually edit the template files of Visual Studio.
See this link for a detailed HOW-TO.
This is possible as described here and here.
You might see some issues due to the Template Cache of VS - on how to deal with them see esp. the comments here.
An "official" source on how to do this can be found at http://blogs.msdn.com/b/oanapl/archive/2009/03/06/visual-studio-templates-add-new-item-to-project.aspx

Categories