Showing posts from 2013

Three simple tricks for better C# code

Here are 3 tricks that once you start using them can save you a lot of typing when dealing with C#. 1. The Null Coalescing Operator (??) This is a short-cut for the ternary operator (?:) checking against a null: string name = value; if (value == null) { name = string.Empty; } Can now be condensed into one line: string name = value ?? string.Empty; 2. Auto properties There is no need to define a variable for simple backing fields for most properties in C#. In fact most things like int, strings, double i just let the compiler handle it. public class MyPoint { public int X { get; set; } public int Y { get; set; } } 3. Is vs As You should try and avoid the is operator when you can do an as cast. The is requires two casts where the as does just one. Even with the added null check it's always going to be faster to do an as vs is. var sq = Square as Shape; if ( sq != null ) { volume = sq.Calcul

ASP.NET User Control with Template Content user controls are great.  I use them all the time when working on .net websites.  I was working on a site using the ajax extender toolkit.  I created a panel that could be expanded collapsed with a button click.  I inserted a few into the site and liked them so much I made some more.  Well after the 7th panel it started getting tedious.  What a perfect place for a user control.  The problem was I needed to be able to place whatever custom content I wanted inside the panel.  After much searching around I wasn't finding what I wanted.  The answer it turns out is an user control with custom templates. Create a new user control for your site.  Mine is called CollapsiblePanel.  In the code behind for the page you need to add a new class MessageContainer that inherits from Control and implements the INamingContainer Interface .   Then in the user controls OnInit method you need to check for content and add it to your user control. using System; using

C++ convert char * to to wide string wstring

Often times when working with older code you find yourself staring at some old char* variables. I often find myself having to convert them to wide strings for unicode support (wstring). I created a handy function that makes this simple. wstring charToWideString( const char* src ) { return std::wstring( src, src + strlen(src) ); } Now simply call thus: wstring thus = charToWideString( "thus" ); Drop this function in a static class or header file and use it anywhere.

ASP.NET Create Javascript Alert on Server Side

Many times in you find yourself needing to create an alert on the client side but you're processing on the server side. The trick is to register a Javascript to the client from the server. I have found a nifty way of creating a class that will allow you to do this. public static class ClientMessageBox { public static void Show(string message, Control owner) { Page page = (owner as Page) ?? owner.Page; if (page == null) { return; } page.ClientScript.RegisterStartupScript(owner.GetType(), "ShowMessage", string.Format("<script type="text/javascript">;alert('{0}')</script>", message)); } } // Example of using class and method protected void Page_Load(object sender, EventArgs e) { ClientMessageBox.Show("Hello World", this); } You can now call ClientMessageBox.Show('text', this ); anywhere server side and get a

C# Numeric Only TextBox

Once in a while you find yourself needing to stop certain keypresses in a C# desktop application.  I've found the best way is to handle this in the keypress event of a text box.  This is some very small code that shows how to only allow numeric digits to be pressed. private void textBox_KeyPress( object sender, KeyPressEventArgs e ) {      e.Handled = !char.IsDigit( e.KeyChar ); } Now if you want to allow other characters, like the decimal, etc you need to get a bit fancier. private void textBox_KeyPress( object sender, KeyPressEventArgs e ) {        if( char.IsDigit( e.KeyChar ) ||            e.KeyChar == '.' )        {           e.Handled = false;       }       else       {           e.Handled = true;       } } To allow more characters you just need to modify that first if parameter to your needs.