Vadim's Weblog

Never stop learning.

Archive for the ‘VS2005’ Category

Incremental search in Visual Studio

Posted by Vadim on October 18, 2008

When you’re writing code in VS environment, I bet that you often use Ctrl+F keystroke to display “Find and Replace” dialog box.

There’s a better way.  Next time you need to look for some text in your code, press Ctrl+I and start typing your search.  After the text is found, you can use F3 key to find a next occurrence.

As soon as you press Ctrl+I, you’ll see “Incremental Search:” labelIncrementalSearch on the left part of VS status bar.  Your search text will appear on the status bar as you type it.  If you mistype your search text, you can use Backspace key to correct wrong characters.

kick it on DotNetKicks.com

Posted in Tips And Tricks, Visual Studio, VS2005, VS2008 | Tagged: , , | Leave a Comment »

Explaining GetHashCode method.

Posted by Vadim on July 5, 2008

Every object you ever created or used in .NET has GetHashCode method along with Equals, GetType, and ToString methods.  This method is an instance method of an Object class from which any other class derives.

GetHashCodeIntelisense

GetHashCode() method returns an integer that identifies an object instance.  I also can repeat MSDN documentation but you can read it on your own.

You would want to override this method along with Equals() method in your class if the object of your class is going to be used as a key in Hashtable.

The best way to explain it is to show an example. In I was using class Point as an example.  I’m going to continue with this class.  So let assume that we have class like this:

   1: public class Point
   2: {
   3:   private readonly int _x;
   4:   private readonly int _y;
   5:
   6:   public Point(int x, int y)
   7:   {
   8:     _x = x;
   9:     _y = y;
  10:   }
  11: }

Next we want to use Point as a key in a Hashtable.  Remember that the key of the Hasthable must be unique.  In the code bellow we have two identical keys (line 2 & 4). We should expect ArgumentException.

   1: Hashtable hashtable = new Hashtable();
   2: hashtable.Add(new Point(2, 3), "Point 1");
   3: hashtable.Add(new Point(5, 3), "Point 2");
   4: hashtable.Add(new Point(2, 3), "Point 3");

However, no exception was thrown.  There are two reasons why exception is not thrown:

  1. Equals() method that is also an instance method of Object class will always indicate that Point in line 2 and line 4 are different. This expected because two objects have different references.
  2. GetHashCode() method returns different number for these objects.

Try to execute code bellow:

   1: static void Main(string[] args)
   2:  {
   3:    var point1 = new Point(2, 3);
   4:    var point2 = new Point(2, 3);
   5:    Console.WriteLine("point1 Hash: {0}"
   6:         , point1.GetHashCode());
   7:    Console.WriteLine("point2 Hash: {0}"
   8:         , point2.GetHashCode());
   9:    Console.WriteLine("point1 equal to point2: {0}"
  10:         , point1.Equals(point2));
  11:  }

You will get output similar to this one:

point1 Hash: 58225482
point2 Hash: 54267293
point1 equal to point2: False

You can see that we got different hash codes.

To solve this problem we override Equal() and GetHashCode() in our Point class.

First we override Equals() method:

   1: public override bool Equals(object obj)
   2: {
   3:   if (ReferenceEquals(null, obj)) return false;
   4:   if (ReferenceEquals(this, obj)) return true;
   5:   return ((Point)obj)._x == _x && ((Point)obj)._y == _y;
   6: }

You can see that I use ReferenceEquals() method that is static method of Object class.  It checks if the specified objects are the same.

Next we override GetHashCode() method:

   1: public override int GetHashCode()
   2: {
   3:   unchecked
   4:   {
   5:     return (_x * 397) ^ _y;
   6:   }
   7: }

The whole Point would look like this:

   1: public class Point
   2: {
   3:   private readonly int _x;
   4:   private readonly int _y;
   5:
   6:   public Point(int x, int y)
   7:   {
   8:     _x = x;
   9:     _y = y;
  10:   }
  11:
  12:   public override bool Equals(object obj)
  13:   {
  14:     if (ReferenceEquals(null, obj)) return false;
  15:     if (ReferenceEquals(this, obj)) return true;
  16:     return ((Point)obj)._x == _x && ((Point)obj)._y == _y;
  17:   }
  18:
  19:   public override int GetHashCode()
  20:   {
  21:     unchecked
  22:     {
  23:       return (_x * 397) ^ _y;
  24:     }
  25:   }
  26: }

Now if you try to execute the application the output should look exactly like this:

point1 Hash: 793
point2 Hash: 793
point1 equal to point2: True

Also if we try to add new Point(2, 3) twice, we get an ArgumentException as expected.

kick it on DotNetKicks.com

Posted in .Net, C#, Coding, GetHashTable, VS2005, VS2008 | 15 Comments »

Where’s my R# File Templates?

Posted by Vadim on May 27, 2008

After installing ReSharper 4.0 Beta over a previous version, my user defined File Templates disappeared.  I was able to use my user defined Live Templates.  After some investigation, I was able to figure out how to get them back.

Actually they didn’t disappear, I just had to associate them with project types.

reShrp_template

In case the same happens to you and you cannot find your File Templates here are the steps to get them back.

1. Follow the same steps as you would do when a adding a new item:

Click right button on your project in Visual Studio and select Add -> New from Template -> More… .

Now you should see ‘Choose Template’ dialog.

 

2. In ‘Choose Template’ dialog click on ‘Add to quicklist’ check box and select the type of the project your template should belong.  In my case I had to select ‘C# Projects’

choose_template

I just want to mentioned that this behavior I’ve happened in VS2005, I haven’t check what happens in VS 2008.

Posted in ReSharper, VS2005 | 1 Comment »

VS 2005 fails to load.

Posted by Vadim on May 23, 2008

On two different occasions I was asked to help when VS 2005 would not load.  We were able see only VS 2005 splash screen and that’s it.  In first case the CPU usage by devenv.exe was 25%.  To resolve this problem I launched VS in safe mode. 

C:\Program Files\Microsoft Visual Studio 8\Common7\IDE>devenv.exe /SafeMode

This prevents all third-party VSPakages from loading.  After that VS 2005 was loading properly all the time.

I believe that in both cases it happened after the developer installed an add-in.

Posted in Visual Studio, VS2005 | Leave a Comment »

How To: Create a ReSharper File Template.

Posted by Vadim on April 1, 2008

ReSharper is a wonderful tool that makes writing code much easier.  One of the features of this tool is ability to create templates.  With ReSharper you can create Live, Surround With, and File Template.  In this post I want to share with you how to create a File Template for MbUnit Test Fixture.

MbTest Template Selection After completion of this tutorial, you’ll be able to create an MbUnit Fixture by right clicking the test project and adding the test fixture.

 

 

Step 1: Open ReSharper – Options dialog.

Start Visual Studio and from menu bar select ReSharper – Options (you also can get there by pressing Alt-R + O).

ReSharper_Options.

Step 2: Create a new template.

  • In the left panel select File Templates under Templates section.
  • Then select User Templates in Available templates.
  • In this sample we’re going to create C# template, so select Class (C#) in Quick access list (it should be the first item in the list).
  • Last click on Create Template icon ReSharper Create Template icon to see Edit Template dialog box.

Step 3: Create and save MbUnit Test Fixture template.

  • In Name text box enter MbUnit Fixture.  It’s a name that will appear in the menu when you add a new test fixture.
  • In File name prefix text box enter MbTest.  It’s going to be a suggestion for your test file / class.

ReSharper Edit Template General 

ReSharper Template OptionsNext click on everywhere link next to the word Available to see the Template Options dialog box.  Select Only in projects for language radio button and chose C# projects including ASP.NET C#projects) from the drop-down control. Extension for created files leave as cs.

Now we are ready to create actual template.

Enter the code below into Template text.

   1: using MbUnit.Framework;
   2:  
   3: namespace $NAMESPACE$
   4: {
   5:    [TestFixture]
   6:    [FixtureCategory("$CATEGORY$")]
   7:    [Author("Vadim Kreynin", "Vadim@kreynin.com")]
   8:    public class $CLASS$ {$END$}
   9: }

I’m sure that you noticed $NAMESPACE$, $CATEGORY$, and $CLASS$.  These three are ReSharper variables.  You declare a variable by surrounding a word with $ sign.

The reason we created variables because we want to do something with them.

ReSharper has automatically created entries for our variables that looks like this:ReSharper Edit Template Variables

Click on Choose macro link and choose following options in Choose Macro dialog box.

Variable Available macro
$NAMESPACE$ Default namespace for current file
$CATEGORY$ Constant value
$CLASS$ Current file name without extension

Uncheck Editable Occurrence check box for NAMESPACE variable.

You can see that CATEGORY variable has a red link Constant value.  Click on it and enter Category in String value text box of Choose Parameter Value dialog box.

You’re done!  Click on OK button of Edit Template dialog box that should look like this: ReSharper Edit Template

Now you can start using this template.

Next time I’ll show how to create a Live Template for tests, test SetUp(s) and TearDown(s).

 

kick it on DotNetKicks.com

Posted in .Net, MbUnit, ReSharper, Visual Studio, VS2005, VS2008 | 5 Comments »

Copy and Paste in Visual Studio and MS SQL Server Management Studio.

Posted by Vadim on March 21, 2007

I realized that many people don’t know how easy Visual Studio or SQL Server Management Studio allows us to copy, cut & paste. I’m sure that many of you know that but if you don’t, please give it try I’m sure that you’ll save some time.

Even my mother knows Ctrl-C, Ctrl-X, & Ctrl-V shortcuts to copy, cut, & paste. But do you know that you don’t need to select a line to copy/cut it. Just move cursor to the line you want to copy/cut and press Ctrl-C or Ctrl-X, the whole line will be copied into the clipboard.
We copied a line, now we can paste it. The line always will be posted above the line where you cursor is. And you cursor can be in any position on that line; I repeat ANY position of the line.
Do you know that you can cycle through the clipboard ring? Here’s the example where we are going to swap ‘var1’ and ‘var2’ words:

public void foo()
{
string s1 = "var1";
string s2 = "var2"
}

  • First double click on word ‘var1’, to select it and then press Ctrl-C, to put the selection into the clipboard.
  • Next double click on word ‘var2 and press Ctrl-C again. Now we have both selections in the clipboard.
  • Now double click on word ‘var1’ again and replace it with ‘var2’ by pressing Ctrl-V. So far nothing new.
  • Last, double click on word ‘var2’ on the next line where we’re declaring variable s2 and press Ctrl-Shift-V twice (it means two times :) ).

kick it on DotNetKicks.com

Posted in Tips And Tricks, VS2005 | Tagged: , | 9 Comments »