A typedef in C++, such as
typedef short SmallNumber; // C++can be translated in C# as
using SmallNumber = System.Int16; // C#This example is trivial, but it's very handy to be able to create an alias for long type names, especially when you start getting into generics.
[ add comment ] ( 12 views ) | permalink |




( 1 / 1 )I've gotten myself into the habit of adding regions to the code in my C# files because it helps me organize the content better. Up until now, I have been typing the region tags manually, which isn't that much typing, but it is enough to be slightly annoying. Fortunately, there are a few shortcuts to help out.
If you are starting fresh and want to create a region with nothing inside, you can type #region and then press the tab key. This automatically adds the #endregion and allows you to add a region name.
If you want to surround existing code with a region, you can use Visual Studio's built-in region code snippet. Here's one way to do it:
1. Highlight the code you wish to wrap in regions.
2. Press Ctrl-K, Ctrl-X.
3. Select 'Visual C#' from the pop-up menu.
4. Select #region.
That is getting a little bit long, so recording a macro and assigning it a keyboard shortcut might be the way to go.
[ add comment ] ( 5 views ) | permalink |




( 3 / 5 )Just a quick entry to post a link I found the other day of a code example for implementing drag-and-drop functionality in a DataGridView object. I originally used it for the DataGridView and then expanded it to other controls as well. It works pretty slick without much coding. (The example code is written with good English comments, despite the German text at the top of the page. Fear not. :))
[ add comment ] ( 14 views ) | permalink |




( 0 / 0 )A co-worker told me about this typing-saver for creating a string array and defining the elements inside the array. Before I was doing the classic way:
string[] numberArray = new string[] { "one", "two", "three" };However, you don't need to specify the new string array at all! The above line of code can be simplified:
string[] numberArray = { "one", "two", "three" };Not only does the second way save a little extra typing, but it is a lot easier to read.
[ add comment ] ( 4 views ) | permalink |




( 5 / 1 )Recently, I needed to set an image of a PictureBox control in a C# Windows application. The image was stored in the database as binary data. My first thought was to save the image from the database to a temporary location on the disk and then set the image using that temporary path. But, after a little bit of googling, I found a better solution. You can simply set the image from a memory stream:
byte[] imageBytes = GetImageFromDatabase();
MemoryStream ms = new MemoryStream(imageBytes, false);
pictureBox1.Image = Image.FromStream(ms);That's it! Sometimes .Net makes life so easy.
[ add comment ] ( 6 views ) | permalink |




( 0 / 0 )I ran into an interesting HTML encoding issue on an ASP.Net site today.
I was binding some data to an asp:grid control and then doing some post-processing to the data in the OnDataBoundRow event handler. One of the data cells had a "á" character in it, which ended up getting displayed like this:
á Interestingly enough, in the page source, the browser wasn't converting that to a text character because it was converting the ampersand and not the rest of the code for some reason:
áThe fix was to use a handy .Net function in the System.Web.HttpUtility class:
text = HttpUtility.HtmlDecode(text);This replaces all of the html-encoded characters with their actual text characters. After I ran the data through that function, the characters displayed correctly.
[ add comment ] ( 150 views ) | permalink |




( 2.9 / 17 )
Categories



