My Faq on "hashtable lookups for struct types" is published at http://blogs.msdn.com/CSharpFaq
Check out
http://blogs.msdn.com/csharpfaq/archive/2006/03/20/556192.aspx
for a FAQ on Hashtable lookup for value types.
Check out
http://blogs.msdn.com/csharpfaq/archive/2006/03/20/556192.aspx
for a FAQ on Hashtable lookup for value types.
Many a times, we use the catch block inside the try catch block for our clean up code.
Something like
try
{
// Do something
}
catch
{
// work failed, clean up code here
}
Rather than the above approach of using the catch block, it would be nicer to use the finally block, something like
bool workSuccessful = false;
try
{
// do some work
workSuccessful = true;
}
finally
{
if(!workSuccessfull)
{
// cleanup code here.
}
}
There is elegance in the latter method and I would certainly recommend that approach, if you cannot use “using“. See below for details.
PS: Use this approach only if better alternatives are not available. One of the automatic cleanup approaches available with C# is the using construct.
Something like,
using (TextReader tr = new StreamReader(“FileName”))
{
// do my work here.
}
The “using” construct automatically clean up the unmanaged resource (TextReader) once the block has completed execution.
In the event that you cannot use “using“, the try-finally approach would be the best way.
Do you want your application not in appear in Add/Remove Programs list?
If yes, ARPSYSTEMCOMPONENT property of the Windows Installer SDK helps.
Setting this property to 1 prevents the installed application to be displayed in the Add/Remove Programs List.
More information on this key is available at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/msi/setup/arpsystemcomponent.asp
On a side note, i fyou want a quick access to Add/Remove Programs List, type appwiz.cpl at your Start > Run prompt.
Frequently we desire to autopopulate some fields on our web form with the current logged in username and domain.
How to get that information?
Well, User.Identity.Name comes to the rescue.
Set the text property of the field to User.Identity.Name and it will show the logged-in username prefixed by the domain information
You notice that you have a chuck of code which could easily be transitioned to a new function. How tdo you do that?
Again, Visual Studio Refactoring menu comes to the rescue.
Suppose you have the following code in your function
public
void Myfunc(){
}
We realize that code containing Console.Writeline is replicated. Select one set of the Console.Writeline instructions and right click > Refactor > Extract Method…
Type the name of the new function you want to create containing the selected lines and Click OK.
A new method containing the selected lines is created. So your code will look like
public
void Myfunc(){
NewMethod();
}
private
static void NewMethod(){
}
Keyboard shortcut: Ctrl R + Ctrl M
Cavaet: You will have to delete the second set manually as currently VS editor is not smart enough to replace all the occurances of the selected lines. Maybe in the next version we can get that feature.
Did you mistype a variable/function/property only to realize it in the code review and are frustrated over the time you will need to spend to correct it across the whole source code?
Visual Studio 2005 has a new feature called refactoring by which you can rename a property/function/variable at one location and the same will be replicated across all the location where the property/function/variable is referenced.
To do that, select the property/variable/function you desire to rename and right click and select Refactor > Rename. A Rename window will appear and you can select whether you want to preview the reference changes, or you want to change the entity in the comments also.
Keyboard shortcut: Ctrl R + Ctrl R
Here is a C# code snippet to determine if a particular DLL is registered or not.
[DllImport(“kernel32”)]
public extern static int LoadLibrary(string lpLibFileName);
[DllImport(“kernel32”)]
public extern static bool FreeLibrary(int hLibModule);
public bool IsDllRegistered(string DllName)
{
int libId = LoadLibrary(DllName);
if (libId>0) FreeLibrary(libId);
return (libId>0);
}
Source: http://blogs.msdn.com/asanto
Time and again we write code and our brackets get out of visual sync, i.e. they no longer appear as a coherent set even though they may be.
In Visual Studio, there is a feature known as Format Document which will align the code systematically.
It can be invoked by the key combination of Ctrl K + Ctrl D
Suppose you code looks like
namespace LogFileCheck
{
class Program
{
static void Main(string[] args)
{
TextReader sr = new StreamReader(“mb20051116_05000600_BAYTRARPT03_k.msn.com_w3svc10000.log”, Encoding.UTF8);
TextWriter writesr =
new StreamWriter(“mb20051116_05000600_BAYTRARPT03_k.msn.com_w3svc10000_csResult.log”,
false, Encoding.UTF8);
while (sr.Peek()
!= -1)
{
string line = sr.ReadLine();
if (Regex.IsMatch(line, “&di=78”) && Regex.IsMatch(line, @”([^,]*,){19}66″))
writesr.WriteLine(line);}
sr.Close();
writesr.Close();
}
}
}
Press the magic keys Ctrl K + Ctrl D and voila, all your code looks pretty organized as under:
namespace LogFileCheck
{
class Program
{
static void Main(string[] args)
{
TextReader sr = new StreamReader(“mb20051116_05000600_BAYTRARPT03_k.msn.com_w3svc10000.log”, Encoding.UTF8);
TextWriter writesr = new StreamWriter(“mb20051116_05000600_BAYTRARPT03_k.msn.com_w3svc10000_csResult.log”, false, Encoding.UTF8);
while (sr.Peek() != -1)
{
string line = sr.ReadLine();
if (Regex.IsMatch(line, “&di=78”) && Regex.IsMatch(line, @”([^,]*,){19}66″))
writesr.WriteLine(line);
}
sr.Close();
writesr.Close();
}
}
}
Want to format only a small selected section of the dirty code? Select the area you want to format and press Ctrl K + Ctrl F.
How often do you wish that you could put a mark at a particular location in your source code and then switch to that point with a simple click?
With Visual Studio, you can do that with the help of bookmarks.
A bookmark is a virtual placeholder which notes the position where you place one and you can quickly go to that position from anywhere in your source files by just a few keyboard clicks.
How to define a bookmark?
To create a bookmark, press Ctrl K + Ctrl K
This creates a book mark in the code. This is indicated by a blue button like indication on the left side of the line where you placed the bookmark.
Now to switch bookmark from any portion of your code, just press Ctrl K + Ctrl N
To go to previous bookmark, press Ctrl K + Ctrl P
To unmark a particular bookmark, navigate to that bookmark and press Ctrl K + Ctrl K (yes, this is the same combination you used to create a bookmark, what you are currently doing is toggling a bookmark)
To clear all your bookmarks, press Ctrl K + Ctrl L
If you have many files in your current folder and you want to navigate to the next bookmark in the folder, the shortcuts for next bookmark in folder is Ctrl Shift K + Ctrl Shift N and the previous bookmark in folder is Ctrl Shift K + Ctrl Shift P
All the above options are also available from the menu, Edit > Bookmark > ….
If you want to see all the bookmarks in one window, Go to View -> Bookmark Window (Ctrl K + Ctrl W). And click on the bookmark you want to go to.
Happy coding.
This post is aggregated at http://msmvps.com/blogs/vipul/default.aspx
RSS link: http://msmvps.com/blogs/vipul/rss.aspx
Atom: http://msmvps.com/blogs/vipul/atom.aspx
One of the lightly used features of VS2003 and VS2005 continue to be Incremental search. Developers usually know the text which they are searching for.
Due to lack of awareness of the VS editor features, I have seen many a developers editing code in TextPad and other editors.
If you know the exact text which you are searching for, you can use the incremental search feature of Visual Studio.
Press Ctrl + I. and start typing the text you are looking for. Your cursor will start to look like binocular facing downloads, and the first text matching the pattern will be selected. As you keep typing the complete text, the selection will jump to the location with contains the complete text. The status bar will contain the text you are looking for.
Hot keys:
Start Incremental Search: Ctrl + I
Made a mistake in typing: Hit Backspace till the wrong text is removed
Found the text you were searching for: Hit Escape
Change the search direction: Ctrl + Shift + I