Posts Tagged ‘tip’
February 29th, 2012
I have found a third use for C# using statement.
The first is for including namespaces, the second is shorthand for making use Dispose is called and the third is for naming types.
using Cache = Dictionary<string, KeyValuePair<string,string>();
Cache cache = new Cache();
As can be seen in the code above I have replaced a long type description with a short.
Honour thouse who should.
May 4th, 2011
Just a tip up front. IE7 runs an older script engine than IE8 and has no full support for Json.
So your code that calls JSON.stringify does not work.
The remedy is to include json2.js from https://github.com/douglascrockford/JSON-js/blob/master/json2.js. It does not interfere with IE8+. Just download the file (the “raw” button – saveas) to your script lib and include as so:
<script src="Script/json2.js"; type="text/javascript"></script><!-- Needed for IE7. -->
Running IE9 in compatibility mode does not make this error appear – it seems like IE9-in-IE7-mode is running a newer script engine.
That impediment avoided you are free to continue adding value to your site.
May 3rd, 2011
This setting has been around a long time but it seems to still elude users.
There is a way to have the document you have open in Visual studio to also be tracked and highlighted in the Solution explorer window.
Menu->Tools->Options->

Checkbox: Track active item in Solution explorer
March 22nd, 2011
Due to some reason unknown to man there is in Windows the idea of every window to have a title bar, unusable for anything but dragging and double clicking and holding approximately four buttons. Chrome and Opera have reused the space for tabs, well done!
Visual studio has its own solution. Press shift-alt-enter (or somewhere on the menu) and the application maximizes itself to get rid of caption bar and borders. Unfortunately the menu is still visible (it should be able to set it to auto hide) . Then remove the tool bars and set the tool windows to auto hide; almost everything is now the editor.
Now shift back to the space wasting layout (shift-alt-enter) and the old setting is back. So not only do we get more working space, we get a fast layout switch too the same way it switches layout when and when not debugging.
Also applicable for Sqlserver management studio.

Viusal studion x 2
November 7th, 2010
Advise: before attempting parallel programming – read up on the subject!
Normally we program by intellisense and the assumption that the name of the method does what we suppose it does. It can be thought of as lazy, that we don’t grok the framework before we start using it. But is does seem to work.
This is not the case with parallel programming. Do it without understanding it and you’re down the slippery slope.
Download the Threading chapter of C#4 in a nutshell by Albahari. It is gratis.
Read up. The text is easy to read and well written.
I have read one other part (looking for interface and inheritance and virtual) of the first release and it was also good. If the rest of the book is as well written I suggest everyone to get a copy.
July 12th, 2010
Avoid
try{
...
}catch( Exception exc ){
...
throw exc;
}
since it destroys the exception stack.
Instead write
try{
...
}catch( Exception ){
...
throw;
}
If you don’t understand what “destroying the exception stack” means you probably don’t want to destroy it and this recommendation is even more something to heed.
To get to the exception use $exception.
June 2nd, 2010
Update: I wrote a longer article here.
List<T> is the work horse of list handling in dotnet. But there are several more to choose from; ArrayList, Hashtable, SortedList, DictionaryEntry, ListDictionary, StringCollection, NameValueCollection, StringDictionary, HybridDictionary andOrderedDictionary for instance. Several of these are replaceable by generic lists so don’t memorise them.
Here is a link to a better explanation: http://www.platinumbay.com/blogs/dotneticated/archive/2007/06/08/hashtables-and-stacks-and-linkedlists-oh-my.aspx
February 4th, 2010
The text and code below is only tested for Winform. WPF might behave differently; hopefully behaves differently.
I won’t go into any details on why the ISite.DesignMode is good and sometimes even crucial to have. It is much better done in the link at the bottom.
First out there is unfortunately a caveat with DesignMode, it doesn’t work with inherited forms and user controls. This is a problem since I always inherit from the base form in my projects and tell others to do the same. It also doesn’t work inside the form’s or the user control’s constructor and then by any method that is called by the constructor. Long call chains to might make this hard to track down.
Secondly there is another way to check for design mode and that is to check for the LicenseManager.UsageMode but this in turn doesn’t work in event handlers.
Thirdly one can set a flag in the base Form and base UserControl. But this doesn’t solve the problem since we cannot be sure with what to set this value.
Fourthly is a workaround where one can check for the name of the process itself which is Visual Studio’s process name (devenv) in case of design mode. This process name might change in future releases of Visual Studio and is also different for other IDEs.
Fifthly is other process information like the name of the module and stuff. Check into the Process and you’ll see that the app is called MyApp.vshost.exe while being debugged.
The fourth solution (processname=devenv) seems to be the most viable but I believe there is something useful in the fifth (other process information). There must be a way to notice that the name of all your code doesn’t match the name of the running environment; namespace, assembly name, whatever. I still haven’t figured out how though.
Below is some code to copy-paste.
It is not 100% correct though since the DesignMode property isn’t virtual. This might end with some really tough-to-track-down bugs where one iterates the forms but don’t get the overloaded DesignMode flag. The code also doesn’t promise to work forever since there is a magic string naming the name of the Visual Studio process.
public partial class MyAppForm : System.Windows.Forms.Form{protected new bool DesignMode{get{return HelperMethods.IsDesignMode(this);}}}
public partial class MyAppUserControl : UserControl{protected new bool DesignMode{get{return HelperMethods.IsDesignMode(this);}}}
class HelperMethods{public static bool IsDesignMode(System.ComponentModel.Component x){var site = x as System.ComponentModel.ISite;return( null == site ? false : site.DesignMode ) ||System.ComponentModel.LicenseManager.UsageMode == System.ComponentModel.LicenseUsageMode.Designtime ||System.Diagnostics.Process.GetCurrentProcess().ProcessName == "devenv";}}
The code above is also in pastebin for easier copy-paste.
A good article is here: http://dotnetfacts.blogspot.com/2009/01/identifying-run-time-and-design-mode.html
There is a description of how to debug this here: http://brennan.offwhite.net/blog/2006/08/30/design-time-debugging-aspnet-20-in-visual-studio-2005/
Error 4 The type or namespace name ‘AccountController’ could not be found (are you missing a using directive or an assembly reference?) C:\myprojectpath\MvcApplication1.Tests\Controllers\AccountControllerTest.cs 317 24 MvcApplication1.Tests
November 19th, 2007
Since many years – many years before I noticed it – has it been possible to dump the text of a message box with ctrl-c.
Example: an application throws an exception and a dialogue with an error message pops up. Instead of dumping the screen and attaching the picture file just copy the text with ctrl-c and past it in you email.