<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-8232276077748806216</id><updated>2012-01-31T21:01:17.139-08:00</updated><category term='visual studio'/><category term='C#'/><category term='Web Design'/><category term='Keyboard Shortcuts'/><category term='MySQL'/><category term='CSS'/><category term='Chrome'/><category term='MySQL .NET Provider'/><category term='Accessibility'/><category term='macros'/><category term='Opera'/><category term='Annoyances'/><category term='Sybase'/><category term='IE'/><category term='Stored Procedures'/><category term='HowTos'/><category term='Tips and Tricks'/><category term='.NET'/><category term='ASP.NET'/><category term='FireFox'/><title type='text'>A .NET Developer's Blog</title><subtitle type='html'>A blog about .NET, Web Development and computer related tips &amp;amp; tricks.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://waldev.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8232276077748806216/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://waldev.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Wal</name><uri>http://www.blogger.com/profile/04090512767164140583</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://4.bp.blogspot.com/-l3XeciMtu3k/TmhzpLAd6CI/AAAAAAAAAGI/USYyGNuZ0wo/s220/photo_rect.jpg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>17</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-8232276077748806216.post-8458899226498007576</id><published>2011-07-12T04:33:00.000-07:00</published><updated>2011-07-26T02:18:15.314-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ASP.NET'/><title type='text'>Turning URLs into Hyperlinks (with Truncating Long URLs) in ASP.NET</title><content type='html'>Turning URLs into hypyerlinks is fairly easy and straightforward, and actually has been discussed many times before (for example, see &lt;a href="http://www.codeproject.com/KB/aspnet/Autohyperlink.aspx" rel="nofollow"&gt;here&lt;/a&gt; and &lt;a href="http://madskristensen.net/post/Turn-URLs-into-hyperlinks-in-ASPNET.aspx" rel="nofollow"&gt;here&lt;/a&gt;). But some times, you also need to truncate the URL in the link text if it's too long. For example, if you have some text with a very long URL like this:&lt;br /&gt;&lt;br /&gt;Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco http://www.example.com/thisisaveeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeerylongurl.html laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.&lt;br /&gt;&lt;br /&gt;You'll probably want to not only turn the URL into a hyperlink, but also truncate the URL in the hyperlink's text to look something like this:&lt;br /&gt;&lt;br /&gt;Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco &lt;a href="http://www.example.com/thisisaveeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeerylongurl.html"&gt;http://www.example.com/thisisaveeeeeeeeee...&lt;/a&gt; laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Finding and Truncating the URLs&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Finding the URLs is straightforward and is easily done using Regular expressions, but to truncate the long URLs, we'll need to to check whether the URL is too long, and if so, truncate it before using it in the hyperlink's text.&lt;br /&gt;&lt;br /&gt;In this post, I'm going to discuss the details of how to find the URLs. For truncating long URLs, I'm going to use code from my previous post about &lt;a href="http://waldev.blogspot.com/2010/09/truncate-text-string-aspnet-fit-width.html" rel="nofollow"&gt;truncating long text in ASP.NET&lt;/a&gt;. Please refer to that post for the details of how the text is truncated.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;The Code&lt;/b&gt;&lt;br /&gt;&lt;pre class="sourcecodesection nowrap"&gt;public static class UIHelper&lt;br /&gt;{&lt;br /&gt;    private static string ConvertURLs(string text, int textMaxWidth, string fontName, int fontSizeInPixels, bool isFontBold)&lt;br /&gt;    {&lt;br /&gt;        if (string.IsNullOrEmpty(text))&lt;br /&gt;            return text;&lt;br /&gt;&lt;br /&gt;        return Regex.Replace(&lt;br /&gt;            text, &lt;br /&gt;            @"(http(s)*://([\w.]+/?)\S*)",&lt;br /&gt;            LinkTextTruncator.GetLinkTextTruncator(textMaxWidth, fontName, fontSizeInPixels, isFontBold).MatchEvaluator,&lt;br /&gt;            RegexOptions.IgnoreCase | RegexOptions.Compiled);&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;class LinkTextTruncator&lt;br /&gt;{&lt;br /&gt;    public LinkTextTruncator(&lt;br /&gt;        int textMaxWidth, &lt;br /&gt;        string fontName, &lt;br /&gt;        int fontSizeInPixels, &lt;br /&gt;        bool isFontBold)&lt;br /&gt;    {&lt;br /&gt;        _textMaxWidth = textMaxWidth;&lt;br /&gt;        _fontName = fontName;&lt;br /&gt;        _fontSizeInPixels = fontSizeInPixels;&lt;br /&gt;        _isFontBold = isFontBold;&lt;br /&gt;        _matchEvaluator = new MatchEvaluator(TruncateText);&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    private int _textMaxWidth;&lt;br /&gt;    private string _fontName;&lt;br /&gt;    private int _fontSizeInPixels;&lt;br /&gt;    private bool _isFontBold;&lt;br /&gt;    private MatchEvaluator _matchEvaluator;&lt;br /&gt;&lt;br /&gt;    public string TruncateText(Match m)&lt;br /&gt;    {&lt;br /&gt;        string url = m.ToString();&lt;br /&gt;        return string.Format(&lt;br /&gt;            "&lt;a false;\"="" href="http://www.blogger.com/%22%7B0%7D/%22" onclick="\&amp;quot;window.open(this.href);return" onkeypress="\&amp;quot;window.open(this.href);return" rel="\&amp;quot;nofollow\&amp;quot;"&gt;{1}&lt;/a&gt;",&lt;br /&gt;            url,&lt;br /&gt;            TextTruncator.TruncateText( // For the implementation of TextTruncator.TruncateText(), please refer to my post about truncating long text in ASP.NET&lt;br /&gt;                url,&lt;br /&gt;                _textMaxWidth,&lt;br /&gt;                _fontName,&lt;br /&gt;                _fontSizeInPixels,&lt;br /&gt;                _isFontBold));&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public MatchEvaluator MatchEvaluator&lt;br /&gt;    {&lt;br /&gt;        get&lt;br /&gt;        {&lt;br /&gt;            return _matchEvaluator;&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    //&lt;br /&gt;    // Static Members&lt;br /&gt;    //&lt;br /&gt;&lt;br /&gt;    private static Dictionary&lt;string, linktexttruncator=""&gt; _linkTextTruncatorDic;&lt;br /&gt;    private static Dictionary&lt;string, linktexttruncator=""&gt; LinkTextTruncatorDic&lt;br /&gt;    {&lt;br /&gt;        get&lt;br /&gt;        {&lt;br /&gt;            if (_linkTextTruncatorDic == null)&lt;br /&gt;            {&lt;br /&gt;                _linkTextTruncatorDic = new Dictionary&lt;string, linktexttruncator=""&gt;();&lt;br /&gt;            }&lt;br /&gt;            return _linkTextTruncatorDic;&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    private static object _linkTextTruncatorLocker = new object();&lt;br /&gt;    public static LinkTextTruncator GetLinkTextTruncator(int textMaxWidth, string fontName, int fontSizeInPixels, bool isFontBold)&lt;br /&gt;    {&lt;br /&gt;        string specs = textMaxWidth.ToString() + "px_" + fontName.ToLower() + "_" + fontSizeInPixels.ToString() + "px" + (isFontBold ? "_bold" : "");&lt;br /&gt;        if (!LinkTextTruncatorDic.ContainsKey(specs))&lt;br /&gt;        {&lt;br /&gt;            lock (_linkTextTruncatorLocker)&lt;br /&gt;            {&lt;br /&gt;                LinkTextTruncatorDic.Add(specs, new LinkTextTruncator(textMaxWidth, fontName, fontSizeInPixels, isFontBold));&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        return LinkTextTruncatorDic[specs];&lt;br /&gt;    }&lt;br /&gt;}&lt;/string,&gt;&lt;/string,&gt;&lt;/string,&gt;&lt;/pre&gt;&lt;br /&gt;&lt;b&gt;Usage Example&lt;/b&gt;&lt;br /&gt;&lt;pre class="sourcecodesection nowrap"&gt;&amp;lt;%= UIHelper.ConvertURLs("Some text that contains URLs", 200, "Verdana", 12, false) %&amp;gt;&lt;/pre&gt;&lt;br /&gt;&lt;b&gt;Notes About the Code&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;For the implementation of TextTruncator.TruncateText(), please check my previous post&amp;nbsp;about&amp;nbsp;&lt;a href="http://waldev.blogspot.com/2010/09/truncate-text-string-aspnet-fit-width.html" rel="nofollow"&gt;truncating long text in ASP.NET&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;If you check the code in UIHelper.ConvertURLs(), you'll find that I use one of the overloaded versions of Regex.Replace() that takes a MatchEvaluator delegate. Every time a match is found (a URL in this case), LinkTextTruncator.TruncateText() is called, which converts the URL into an anchor tag and calls TextTruncator.TruncateText(), which truncates the URL if it's longer than the specified width, otherwise it returns the same URL untruncated.&lt;br /&gt;&lt;br /&gt;I chose to cache the LinkTextTruncator objects (by font family, size and weight) in order to avoid creating a new object every time URLs are truncated and so slightly alleviate the overhead on the garbage collector. But, it should be easy to remove the caching if you find it unneeded.&lt;br /&gt;&lt;br /&gt;Needless to say, you need to encode the text passed to ConvertURLs() (using HttpUtility.HtmlEncode(), or Html.Encode() if you're using ASP.NET MVC). You should notice that any ampersands in the URL will be converted to &amp;amp; when the text is encoded, but this is not only valid in HTML, it's actually required but often ignored. If for some reason you don't want the ampersands in the URLs to be converted, you can use Replace("&amp;amp;", "&amp;amp;") on the URL in LinkTextTruncator.TruncateText().&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8232276077748806216-8458899226498007576?l=waldev.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://waldev.blogspot.com/feeds/8458899226498007576/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://waldev.blogspot.com/2011/07/url-into-hyperlink-aspnet-truncate-long.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8232276077748806216/posts/default/8458899226498007576'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8232276077748806216/posts/default/8458899226498007576'/><link rel='alternate' type='text/html' href='http://waldev.blogspot.com/2011/07/url-into-hyperlink-aspnet-truncate-long.html' title='Turning URLs into Hyperlinks (with Truncating Long URLs) in ASP.NET'/><author><name>Wal</name><uri>http://www.blogger.com/profile/04090512767164140583</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://4.bp.blogspot.com/-l3XeciMtu3k/TmhzpLAd6CI/AAAAAAAAAGI/USYyGNuZ0wo/s220/photo_rect.jpg'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8232276077748806216.post-6461340737910313405</id><published>2010-09-08T00:39:00.001-07:00</published><updated>2010-10-04T22:13:36.158-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ASP.NET'/><title type='text'>Truncating a text string in ASP.NET to fit within a given pixel width</title><content type='html'>Sometimes when developing a web application, you have strings that must fit within a certain pixel width (usually the width of a container, like a div, p, td .. etc). This is esp. more common with links, as you sometimes need every link to fit within one single line and doesn't wrap to the next line (which will happen if the text of the link is too wide to fit within the width of the container). Or to express it visually, you might have a link that looks like this:&lt;br /&gt;&lt;br /&gt;&lt;a href="javascript:void()"&gt;Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis.&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;And want to make it look like this:&lt;br /&gt;&lt;br /&gt;&lt;a href="javascript:void()"&gt;Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor ...&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;While this should eventually be possible with CSS (using the text-overflow style), it's still not implemented in all browsers. There are some javascript solutions for now that can do the same thing across all browsers, still, I personally prefer a server-side solution as it:&lt;br /&gt;&lt;br /&gt;&lt;ol&gt;&lt;li&gt;Saves bandwidth (which can also be good for the user experience if the text is too long and takes more time to download, esp. if the user has a slow connection).&lt;/li&gt;&lt;li&gt;Is guaranteed to work in all browsers (including the older ones) regardless of whether they have javascript enabled, or even support it.&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;OK, now that you're (hopefully!) convinced, let's see how this can be done in ASP.NET. Obviously, first thing we need to do is measure the text width so that if it's longer than the specified width, we truncate it. For this, we'll need to use TextRenderer.MeasureText(), which is actually intended for Windows Forms Applications, but still can be used with ASP.NET.&lt;br /&gt;&lt;br /&gt;I wrapped the code required for the truncation into a class, TextTruncator, which you can see its code here (also see the download link at the end of the post):&lt;br /&gt;&lt;pre class="sourcecodesection nowrap"&gt;using System;&lt;br /&gt;using System.Collections.Generic;&lt;br /&gt;using System.Linq;&lt;br /&gt;using System.Text;&lt;br /&gt;using System.Windows.Forms;&lt;br /&gt;using System.Drawing;&lt;br /&gt;&lt;br /&gt;namespace aspnet_TextTruncator&lt;br /&gt;{&lt;br /&gt;    public static class TextTruncator&lt;br /&gt;    {&lt;br /&gt;        // Private Properties&lt;br /&gt;        //&lt;br /&gt;&lt;br /&gt;        private static Dictionary&amp;lt;string, int[]&amp;gt; _fontWidthDic;&lt;br /&gt;        private static Dictionary&amp;lt;string, int[]&amp;gt; FontWidthDic&lt;br /&gt;        {&lt;br /&gt;            get&lt;br /&gt;            {&lt;br /&gt;                if (_fontWidthDic == null)&lt;br /&gt;                {&lt;br /&gt;                    _fontWidthDic = new Dictionary&amp;lt;string, int[]&amp;gt;();&lt;br /&gt;                }&lt;br /&gt;                return _fontWidthDic;&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        //&lt;br /&gt;        // Public Methods&lt;br /&gt;        //&lt;br /&gt;&lt;br /&gt;        public static string TruncateText(string text, int textMaxWidth, string fontName, int fontSizeInPixels)&lt;br /&gt;        {&lt;br /&gt;            return TruncateText(text, textMaxWidth, fontName, fontSizeInPixels, false);&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        public static string TruncateText(string text, int textMaxWidth, string fontName, int fontSizeInPixels, bool isFontBold)&lt;br /&gt;        {&lt;br /&gt;            if (string.IsNullOrEmpty(text))&lt;br /&gt;                return text;&lt;br /&gt;&lt;br /&gt;            // Check&lt;br /&gt;            //&lt;br /&gt;            if (textMaxWidth &amp;lt; 1 ||&lt;br /&gt;                string.IsNullOrEmpty(fontName) ||&lt;br /&gt;                fontSizeInPixels &amp;lt; 1)&lt;br /&gt;            {&lt;br /&gt;                throw new ArgumentException();&lt;br /&gt;            }&lt;br /&gt;&lt;br /&gt;            int[] fontWidthArray = GetFontWidthArray(fontName, fontSizeInPixels, isFontBold);&lt;br /&gt;            int ellipsisWidth = fontWidthArray['.'] * 3;&lt;br /&gt;            int totalCharCount = text.Length;&lt;br /&gt;            int textWidth = 0;&lt;br /&gt;            int charIndex = 0;&lt;br /&gt;            for (int i = 0; i &amp;lt; totalCharCount; i++)&lt;br /&gt;            {&lt;br /&gt;                textWidth += fontWidthArray[text[i]];&lt;br /&gt;                if (textWidth &amp;gt; textMaxWidth)&lt;br /&gt;                {&lt;br /&gt;                    return text.Substring(0, charIndex) + "...";&lt;br /&gt;                }&lt;br /&gt;                else if (textWidth + ellipsisWidth &amp;lt;= textMaxWidth)&lt;br /&gt;                {&lt;br /&gt;                    charIndex = i;&lt;br /&gt;                }&lt;br /&gt;            }&lt;br /&gt;            return text;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        //&lt;br /&gt;        // Private Methods&lt;br /&gt;        //&lt;br /&gt;&lt;br /&gt;        private static int[] GetFontWidthArray(string fontName, int fontSizeInPixels, bool isFontBold)&lt;br /&gt;        {&lt;br /&gt;            string fontEntryName = fontName.ToLower() + "_" + fontSizeInPixels.ToString() + "px" + (isFontBold ? "_bold" : "");&lt;br /&gt;            int[] fontWidthArray;&lt;br /&gt;            if (!FontWidthDic.TryGetValue(fontEntryName, out fontWidthArray))&lt;br /&gt;            {&lt;br /&gt;                fontWidthArray = CreateFontWidthArray(new Font(fontName, fontSizeInPixels, isFontBold ? FontStyle.Bold : FontStyle.Regular, GraphicsUnit.Pixel));&lt;br /&gt;                FontWidthDic[fontEntryName] = fontWidthArray;&lt;br /&gt;            }&lt;br /&gt;&lt;br /&gt;            return fontWidthArray;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        private static int[] CreateFontWidthArray(Font font)&lt;br /&gt;        {&lt;br /&gt;            int[] fontWidthArray = new int[256];&lt;br /&gt;            for (int i = 32; i &amp;lt; 256; i++)&lt;br /&gt;            {&lt;br /&gt;                char c = (char)i;&lt;br /&gt;                fontWidthArray[i] = IsIllegalCharacter(c, false) ? 0 : GetCharWidth(c, font);&lt;br /&gt;            }&lt;br /&gt;            return fontWidthArray;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        private static int GetCharWidth(char c, Font font)&lt;br /&gt;        {&lt;br /&gt;            // Note1: For typography related reasons, TextRenderer.MeasureText() doesn't return the correct&lt;br /&gt;            // width of the character in pixels, hence the need to use this hack (with the '&amp;lt;' &amp; '&amp;gt;'&lt;br /&gt;            // characters and the subtraction). Note that &amp;lt;' and '&amp;gt;' were chosen randomly, other characters &lt;br /&gt;            // can be used.&lt;br /&gt;            //&lt;br /&gt;&lt;br /&gt;            // Note2: As the TextRenderer class is intended to be used with Windows Forms Applications, it has a &lt;br /&gt;            // special use for the ampersand character (used for Mnemonics). Therefore, we need to check for the &lt;br /&gt;            // ampersand character and replace it with '&amp;&amp;' to escape it (TextRenderer.MeasureText() will treat &lt;br /&gt;            // it as one ampersand character)&lt;br /&gt;            //&lt;br /&gt;&lt;br /&gt;            return&lt;br /&gt;                TextRenderer.MeasureText("&amp;lt;" + (c == '&amp;' ? "&amp;&amp;" : c.ToString()) + "&amp;gt;", font).Width -&lt;br /&gt;                TextRenderer.MeasureText("&amp;lt;&amp;gt;", font).Width;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        private static bool ContainsIllegalCharacters(string text, bool excludeLineBreaks)&lt;br /&gt;        {&lt;br /&gt;            if (!string.IsNullOrEmpty(text))&lt;br /&gt;            {&lt;br /&gt;                foreach (char c in text)&lt;br /&gt;                {&lt;br /&gt;                    if (IsIllegalCharacter(c, excludeLineBreaks))&lt;br /&gt;                        return true;&lt;br /&gt;                }&lt;br /&gt;            }&lt;br /&gt;&lt;br /&gt;            return false;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        private static bool IsIllegalCharacter(char c, bool excludeLineBreaks)&lt;br /&gt;        {&lt;br /&gt;            // See the Windows-1252 encoding (we use ISO-8859-1, but all browsers, or at least&lt;br /&gt;            // IE, FF, Opera, Chrome and Safari, interpret ISO-8859-1 as Windows-1252).&lt;br /&gt;            // For more information, see http://en.wikipedia.org/wiki/ISO/IEC_8859-1#ISO-8859-1_and_Windows-1252_confusion&lt;br /&gt;            //&lt;br /&gt;&lt;br /&gt;            return&lt;br /&gt;                (c &amp;lt; 32 &amp;&amp; (!excludeLineBreaks || c != '\n')) ||&lt;br /&gt;                c &amp;gt; 255 ||&lt;br /&gt;                c == 127 ||&lt;br /&gt;                c == 129 ||&lt;br /&gt;                c == 141 ||&lt;br /&gt;                c == 143 ||&lt;br /&gt;                c == 144 ||&lt;br /&gt;                c == 157;&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;b&gt;Code Description:&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;The class has only one public method, TruncateText(), which you should call to truncate the text. For performance reasons, I cache the width of the letters so that I don't need to call TextRenderer.MeasureText() again. This was done for good reasons as TextRenderer.MeasureText() can be really slow (in my testing, this change cut the truncation time for a million strings from 8 minutes to 2 seconds!).&lt;br /&gt;&lt;br /&gt;Because I chose to cache the width of the letters, I had to limit the character set to ISO-8859-1 (for Latin based languages) which works well for my own purposes. If you need this code to work with Unicode, you'll need to remove the letter width caching mechanism and call TextRenderer.MeasureText() every time you want to measure the text width (which will slow things down a bit, but shouldn't be noticeable unless you plan to use this on a very high traffic website and on not-fast-enough machines, you'll have to do your own testing to be sure). You could also change the letter width caching mechanism to use a dictionary instead of an array and only add the letters to the dictionary when they are actually used.&lt;br /&gt;&lt;br /&gt;You may notice that in TruncateText(), instead of passing a font object, I pass the font name, size in pixels and whether it's bold. I do this for convenience reasons, but this should be fairly easy to change if you want to pass a Font object (probably, to also use other font styles like italic, but you'll also need to change the caching mechanism of the letter width).&lt;br /&gt;&lt;br /&gt;Note: You'll need to add references to the System.Windows.Forms and System.Drawing assemblies in your project in order for this code to work.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Example:&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;In your .aspx page&lt;br /&gt;&lt;pre class="sourcecodesection nowrap"&gt;&amp;lt;%= TextTruncator.TruncateText("Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut", 300, "Verdana", 12) %&amp;gt;&lt;/pre&gt;Of course in real use, the text will most likely come from your database, an xml file or another source.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;A Note About Web Browsers&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;In many discussions on the web, you'll read that you shouldn't depend on the text width in pixels as there's no guarantee that different browsers will display the text in the same way (i.e. the width of the text may differ from one browser to another). While it's true that there's no guarantee, in my real life testing in all popular browsers (and less popular ones), I found that in all browsers the width of the text as displayed in the browser never exceeded the maximum width I specified for the text in TruncateText(), sometimes it was slightly smaller (by only a few pixels - that was in the older versions of Safari on Windows), but again it was never larger. If you are paranoid, like me, always use a 'safety margin'. For example, if the width of the container of the text (div, p, td or whatever element) is 500px, make the maximum width (that's passed to TruncateText()) less by 10px or 20px, that's 490 or 480.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;a href="http://www.waleedeissa.com/uploads/TextTruncator.zip"&gt;Download code and demo project&lt;/a&gt;&lt;/b&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8232276077748806216-6461340737910313405?l=waldev.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://waldev.blogspot.com/feeds/6461340737910313405/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://waldev.blogspot.com/2010/09/truncate-text-string-aspnet-fit-width.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8232276077748806216/posts/default/6461340737910313405'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8232276077748806216/posts/default/6461340737910313405'/><link rel='alternate' type='text/html' href='http://waldev.blogspot.com/2010/09/truncate-text-string-aspnet-fit-width.html' title='Truncating a text string in ASP.NET to fit within a given pixel width'/><author><name>Wal</name><uri>http://www.blogger.com/profile/04090512767164140583</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://4.bp.blogspot.com/-l3XeciMtu3k/TmhzpLAd6CI/AAAAAAAAAGI/USYyGNuZ0wo/s220/photo_rect.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8232276077748806216.post-1261088850509206396</id><published>2010-08-30T20:16:00.000-07:00</published><updated>2010-09-12T07:13:23.929-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='visual studio'/><category scheme='http://www.blogger.com/atom/ns#' term='macros'/><title type='text'>Using Visual Studio Macros for Code Generation</title><content type='html'>Visual Studio Macros are great for automating repetitive tasks and can be real time savers. One thing I esp. like to use them for is code generation.&lt;br /&gt;&lt;br /&gt;For example, this macro generates the code for C# class properties (I actually prefer this to using automatic properties as I don't have to change the code later):&lt;br /&gt;&lt;br /&gt;&lt;pre class="sourcecodesection nowrap"&gt;Sub CreateProperties()&lt;br /&gt;&lt;br /&gt;    Dim textSelection As EnvDTE.TextSelection&lt;br /&gt;    textSelection = CType(DTE.ActiveDocument.Selection(), EnvDTE.TextSelection)&lt;br /&gt;&lt;br /&gt;    Dim outputText As String&lt;br /&gt;    Dim regExpression As New Regex("\s*(static)*\s*(\S+)\s*([a-zA-Z0-9_]+)", RegexOptions.Compiled)&lt;br /&gt;    For Each line As String In textSelection.Text.Split(New Char() {vbNewLine})&lt;br /&gt;    Dim m As Match = regExpression.Match(line)&lt;br /&gt;        If m.Success Then&lt;br /&gt;            Dim staticModifier As String = m.Groups(1).Value&lt;br /&gt;            Dim propertyDataType As String = m.Groups(2).Value&lt;br /&gt;            Dim propertyName As String = m.Groups(3).Value.Substring(0, 1).ToLower() &amp; m.Groups(3).Value.Remove(0, 1)&lt;br /&gt;            outputText &amp;= String.Format("private {4}{0} _{1};{3}public {4}{0} {2}{3}{{{3}get{3}{{{3}return _{1};{3}}}{3}}}{3}{3}", _&lt;br /&gt;            propertyDataType, _&lt;br /&gt;            propertyName, _&lt;br /&gt;            propertyName.Substring(0, 1).ToUpper() &amp; propertyName.Remove(0, 1), _&lt;br /&gt;            Environment.NewLine, _&lt;br /&gt;            IIf(staticModifier = "", "", staticModifier &amp; " "))&lt;br /&gt;        Else&lt;br /&gt;            If Not Regex.IsMatch(line, "^\s*$") Then&lt;br /&gt;                MsgBox("Input mismatch")&lt;br /&gt;                Exit Sub&lt;br /&gt;            End If&lt;br /&gt;        End If&lt;br /&gt;    Next&lt;br /&gt;&lt;br /&gt;    textSelection.Insert(outputText, 0)&lt;br /&gt;    textSelection.SmartFormat()&lt;br /&gt;&lt;br /&gt;End Sub&lt;/pre&gt;&lt;br /&gt;To use this macro, you type the data type of the property and its name (you can also use the static modifier), like this:&lt;br /&gt;&lt;br /&gt;&lt;pre class="sourcecodesection nowrap"&gt;int count&lt;br /&gt;string name&lt;br /&gt;string description&lt;br /&gt;static int globalCount&lt;/pre&gt;&lt;br /&gt;Then, you select the code and run the macro (most conveniently, using a keyboard shortcut), to get this code:&lt;br /&gt;&lt;br /&gt;&lt;pre class="sourcecodesection nowrap"&gt;private int _count;&lt;br /&gt;public int Count&lt;br /&gt;{&lt;br /&gt;    get&lt;br /&gt;    {&lt;br /&gt;        return _count;&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;private string _name;&lt;br /&gt;public string Name&lt;br /&gt;{&lt;br /&gt;    get&lt;br /&gt;    {&lt;br /&gt;        return _name;&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;private string _description;&lt;br /&gt;public string Description&lt;br /&gt;{&lt;br /&gt;    get&lt;br /&gt;    {&lt;br /&gt;        return _description;&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;private static int _globalCount;&lt;br /&gt;public static int GlobalCount&lt;br /&gt;{&lt;br /&gt;    get&lt;br /&gt;    {&lt;br /&gt;        return _globalCount;&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;This is only a very simple use of VS Macros to demonstrate how they can be used for code generation. They are very powerful and can do much more complex things. If you do a lot of coding, it's certainly worth the time to &lt;a href="http://msdn.microsoft.com/en-us/library/b4c73967(v=VS.100).aspx"&gt;learn more about this very powerful, yet underused, feature of Visual Studio&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;Happy coding!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8232276077748806216-1261088850509206396?l=waldev.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://waldev.blogspot.com/feeds/1261088850509206396/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://waldev.blogspot.com/2010/08/visual-studio-macros.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8232276077748806216/posts/default/1261088850509206396'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8232276077748806216/posts/default/1261088850509206396'/><link rel='alternate' type='text/html' href='http://waldev.blogspot.com/2010/08/visual-studio-macros.html' title='Using Visual Studio Macros for Code Generation'/><author><name>Wal</name><uri>http://www.blogger.com/profile/04090512767164140583</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://4.bp.blogspot.com/-l3XeciMtu3k/TmhzpLAd6CI/AAAAAAAAAGI/USYyGNuZ0wo/s220/photo_rect.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8232276077748806216.post-4710709368743793931</id><published>2009-08-08T07:45:00.001-07:00</published><updated>2010-09-26T19:25:02.572-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='IE'/><category scheme='http://www.blogger.com/atom/ns#' term='Chrome'/><category scheme='http://www.blogger.com/atom/ns#' term='Accessibility'/><category scheme='http://www.blogger.com/atom/ns#' term='FireFox'/><category scheme='http://www.blogger.com/atom/ns#' term='Opera'/><title type='text'>How to change the background color of web pages in your browser</title><content type='html'>&lt;div&gt;If you spend too much time browsing the web on your computer, you may start to notice that you're getting too much darkness around your eyes. It's because of this white background color on most web pages, it's slowly turning you into a raccoon. Luckily, this can be changed.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Most web browsers allow you to control the colors of web pages. Here's how to change the background and text colors in all the popular browsers:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;Internet Explorer&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;ol&gt;&lt;li&gt;On the menu, go to &lt;i&gt;Tools &gt; Internet Options&lt;/i&gt;&lt;/li&gt;&lt;li&gt;On the &lt;i&gt;Internet Options&lt;/i&gt; dialog box, click the &lt;i&gt;Colors&lt;/i&gt; button (at the bottom left corner).&lt;/li&gt;&lt;li&gt;Uncheck the &lt;i&gt;Use Windows colors&lt;/i&gt; check box, then change the text and background colors to the colors you want (I use black for the background and grey for the text), then click &lt;i&gt;OK&lt;/i&gt;.&lt;/li&gt;&lt;li&gt;Next, click the &lt;i&gt;Accessibility&lt;/i&gt; button (this time at the bottom right corner).&lt;/li&gt;&lt;li&gt;Check the &lt;i&gt;Ignore colors specified on Web pages&lt;/i&gt; check box and click &lt;i&gt;OK&lt;/i&gt;.&lt;/li&gt;&lt;li&gt;Click &lt;i&gt;OK&lt;/i&gt; on the &lt;i&gt;Internet Options&lt;/i&gt; dialog box to close it and apply the new settings.&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;FireFox&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;ol&gt;&lt;li&gt;On the menu, go to &lt;i&gt;Tools &gt; Options&lt;/i&gt;&lt;/li&gt;&lt;li&gt;On the &lt;i&gt;Options&lt;/i&gt; dialog box, click the &lt;i&gt;Content&lt;/i&gt; tab (at the top)&lt;/li&gt;&lt;li&gt;Now, click the &lt;i&gt;Colors&lt;/i&gt; button.&lt;/li&gt;&lt;li&gt;Uncheck the &lt;i&gt;Use system colors&lt;/i&gt; check box and select the colors you want for the background and text (black and grey for example)&lt;/li&gt;&lt;li&gt;Uncheck the &lt;i&gt;Allow pages to choose their own colors, instead of my selections above&lt;/i&gt; check box&lt;/li&gt;&lt;li&gt;Click &lt;i&gt;OK&lt;/i&gt; to close the &lt;i&gt;Colors&lt;/i&gt; dialog box.&lt;/li&gt;&lt;li&gt;Click &lt;i&gt;OK&lt;/i&gt; to close the &lt;i&gt;Options&lt;/i&gt; dialog box and apply the new settings&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;Opera&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;It's a little trickier with Opera but fortunately it's still possible.&lt;/div&gt;&lt;div&gt;&lt;ol&gt;&lt;li&gt;Create a new text file (using Notepad or your favorite text editor) and copy and paste the code below into it, save the file anywhere you want but remember where you saved it (you may name the file opera.css, but this is optional, you can give it any other name you like)&lt;pre class="sourcecodesection"&gt;*&lt;br /&gt;{&lt;br /&gt;    background: transparent !important;&lt;br /&gt;    color: #C0C0C0 !important;&lt;br /&gt;    border-color: #C0C0C0 !important;&lt;br /&gt;}&lt;br /&gt;html, body&lt;br /&gt;{&lt;br /&gt;    background: #000 !important;&lt;br /&gt;}&lt;br /&gt;a:link, a:hover, a:active&lt;br /&gt;{&lt;br /&gt;    color: #00E !important;&lt;br /&gt;}&lt;br /&gt;a:visited&lt;br /&gt;{&lt;br /&gt;    color: #551A8B !important;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li&gt;Now in Opera, on the menu, go to &lt;i&gt;Tools &gt; Preferences&lt;/i&gt;&lt;/li&gt;&lt;li&gt;Click the &lt;i&gt;Advanced&lt;/i&gt; tab&lt;/li&gt;&lt;li&gt;On the left side, click &lt;i&gt;Content&lt;/i&gt;&lt;/li&gt;&lt;li&gt;Click the &lt;i&gt;Style Options&lt;/i&gt; button&lt;/li&gt;&lt;li&gt;On the &lt;i&gt;Style Options&lt;/i&gt; dialog box, under &lt;i&gt;My style sheet&lt;/i&gt;, select the file you created in step #1 (you'll replace the value already in there but this is ok)&lt;/li&gt;&lt;li&gt;Now click the &lt;i&gt;Presentation Modes&lt;/i&gt; tab (also on the &lt;i&gt;Style Options&lt;/i&gt; dialog box).&lt;/li&gt;&lt;li&gt;By default, you should only have The &lt;i&gt;Page style sheet&lt;/i&gt; and Page &lt;i&gt;fonts and colors&lt;/i&gt; check boxes checked under both the &lt;i&gt;Author mode&lt;/i&gt; and &lt;i&gt;User mode&lt;/i&gt; (left and right), you should also have &lt;i&gt;Default mode&lt;/i&gt; set to &lt;i&gt;Author mode&lt;/i&gt; (the drop down list at the bottom right). Now, under the &lt;i&gt;Author mode&lt;/i&gt; on the left, check the &lt;i&gt;My style sheet&lt;/i&gt; check box and click &lt;i&gt;OK&lt;/i&gt;.&lt;/li&gt;&lt;li&gt;Click &lt;i&gt;OK&lt;/i&gt; to close the &lt;i&gt;Preferences&lt;/i&gt; dialog box and apply the new settings&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;Google Chrome&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/div&gt;&lt;div&gt;Unfortunately for Google Chrome, you can't control the page background or text colors (this is true as to the latest version at the time of writing this post, the version I have here is 2.0.172.39 which is the latest version). The only solution I found was to use a bookmarklet, this is a short piece of code that you add to your bookmarks, you click it everytime you want to change the colors of the page (actually you can also paste it into your browser's address bar then press Enter to make it work). This is not really practical but it's all what you've got for now, hope google will fix this soon. I created this little bookmarklet for you which will change the background color to black and the text color to light grey (copy the code and paste it into the address bar in Google Chrome then press Enter, you should add it to your bookmarks to make it easier to change the color of the page by only clicking it):&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;pre class="sourcecodesection"&gt;javascript:(function(){var allElements=document.getElementsByTagName("*");for(var i=0;i&amp;lt;allElements.length;i++){var element=allElements[i];element.style.background="#000 !important";element.style.color="#C0C0C0 !important";element.style.borderColor="#C0C0C0 !important";if(element.tagName.toLowerCase()=="a"){element.style.color="#00E !important";}}})()&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;&lt;b&gt;Update&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Google Chrome now supports user stylesheets. This means you don't need to use any bookmarklets. Assuming you use Windows, change the page colors by going to "C:\Documents and Settings\{your windows user}\Local Settings\Application Data\Google\Chrome\User Data\Default\User StyleSheets" (change the drive letter if you have Windows installed on a drive other than C). Now, open the Custom.css file then copy and paste the same code above used with Opera and save the file.&lt;br /&gt;&lt;br /&gt;You may also now use one of many Google Chrome extensions that allow you to control the page colors. For example, see the &lt;a href="https://chrome.google.com/extensions/detail/jbmkekhehjedonbhoikhhkmlapalklgn"&gt;Change Colors&lt;/a&gt; extension.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8232276077748806216-4710709368743793931?l=waldev.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://waldev.blogspot.com/feeds/4710709368743793931/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://waldev.blogspot.com/2009/08/change-page-background-color-browser.html#comment-form' title='7 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8232276077748806216/posts/default/4710709368743793931'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8232276077748806216/posts/default/4710709368743793931'/><link rel='alternate' type='text/html' href='http://waldev.blogspot.com/2009/08/change-page-background-color-browser.html' title='How to change the background color of web pages in your browser'/><author><name>Wal</name><uri>http://www.blogger.com/profile/04090512767164140583</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://4.bp.blogspot.com/-l3XeciMtu3k/TmhzpLAd6CI/AAAAAAAAAGI/USYyGNuZ0wo/s220/photo_rect.jpg'/></author><thr:total>7</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8232276077748806216.post-3221222165770046913</id><published>2009-02-13T04:05:00.000-08:00</published><updated>2011-11-02T03:36:58.233-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='CSS'/><title type='text'>How to make a textbox and a textarea same width across browsers</title><content type='html'>Thanks to the *brilliant* CSS box model, very simple tasks have become a nightmare.&lt;br /&gt;&lt;br /&gt;I've been struggling lately to do a very simple thing, which is to make a textbox (ie. &amp;lt;input type="text"&amp;gt;) and a textarea same width so that they align nicely.&lt;br /&gt;&lt;br /&gt;The problem with making a textbox and textarea same width is that all browsers add 1px padding to textboxes that can't be removed. In other words, setting the padding to 0px won't remove that 1px padding. A common trick to get rid of padding is to float the element but unluckily this doesn't work with textboxes. Well, this means we have extra 2px in the width of the textbox that we simply can't get rid of. Now, this wouldn't be a problem if the same thing applied to textreas as well but unfortunately this is not the case. In most browsers (IE, Safari and Chrome), setting the padding of a textrea to 0px *unfortunately* works. One exception to this is FireFox which applies the 1px non-removable padding to textareas as well. This is why the problem doesn't exist with FireFox. But, should we ignore IE, Safari and Chrome and assume everyone is using FireFox? I guess not, it definitely makes more sense to make it work in those browsers and find a hack for FireFox and actually this is what I did.&lt;br /&gt;&lt;br /&gt;Having had a look on mozilla's website, I came across those two extensions to CSS:&lt;br /&gt;&lt;a href="https://developer.mozilla.org/en/CSS/-moz-padding-start"&gt;-moz-padding-start&lt;/a&gt; and &lt;a href="https://developer.mozilla.org/en/CSS/-moz-padding-end"&gt;-moz-padding-end&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;So, to make a textbox and a textrea same width across browsers you can use this code:&lt;br /&gt;&lt;br /&gt;&lt;pre class="sourcecodesection"&gt;&amp;lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&amp;gt;&lt;br /&gt;&amp;lt;html&amp;gt;&lt;br /&gt;&amp;lt;head&amp;gt;&lt;br /&gt;&amp;lt;style type="text/css"&amp;gt;&lt;br /&gt;*&lt;br /&gt;{&lt;br /&gt;    padding: 0px;&lt;br /&gt;    margin: 0px;&lt;br /&gt;}&lt;br /&gt;#textbox&lt;br /&gt;{&lt;br /&gt;    width: 500px;&lt;br /&gt;    -moz-padding-start: 1px;&lt;br /&gt;    -moz-padding-end: 1px;&lt;br /&gt;}&lt;br /&gt;#textarea&lt;br /&gt;{&lt;br /&gt;    width: 500px;&lt;br /&gt;    padding: 1px;&lt;br /&gt;}&lt;br /&gt;&amp;lt;/style&amp;gt;&lt;br /&gt;&amp;lt;/head&amp;gt;&lt;br /&gt;&amp;lt;body&amp;gt;&lt;br /&gt;&amp;lt;input type="text" id="textbox" /&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;&amp;lt;textarea id="textarea"&amp;gt;&amp;lt;/textarea&amp;gt;&lt;br /&gt;&amp;lt;/body&amp;gt;&lt;br /&gt;&amp;lt;/html&amp;gt;&lt;/pre&gt;&lt;br /&gt;Adding 1px to the textrea makes the text look nicer than with no padding at all (with 0px padding, the text sticks to the border which is not visually appealing). In FireFox, we will have 2px padding for the textarea and 2px left and right padding for the textbox, but, visually speaking, it almost looks the same (try to view the code in FireFox).&lt;br /&gt;&lt;br /&gt;This code was tested in IE, Safari, Chrome, FireFox and SeaMonkey (which uses the same rendering engine as FF) and worked perfectly. Note that this hack is not perfect. For example, Opera will display the textarea 2px wider than the textbox. For me I don't care about Opera very much (not so many people use it), besides it's only 2px, so, it's not something that will make your site unusable. There could probably be a hack for Opera too but again I'm not interested in Opera, so for me it's not worth the time. It probably won't work with other less popular browsers too. I tested it in the ones I care most about (2 extra pixels in a not popular browser is not a big deal. I can live with that).&lt;br /&gt;&lt;br /&gt;In terms of rendering engines (aka layout engines), this will work with Trident based browsers (Internet Explorer), WebKit based browsers (Safari, Google Chrome and other browsers) and Gecko based browsers (FireFox, SeaMonkey and other browsers).&lt;br /&gt;&lt;br /&gt;Please note that this won't work if the visual styles are not enabled in Windows XP or Vista (with the exception of FireFox and the other browsers that are based on the same engine), but as very few people (almost none) disable the visual styles so I'm not so worried about that one too.&lt;br /&gt;&lt;br /&gt;Finally, if you're a control freak and want it to work in every browser then your last resort is javascript (I was working on that but gave up on it after I came up with this simple hack), but even this won't work if javascript is disabled in the browser.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8232276077748806216-3221222165770046913?l=waldev.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://waldev.blogspot.com/feeds/3221222165770046913/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://waldev.blogspot.com/2009/02/how-to-make-textbox-and-textarea-same.html#comment-form' title='13 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8232276077748806216/posts/default/3221222165770046913'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8232276077748806216/posts/default/3221222165770046913'/><link rel='alternate' type='text/html' href='http://waldev.blogspot.com/2009/02/how-to-make-textbox-and-textarea-same.html' title='How to make a textbox and a textarea same width across browsers'/><author><name>Wal</name><uri>http://www.blogger.com/profile/04090512767164140583</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://4.bp.blogspot.com/-l3XeciMtu3k/TmhzpLAd6CI/AAAAAAAAAGI/USYyGNuZ0wo/s220/photo_rect.jpg'/></author><thr:total>13</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8232276077748806216.post-991578498033823284</id><published>2008-12-27T21:25:00.000-08:00</published><updated>2010-09-08T23:50:14.397-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='CSS'/><title type='text'>How to align checkboxes and their labels consistently across browsers</title><content type='html'>I was trying today to get a checkbox and its label to look consistent across browsers and finally came up with this solution. I tested it in IE6, FF2 and Chrome and it rendered same pixel by pixel in all the three browsers (also browsershots.org confirmed the result for other browsers). Hopefully this will save time for someone else.&lt;br /&gt;&lt;br /&gt;&lt;pre class="sourcecodesection"&gt;&amp;lt;style type="text/css"&amp;gt;&lt;br /&gt;*&lt;br /&gt;{&lt;br /&gt;    padding: 0px;&lt;br /&gt;    margin: 0px;&lt;br /&gt;}&lt;br /&gt;&amp;#x23;wb&lt;br /&gt;{&lt;br /&gt;    width: 15px;&lt;br /&gt;    height: 15px;&lt;br /&gt;    float: left;&lt;br /&gt;}&lt;br /&gt;&amp;#x23;somelabel&lt;br /&gt;{&lt;br /&gt;    float: left;&lt;br /&gt;    padding-left: 3px;&lt;br /&gt;}&lt;br /&gt;&amp;lt;/style&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;div&amp;gt;&lt;br /&gt;&amp;lt;input id="wb" type="checkbox" /&amp;gt;&amp;lt;label for="wb" id="somelabel"&amp;gt;Web Browser&amp;lt;/label&amp;gt;&lt;br /&gt;&amp;lt;/div&amp;gt;&lt;/pre&gt;&lt;br /&gt;* If you don't want to set the padding and margin to zero for all elements as I did here, you could only set them for the label and checkbox.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8232276077748806216-991578498033823284?l=waldev.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://waldev.blogspot.com/feeds/991578498033823284/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://waldev.blogspot.com/2008/12/how-to-align-checkboxes-and-their.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8232276077748806216/posts/default/991578498033823284'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8232276077748806216/posts/default/991578498033823284'/><link rel='alternate' type='text/html' href='http://waldev.blogspot.com/2008/12/how-to-align-checkboxes-and-their.html' title='How to align checkboxes and their labels consistently across browsers'/><author><name>Wal</name><uri>http://www.blogger.com/profile/04090512767164140583</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://4.bp.blogspot.com/-l3XeciMtu3k/TmhzpLAd6CI/AAAAAAAAAGI/USYyGNuZ0wo/s220/photo_rect.jpg'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8232276077748806216.post-1099593229294479850</id><published>2008-10-15T23:48:00.001-07:00</published><updated>2009-07-23T07:06:08.286-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ASP.NET'/><title type='text'>A problem when setting the page title dynamically in ASP.NET</title><content type='html'>Today I encountered a weird problem with ASP.NET. What I wanted to do was really simple. I wanted to change the page title dynamically in code, that's all. In my scenario I had a page that inherits from a basepage class. In that base page class I append the site name to the page title, e.g. if the page title is 'page title' (without the quotes) and the site name is 'mysite.com', I add the site name to the page title so that it becomes 'page title - mysite.com'&lt;br /&gt;&lt;br /&gt;In my page, I overrode OnLoad and set the page title in there, but for an unknown reason I always got an empty string for the page title in the basepage class. So, I created a test page, overrode OnLoad() and set the page title in there, surprisingly that worked. I couldn't see any difference between the two pages, I commented all my code and still got a blank title in the basepage class. Finally, I noticed that the only difference between the two pages is that in my page, the title is set to an empty string in the page directive (ie. Title=""). While in the test page, the title is set to the default value (which is "Untitled Page"). By not keeping the title set to an empty string (just put anything in there) it finally worked.&lt;br /&gt;&lt;br /&gt;This seems like a bug, I doubt this behavior is by design. Anyway, to sum up. If you want to set the page title dynamically in your code, make sure you have a "non empty string" for the page title, i.e., if you have Title="" in your page directive, replace it with any other value, e.g. Title="some value" (even though you're going to change this value), otherwise, it will not work.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8232276077748806216-1099593229294479850?l=waldev.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://waldev.blogspot.com/feeds/1099593229294479850/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://waldev.blogspot.com/2008/10/strange-problem-while-trying-to-set.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8232276077748806216/posts/default/1099593229294479850'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8232276077748806216/posts/default/1099593229294479850'/><link rel='alternate' type='text/html' href='http://waldev.blogspot.com/2008/10/strange-problem-while-trying-to-set.html' title='A problem when setting the page title dynamically in ASP.NET'/><author><name>Wal</name><uri>http://www.blogger.com/profile/04090512767164140583</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://4.bp.blogspot.com/-l3XeciMtu3k/TmhzpLAd6CI/AAAAAAAAAGI/USYyGNuZ0wo/s220/photo_rect.jpg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8232276077748806216.post-8015216421738716023</id><published>2008-09-04T22:08:00.000-07:00</published><updated>2010-01-30T06:41:54.312-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Web Design'/><title type='text'>Are Web Designers Getting too Lazy?!!</title><content type='html'>Recently, I changed the color scheme on my computer because I was literally starting to look like a raccoon (with incredibly dark circles under my eyes). Anyway, I changed the background color of all windows to black and the text color to light gray. After I made the changes, I started to surf the net to realize that so many websites don't specify the colors for the page background and/or the text. They leave them to the browser's default colors!! I had strange (and ugly) looking websites on my computer (sadly, so many of them for well-known and popular websites). I got some pages with white background (they specified the background color correctly) but with light gray for the text! which is almost unreadable. Some other websites had black for the text color and also black for the background! which is completely unreadable! (they specified the text color but not the background color), and many websites didn't specify both, so you have black background and light gray text with the images having white background which is really ugly!&lt;br /&gt;&lt;br /&gt;What the heck is going on?! Are web designers getting so lazy that they decided to depend on the browser's default color settings as if they were hard-coded?!! Didn't it come to their minds that anybody can change the color scheme? It's only two lines in CSS for God's sake!!!!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8232276077748806216-8015216421738716023?l=waldev.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://waldev.blogspot.com/feeds/8015216421738716023/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://waldev.blogspot.com/2008/09/are-web-designers-getting-so-lazy.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8232276077748806216/posts/default/8015216421738716023'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8232276077748806216/posts/default/8015216421738716023'/><link rel='alternate' type='text/html' href='http://waldev.blogspot.com/2008/09/are-web-designers-getting-so-lazy.html' title='Are Web Designers Getting too Lazy?!!'/><author><name>Wal</name><uri>http://www.blogger.com/profile/04090512767164140583</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://4.bp.blogspot.com/-l3XeciMtu3k/TmhzpLAd6CI/AAAAAAAAAGI/USYyGNuZ0wo/s220/photo_rect.jpg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8232276077748806216.post-3648291932095121593</id><published>2008-05-27T00:22:00.000-07:00</published><updated>2010-09-05T19:33:47.624-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><title type='text'>The Myth of for being faster than foreach</title><content type='html'>You probably, like me, heard this so many times that &lt;b&gt;for&lt;/b&gt; is faster than &lt;b&gt;foreach&lt;/b&gt; and you should use it whenever you can. Well, today I wanted to know how much this is true so I did a little test (find below the test and results). As you can see in the results of the test they are both identical but as I find using &lt;b&gt;foreach&lt;/b&gt; is much more convenient for many reasons (including thread safety in some scenarios) so it makes more sense to use &lt;b&gt;foreach&lt;/b&gt; whenever possible.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;More details:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The release build was used in the test&lt;br /&gt;Software used in the test:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Windows XP Pro SP2&lt;/li&gt;&lt;li&gt;.NET Framework 2.0&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;The specs of my computer (where the test was done) are:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;CPU Intel 1.6GHz Dual Core&lt;/li&gt;&lt;li&gt;1024 MB RAM&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Used Code&lt;/span&gt;&lt;br /&gt;&lt;pre class="sourcecodesection nowrap"&gt;using System;&lt;br /&gt;using System.Collections.Generic;&lt;br /&gt;using System.Text;&lt;br /&gt;&lt;br /&gt;namespace for_vs_foreach&lt;br /&gt;{&lt;br /&gt;    class Program&lt;br /&gt;    {&lt;br /&gt;        static void Main(string[] args)&lt;br /&gt;        {&lt;br /&gt;            const int LOOP_COUNT = 10000000;&lt;br /&gt;            int myVar;&lt;br /&gt;            MyCustomObject[] customObjects = new MyCustomObject[LOOP_COUNT];&lt;br /&gt;            for (int i = 0; i &lt; LOOP_COUNT; i++)&lt;br /&gt;            {&lt;br /&gt;                customObjects[i] = new MyCustomObject(i);&lt;br /&gt;            }&lt;br /&gt;&lt;br /&gt;            System.Threading.Thread.Sleep(1000);&lt;br /&gt;&lt;br /&gt;            // foreach&lt;br /&gt;            //&lt;br /&gt;            DateTime beforeForeach = DateTime.Now;&lt;br /&gt;            foreach (MyCustomObject customObject in customObjects)&lt;br /&gt;            {&lt;br /&gt;                myVar = customObject.Var;&lt;br /&gt;            }&lt;br /&gt;            DateTime afterForeach = DateTime.Now;&lt;br /&gt;&lt;br /&gt;            // for&lt;br /&gt;            //&lt;br /&gt;            DateTime beforeFor = DateTime.Now;&lt;br /&gt;            for (int i = 0; i &lt; LOOP_COUNT; i++)&lt;br /&gt;            {&lt;br /&gt;                myVar = customObjects[i].Var;&lt;br /&gt;            }&lt;br /&gt;            DateTime afterFor = DateTime.Now;&lt;br /&gt;&lt;br /&gt;            Console.WriteLine("{0} (foreach test results)", afterForeach.Subtract(beforeForeach));&lt;br /&gt;            Console.WriteLine("{0} (for test results)", afterFor.Subtract(beforeFor));&lt;br /&gt;            Console.Read();&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public class MyCustomObject&lt;br /&gt;    {&lt;br /&gt;        public MyCustomObject(int var)&lt;br /&gt;        {&lt;br /&gt;            _var = var;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        private int _var;&lt;br /&gt;        public int Var&lt;br /&gt;        {&lt;br /&gt;            get&lt;br /&gt;            {&lt;br /&gt;                return _var;&lt;br /&gt;            }&lt;br /&gt;            set&lt;br /&gt;            {&lt;br /&gt;                _var = value;&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;span style="font-weight: bold;"&gt;Results:&lt;/span&gt;00:00:00.0625000 (foreach test results)&lt;br /&gt;00:00:00.0625000 (for test results)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8232276077748806216-3648291932095121593?l=waldev.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://waldev.blogspot.com/feeds/3648291932095121593/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://waldev.blogspot.com/2008/05/myth-of-for-being-faster-than-foreach.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8232276077748806216/posts/default/3648291932095121593'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8232276077748806216/posts/default/3648291932095121593'/><link rel='alternate' type='text/html' href='http://waldev.blogspot.com/2008/05/myth-of-for-being-faster-than-foreach.html' title='The Myth of for being faster than foreach'/><author><name>Wal</name><uri>http://www.blogger.com/profile/04090512767164140583</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://4.bp.blogspot.com/-l3XeciMtu3k/TmhzpLAd6CI/AAAAAAAAAGI/USYyGNuZ0wo/s220/photo_rect.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8232276077748806216.post-457978973755736855</id><published>2008-05-10T22:49:00.000-07:00</published><updated>2010-09-05T19:38:54.721-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><title type='text'>Efficiently Copying Items from One Array to Another in C# (Also generally in .NET)</title><content type='html'>I've been searching recently for an efficient way in C# to copy items from an array to another. At first it seemed that efficient way simply didn't exist. In C++ you can do this very easily by copying the memory block using memcpy() or memmove() but in C# you can't do this in a safe way (sure you can write some unsafe code but this is not really what I wanted, I just wanted to do it the 'clean and safe way'). Anyway, my options were to use Array.Copy() or a for loop (iterate through the source array and copy the items to the destination array). Unfortunately the documentation for Array.Copy() clearly states that it's an O(n) operation where n is length, so, it seemed that the for loop and Array.Copy() were almost equivalent so I decided I could use any of them as it wouldn't make any real difference (my decision was based on what's written in the documentation about Array.Copy() being an O(n) operation)&lt;br /&gt;&lt;br /&gt;Anyway, today I was using .NET Refelctor to view the code for List&amp;lt;T&amp;gt;.ToArray(), I found that the Array.Copy() was used in the code, see below the code for List&amp;lt;T&amp;gt;.ToArray() copied from .NET Reflector:&lt;br /&gt;&lt;br /&gt;&lt;pre class="sourcecodesection nowrap"&gt;public T[] ToArray()&lt;br /&gt;{&lt;br /&gt;    T[] destinationArray = new T[this._size];&lt;br /&gt;    Array.Copy(this._items, 0, destinationArray, 0, this._size);&lt;br /&gt;    return destinationArray;&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;So, I wanted to have a look on Array.Copy(), this is how it shows in .NET Refelctor:&lt;br /&gt;&lt;br /&gt;&lt;pre class="sourcecodesection nowrap"&gt;[ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail)]&lt;br /&gt;public static void Copy(Array sourceArray, int sourceIndex, Array destinationArray, int destinationIndex, int length)&lt;br /&gt;{&lt;br /&gt;    Copy(sourceArray, sourceIndex, destinationArray, destinationIndex, length, false);&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;The code for Copy(sourceArray, sourceIndex, destinationArray, destinationIndex, length, false); can't be viewed using .NET Reflector, here's what you get:&lt;br /&gt;&lt;br /&gt;&lt;pre class="sourcecodesection nowrap"&gt;[MethodImpl(MethodImplOptions.InternalCall), ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail)]&lt;br /&gt;internal static extern void Copy(Array sourceArray, int sourceIndex, Array destinationArray, int destinationIndex, int length, bool reliable);&lt;/pre&gt;&lt;br /&gt;To make a long story short I just was curious to see whether using Array.Copy() is really equivalent to using a simple for loop, so, motivated by my inability to view the code for Array.Copy(),I decided to do my own testing (see the code and results below). Well, despite what's written in the documentation about Array.Copy() being an O(n) operation, Array.Copy() was 10 times faster on my computer! So my guess is that it internally uses some memory copying so it's not really an O(n) operation.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Update:&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Just to be clear, when I said the operation is not O(n), I was referring to the implementation not using a for loop to copy the items (ie. from the performance point of view). I realize it's technically incorrect to say it's not O(n). My apology for the confusion.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;More details:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The release build was used in the test&lt;br /&gt;Software used in the test:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Windows XP Pro SP2&lt;/li&gt;&lt;li&gt;.NET Framework 2.0&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;The specs of my computer (where the test was done) are:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;CPU Intel 1.6GHz Dual Core&lt;/li&gt;&lt;li&gt;1024 MB RAM&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Used Code&lt;/span&gt;&lt;br /&gt;&lt;pre class="sourcecodesection nowrap"&gt;using System;&lt;br /&gt;using System.Collections.Generic;&lt;br /&gt;using System.Text;&lt;br /&gt;&lt;br /&gt;namespace array_copy&lt;br /&gt;{&lt;br /&gt;    class Program&lt;br /&gt;    {&lt;br /&gt;        static void Main(string[] args)&lt;br /&gt;        {&lt;br /&gt;            const int ArraySize = 5000;&lt;br /&gt;            const int LoopCount = 100000;&lt;br /&gt;&lt;br /&gt;            MyCustomObject[] customObjects1 = new MyCustomObject[ArraySize];&lt;br /&gt;            MyCustomObject[] customObjects2 = new MyCustomObject[ArraySize];&lt;br /&gt;            for (int i = 0; i &lt; ArraySize; i++)&lt;br /&gt;            {&lt;br /&gt;                customObjects1[i] = new MyCustomObject(i);&lt;br /&gt;                customObjects2[i] = new MyCustomObject(i);&lt;br /&gt;            }&lt;br /&gt;&lt;br /&gt;            // Array.Copy()&lt;br /&gt;            //&lt;br /&gt;            DateTime beforeArrayCopy = DateTime.Now;&lt;br /&gt;            for (int j = 0; j &lt; LoopCount; j++)&lt;br /&gt;            {&lt;br /&gt;                Array.Copy(customObjects1, 0, customObjects2, 1, customObjects1.Length - 1);&lt;br /&gt;            }&lt;br /&gt;            DateTime afterArrayCopy = DateTime.Now;&lt;br /&gt;&lt;br /&gt;            // For Loop Copy&lt;br /&gt;            //&lt;br /&gt;            DateTime beforeForLoopCopy = DateTime.Now;&lt;br /&gt;            for (int j = 0; j &lt; LoopCount; j++)&lt;br /&gt;            {&lt;br /&gt;                int length = customObjects1.Length - 1;&lt;br /&gt;                for (int i = 0; i &lt; length; i++)&lt;br /&gt;                {&lt;br /&gt;                    customObjects2[i + 1] = customObjects1[i];&lt;br /&gt;                }&lt;br /&gt;            }&lt;br /&gt;            DateTime afterForLoopCopy = DateTime.Now;&lt;br /&gt;&lt;br /&gt;            Console.WriteLine("{0} Array.Copy()", afterArrayCopy.Subtract(beforeArrayCopy));&lt;br /&gt;            Console.WriteLine("{0} For Loop Copy", afterForLoopCopy.Subtract(beforeForLoopCopy));&lt;br /&gt;&lt;br /&gt;            Console.Read();&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public class MyCustomObject&lt;br /&gt;    {&lt;br /&gt;        public MyCustomObject(int var)&lt;br /&gt;        {&lt;br /&gt;            _var = var;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        private int _var;&lt;br /&gt;        public int Var&lt;br /&gt;        {&lt;br /&gt;            get&lt;br /&gt;            {&lt;br /&gt;                return _var;&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;&lt;span style="font-weight: bold;"&gt;Results:&lt;/span&gt;00:00:00.5937500 Array.Copy()&lt;br /&gt;00:00:04.9062500 For Loop Copy&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8232276077748806216-457978973755736855?l=waldev.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://waldev.blogspot.com/feeds/457978973755736855/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://waldev.blogspot.com/2008/05/efficiently-copying-items-from-one.html#comment-form' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8232276077748806216/posts/default/457978973755736855'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8232276077748806216/posts/default/457978973755736855'/><link rel='alternate' type='text/html' href='http://waldev.blogspot.com/2008/05/efficiently-copying-items-from-one.html' title='Efficiently Copying Items from One Array to Another in C# (Also generally in .NET)'/><author><name>Wal</name><uri>http://www.blogger.com/profile/04090512767164140583</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://4.bp.blogspot.com/-l3XeciMtu3k/TmhzpLAd6CI/AAAAAAAAAGI/USYyGNuZ0wo/s220/photo_rect.jpg'/></author><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8232276077748806216.post-6503212289794781266</id><published>2008-04-25T03:26:00.000-07:00</published><updated>2010-09-05T20:00:18.226-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Tips and Tricks'/><title type='text'>Redirecting The Ouput of a Command to the Clipboard</title><content type='html'>I very often use the command line to list file names using the dir command (for web design and other purposes). I usually need to have those file names in notepad. One way to do that is to redirect the command ouput to a file (copy the text from the file then delete it) or mark the text in the command line window and press Enter to copy it to the clipboard. A nicer and cleaner way though is to redirect the output of the command to the clipboard. Unfortunately there's no built-in way for doing this in Windows XP, however, it can be added very easily. In Windows 2003 we have a file called clip.exe (under the Windows or system32 directory, not sure, so search for it), which we can use to redirect the output of any command to the clipboard, but first if you're using Windows XP, copy this file to the Windows or System32 directory on your computer (obviously you'll need a computer with Windows 2003 to get this file, you might as well try seraching the internet, it's most probably there).&lt;br /&gt;&lt;br /&gt;Now to redirect the output of your command to the clipboad, just use a pipe with clip, for example:&lt;br /&gt;&lt;br /&gt;&lt;pre class="sourcecodesection"&gt;dir /b | clip&lt;/pre&gt;&lt;br /&gt;* If you're wondering what the /b switch does, it displays the bare file names (only the file names with no headers or any other information)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8232276077748806216-6503212289794781266?l=waldev.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://waldev.blogspot.com/feeds/6503212289794781266/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://waldev.blogspot.com/2008/04/redirecting-ouput-of-command-to.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8232276077748806216/posts/default/6503212289794781266'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8232276077748806216/posts/default/6503212289794781266'/><link rel='alternate' type='text/html' href='http://waldev.blogspot.com/2008/04/redirecting-ouput-of-command-to.html' title='Redirecting The Ouput of a Command to the Clipboard'/><author><name>Wal</name><uri>http://www.blogger.com/profile/04090512767164140583</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://4.bp.blogspot.com/-l3XeciMtu3k/TmhzpLAd6CI/AAAAAAAAAGI/USYyGNuZ0wo/s220/photo_rect.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8232276077748806216.post-5732204303951142405</id><published>2008-04-24T22:33:00.000-07:00</published><updated>2010-09-08T01:22:44.110-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Tips and Tricks'/><category scheme='http://www.blogger.com/atom/ns#' term='Keyboard Shortcuts'/><title type='text'>My Favorite Windows Keyboard Shortcuts</title><content type='html'>I'm pretty much a keyboard freak, I don't really use the mouse unless I'm forced to :) or in other words, unless I want to do something that cannot be done using the keyboard. In case you didn't know that, you can actually move the mouse cursor using the keyboard, this a feature called MouseKeys which I use when I have a problem with the mouse. To activate MouseKeys, press and hold the left Shift and left Alt keys then press the Num Lock key (you may search google for MouseKeys to get more details about that and to know what keys you should use to move the mouse, they are the Numpad keys by the way)&lt;br /&gt;&lt;br /&gt;I use keyboard shortcuts in most applications (IE, FireFox, HTML Help .. etc), but I'm going to list here only the Windows Keyboard shortcuts (by the way, I use Windows XP but most of these keyboard shortcuts are from earlier versions starting from Windows 95, note though that a few of these keyboard shortcuts don't work in Vista but the majority do). So, here's a list of the keyboard shortcuts that I use quite frequently in Windows (or more accurately what I remember now):&lt;br /&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Ctrl + Shift + Esc (to show the task manager)&lt;/li&gt;&lt;li&gt;Alt + Enter (while an icon is selected, e.g. a file, a folder or an icon on the desktop, to open the properties window for that icon)&lt;/li&gt;&lt;li&gt;Windows Key + Pause (to show the System Properties window)&lt;/li&gt;&lt;li&gt;Windows Key + E (to open a Windows explorer window, I follow this by Ctrl + E twice to hide the folders view)&lt;/li&gt;&lt;li&gt;Windows Key + F (to show the serach window)&lt;/li&gt;&lt;li&gt;Ctrl + E or Ctrl + F (while in a folder window to show the search side panel)&lt;/li&gt;&lt;li&gt;Alt + D (while in a folder window to move the focus to the address textbox in the address bar)&lt;/li&gt;&lt;li&gt;Alt + Space (to activate the system menu of an application)&lt;/li&gt;&lt;li&gt;Alt + Space + X (to maximize the current window, press them in the same sequence)&lt;/li&gt;&lt;li&gt;Alt + Space + N (to minimize the current window, press them in the same sequence)&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;MDI Applications Keyboard Shortcuts&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Ctrl + F4 (to close the currently active MDI child window)&lt;/li&gt;&lt;li&gt;Alt + - (to activate the system menu for the currently active MDI child window)&lt;/li&gt;&lt;li&gt;Alt + - + X (to maximize the current MDI child window, press them in the same sequence)&lt;/li&gt;&lt;li&gt;Alt + - + N (to minimize the current MDI child window, press them in the same sequence)&lt;/li&gt;&lt;li&gt;Ctrl + F6 (to move focus between child MDI windows)&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Using the Keyboard with the Mouse&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Double clicking an icon while holding the Alt key (this will open the properties window for this item instead of opening/running the item itself)&lt;/li&gt;&lt;li&gt;Double clicking a folder icon while holding the Ctrl key (this will open the folder in a new window, note that the folder has to be unselected before you double click it)&lt;/li&gt;&lt;/ul&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8232276077748806216-5732204303951142405?l=waldev.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://waldev.blogspot.com/feeds/5732204303951142405/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://waldev.blogspot.com/2008/04/my-favorite-windows-keyboard-shortcuts.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8232276077748806216/posts/default/5732204303951142405'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8232276077748806216/posts/default/5732204303951142405'/><link rel='alternate' type='text/html' href='http://waldev.blogspot.com/2008/04/my-favorite-windows-keyboard-shortcuts.html' title='My Favorite Windows Keyboard Shortcuts'/><author><name>Wal</name><uri>http://www.blogger.com/profile/04090512767164140583</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://4.bp.blogspot.com/-l3XeciMtu3k/TmhzpLAd6CI/AAAAAAAAAGI/USYyGNuZ0wo/s220/photo_rect.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8232276077748806216.post-1141003759874540679</id><published>2008-04-24T21:22:00.000-07:00</published><updated>2010-09-05T20:01:01.477-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><title type='text'>If you're Worried About Performance, Avoid Using Type Initializers (aka static constructors)</title><content type='html'>I'm currently trying to write a highly scalable web application (using C# and .NET Framework 2.0), so, I simply trying to squeeze every drop of performance of the CPU, the DB .. etc. I'm doing a lot of performance testing to everything I use in my application. In my application, I'm using static constructors heavily as they seem to make things easier, well, this is until I realized that they really affect performance! Actually, I discovered this by accident while doing another test. I wanted to test the performance of using nested static classes vs. using singletons, in the process I used a couple of static constructors in the classes (see the code below), the point was to use the static constructor to initiate the variable used for holding the singleton object reference so that we can remove the lazy loading if condition which I expected it would improve performance, well, this is what I thought but the result was surprising to me, the classes that have static constructors take much more time to be accessed than those that don't have static constructors (see the results at the end of this post). My wild guess is that every time a class with a static constructor is accessed the runtime checks whether the static constructor was called or not which causes the performance hit. Without further ado, here's the code below and the results of running the test on my computer .&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;More details:&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;I used the release build in the test (not the debug build as it very often gives very wrong results)&lt;br /&gt;Software used in the test:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Windows XP Pro SP2&lt;/li&gt;&lt;li&gt;.NET Framework 2.0&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;The specs of my computer (where the test was done) are:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;CPU Intel 1.6GHz Dual Core&lt;/li&gt;&lt;li&gt;1024 MB RAM&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Used Code&lt;/span&gt;&lt;br /&gt;&lt;pre class="sourcecodesection nowrap"&gt;using System;&lt;br /&gt;using System.Collections.Generic;&lt;br /&gt;using System.Text;&lt;br /&gt;using System.Threading;&lt;br /&gt;&lt;br /&gt;namespace singleton_vs_nested_classes&lt;br /&gt;{&lt;br /&gt;    class Program&lt;br /&gt;    {&lt;br /&gt;        static void Main(string[] args)&lt;br /&gt;        {&lt;br /&gt;            const int LOOP_COUNT = 1000000000;&lt;br /&gt;            int tempVar = 0;&lt;br /&gt;            Thread.Sleep(3000);&lt;br /&gt;&lt;br /&gt;            // TestStatic&lt;br /&gt;            //&lt;br /&gt;            tempVar = TestStatic.TestVar;&lt;br /&gt;            DateTime beforeTestStatic = DateTime.Now;&lt;br /&gt;            for (int i = 0; i &lt; LOOP_COUNT; i++)&lt;br /&gt;            {&lt;br /&gt;                tempVar = TestStatic.TestVar;&lt;br /&gt;            }&lt;br /&gt;            DateTime afterTestStatic = DateTime.Now;&lt;br /&gt;&lt;br /&gt;            // TestStaticWithTypeInitializer&lt;br /&gt;            //&lt;br /&gt;            tempVar = TestStaticWithTypeInitializer.TestVar;&lt;br /&gt;            DateTime beforeTestStaticWithTypeInitializer = DateTime.Now;&lt;br /&gt;            for (int i = 0; i &lt; LOOP_COUNT; i++)&lt;br /&gt;            {&lt;br /&gt;                tempVar = TestStaticWithTypeInitializer.TestVar;&lt;br /&gt;            }&lt;br /&gt;            DateTime afterTestStaticWithTypeInitializer = DateTime.Now;&lt;br /&gt;&lt;br /&gt;            // TestNested&lt;br /&gt;            //&lt;br /&gt;            tempVar = TestNested.MyNestedClass.TestVar;&lt;br /&gt;            DateTime beforeTestNested = DateTime.Now;&lt;br /&gt;            for (int i = 0; i &lt; LOOP_COUNT; i++)&lt;br /&gt;            {&lt;br /&gt;                tempVar = TestNested.MyNestedClass.TestVar;&lt;br /&gt;            }&lt;br /&gt;            DateTime afterTestNested = DateTime.Now;&lt;br /&gt;&lt;br /&gt;            // TestSingltonLazyLoading&lt;br /&gt;            //&lt;br /&gt;            tempVar = TestSingltonLazyLoading.Singlton.TestVar;&lt;br /&gt;            DateTime beforeTestSingltonLazyLoading = DateTime.Now;&lt;br /&gt;            for (int i = 0; i &lt; LOOP_COUNT; i++)&lt;br /&gt;            {&lt;br /&gt;                tempVar = TestSingltonLazyLoading.Singlton.TestVar;&lt;br /&gt;            }&lt;br /&gt;            DateTime afterTestSingltonLazyLoading = DateTime.Now;&lt;br /&gt;&lt;br /&gt;            // TestSingltonInitInTypeInitliazer&lt;br /&gt;            //&lt;br /&gt;            tempVar = TestSingltonInitInTypeInitliazer.Singlton.TestVar;&lt;br /&gt;            DateTime beforeTestSingltonInitInTypeInitliazer = DateTime.Now;&lt;br /&gt;            for (int i = 0; i &lt; LOOP_COUNT; i++)&lt;br /&gt;            {&lt;br /&gt;                tempVar = TestSingltonInitInTypeInitliazer.Singlton.TestVar;&lt;br /&gt;            }&lt;br /&gt;            DateTime afterTestSingltonInitInTypeInitliazer = DateTime.Now;&lt;br /&gt;&lt;br /&gt;            // Show results&lt;br /&gt;            //&lt;br /&gt;            Console.WriteLine("{0} TestStatic", afterTestStatic.Subtract(beforeTestStatic));&lt;br /&gt;            Console.WriteLine("{0} TestStaticWithTypeInitializer", afterTestStaticWithTypeInitializer.Subtract(beforeTestStaticWithTypeInitializer));&lt;br /&gt;            Console.WriteLine("{0} TestNested", afterTestNested.Subtract(beforeTestNested));&lt;br /&gt;            Console.WriteLine("{0} TestSingltonLazyLoading", afterTestSingltonLazyLoading.Subtract(beforeTestSingltonLazyLoading));&lt;br /&gt;            Console.WriteLine("{0} TestSingltonInitInTypeInitliazer", afterTestSingltonInitInTypeInitliazer.Subtract(beforeTestSingltonInitInTypeInitliazer));&lt;br /&gt;            Console.Read();&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public static class TestStatic&lt;br /&gt;    {&lt;br /&gt;        private static int _testVar = 5;&lt;br /&gt;        public static int TestVar&lt;br /&gt;        {&lt;br /&gt;            get&lt;br /&gt;            {&lt;br /&gt;                return _testVar;&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public static class TestStaticWithTypeInitializer&lt;br /&gt;    {&lt;br /&gt;        static TestStaticWithTypeInitializer()&lt;br /&gt;        {&lt;br /&gt;            _testVar = 5;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        private static int _testVar;&lt;br /&gt;        public static int TestVar&lt;br /&gt;        {&lt;br /&gt;            get&lt;br /&gt;            {&lt;br /&gt;                return _testVar;&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public static class TestNested&lt;br /&gt;    {&lt;br /&gt;        public static class MyNestedClass&lt;br /&gt;        {&lt;br /&gt;            private static int _testVar = 5;&lt;br /&gt;            public static int TestVar&lt;br /&gt;            {&lt;br /&gt;                get&lt;br /&gt;                {&lt;br /&gt;                    return _testVar;&lt;br /&gt;                }&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public static class TestSingltonLazyLoading&lt;br /&gt;    {&lt;br /&gt;        private static TestSinglton _singltonInstance;&lt;br /&gt;        public static TestSinglton Singlton&lt;br /&gt;        {&lt;br /&gt;            get&lt;br /&gt;            {&lt;br /&gt;                if (_singltonInstance == null)&lt;br /&gt;                {&lt;br /&gt;                    _singltonInstance = new TestSinglton(5);&lt;br /&gt;                }&lt;br /&gt;                return _singltonInstance;&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public static class TestSingltonInitInTypeInitliazer&lt;br /&gt;    {&lt;br /&gt;        static TestSingltonInitInTypeInitliazer()&lt;br /&gt;        {&lt;br /&gt;            _singltonInstance = new TestSinglton(5);&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        private static TestSinglton _singltonInstance;&lt;br /&gt;        public static TestSinglton Singlton&lt;br /&gt;        {&lt;br /&gt;            get&lt;br /&gt;            {&lt;br /&gt;                return _singltonInstance;&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public class TestSinglton&lt;br /&gt;    {&lt;br /&gt;        public TestSinglton(int testVar)&lt;br /&gt;        {&lt;br /&gt;            _testVar = testVar;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        private int _testVar;&lt;br /&gt;        public int TestVar&lt;br /&gt;        {&lt;br /&gt;            get&lt;br /&gt;            {&lt;br /&gt;                return _testVar;&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;&lt;span style="font-weight: bold;"&gt;Results:&lt;/span&gt;00:00:01.2656250 TestStatic&lt;br /&gt;00:00:12.8437500 TestStaticWithTypeInitializer&lt;br /&gt;00:00:01.2812500 TestNested&lt;br /&gt;00:00:03.2187500 TestSingltonLazyLoading&lt;br /&gt;00:00:09.3125000 TestSingltonInitInTypeInitliazer&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8232276077748806216-1141003759874540679?l=waldev.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://waldev.blogspot.com/feeds/1141003759874540679/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://waldev.blogspot.com/2008/04/if-youre-worried-about-performance.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8232276077748806216/posts/default/1141003759874540679'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8232276077748806216/posts/default/1141003759874540679'/><link rel='alternate' type='text/html' href='http://waldev.blogspot.com/2008/04/if-youre-worried-about-performance.html' title='If you&apos;re Worried About Performance, Avoid Using Type Initializers (aka static constructors)'/><author><name>Wal</name><uri>http://www.blogger.com/profile/04090512767164140583</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://4.bp.blogspot.com/-l3XeciMtu3k/TmhzpLAd6CI/AAAAAAAAAGI/USYyGNuZ0wo/s220/photo_rect.jpg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8232276077748806216.post-9105274532058505042</id><published>2008-01-08T08:43:00.000-08:00</published><updated>2009-01-02T02:57:06.665-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Tips and Tricks'/><title type='text'>Installing Windows XP in Virtual PC</title><content type='html'>When you create a new virtual machine in Virtual PC (2007 and earlier), you choose the operating system you want to install on that virtual machine so that the virtual machine settings are optimized for that operating system. Now when you choose Windows XP from the list of operating systems, you get a default value of 128 MB for the memory, don't use that default value! If you do, Windows XP will take ages to install on your virtual machine, so, change the default value to be at least 256 MB. This will make a huge difference.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8232276077748806216-9105274532058505042?l=waldev.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://waldev.blogspot.com/feeds/9105274532058505042/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://waldev.blogspot.com/2008/01/installing-windows-xp-in-virtual-pc.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8232276077748806216/posts/default/9105274532058505042'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8232276077748806216/posts/default/9105274532058505042'/><link rel='alternate' type='text/html' href='http://waldev.blogspot.com/2008/01/installing-windows-xp-in-virtual-pc.html' title='Installing Windows XP in Virtual PC'/><author><name>Wal</name><uri>http://www.blogger.com/profile/04090512767164140583</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://4.bp.blogspot.com/-l3XeciMtu3k/TmhzpLAd6CI/AAAAAAAAAGI/USYyGNuZ0wo/s220/photo_rect.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8232276077748806216.post-4593300799028350165</id><published>2008-01-04T07:14:00.000-08:00</published><updated>2009-01-02T02:58:06.503-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='MySQL'/><category scheme='http://www.blogger.com/atom/ns#' term='.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='MySQL .NET Provider'/><category scheme='http://www.blogger.com/atom/ns#' term='Stored Procedures'/><title type='text'>Return value of ExecuteNonQuery() in the MySQL .NET Provider</title><content type='html'>As support for stored procedures was added in MySQL 5, I thought I'd blog about a difference in behavior I noticed between the SQL Server .NET Provider and the MySQL .NET Provider when executing stored procedures using ExecuteNonQuery().&lt;br /&gt;&lt;br /&gt;You know that when you use ExecuteNonQuery() to execute a stored procedure (or any other SQL statements but we're going to talk about stored procedures here) it returns the number of records affected by the stored procedure. Now, what exactly is returned by the SQL Server Provider? I assume most developers who use the SQL Server Provider know that but in case you didn't know, the SQL Server Provider returns the total number of records affected by executing all statements in the stored procedure.&lt;br /&gt;&lt;br /&gt;Let's take this example to make things clearer. For example, say that you have a stored procedure in which you first insert some records in a table then in the same procedure you update some records in another table. Let's assume that the procedure was executed and it inserted 5 records in the first table and updated 3 records in the second table, now what the SQL Server Provider returns exactly is the total number of records affected by executing this procedure which is 5 + 3 = 8.&lt;br /&gt;&lt;br /&gt;On the other hand, the MySQL .NET Provider only returns the number of records affected by executing the last statement in the procedure, so referring to the preceding example, ExecuteNonQuery() will only return 3.&lt;br /&gt;&lt;br /&gt;The point in this post is that if you're using the value returned from ExecuteNonQuery() for anything in your project, you just should be aware of the difference in behavior between the SQL Server Provider and the MySQL .NET Provider.&lt;br /&gt;&lt;br /&gt;Happy programming!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8232276077748806216-4593300799028350165?l=waldev.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://waldev.blogspot.com/feeds/4593300799028350165/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://waldev.blogspot.com/2008/01/return-value-of-executenonquery-in.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8232276077748806216/posts/default/4593300799028350165'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8232276077748806216/posts/default/4593300799028350165'/><link rel='alternate' type='text/html' href='http://waldev.blogspot.com/2008/01/return-value-of-executenonquery-in.html' title='Return value of ExecuteNonQuery() in the MySQL .NET Provider'/><author><name>Wal</name><uri>http://www.blogger.com/profile/04090512767164140583</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://4.bp.blogspot.com/-l3XeciMtu3k/TmhzpLAd6CI/AAAAAAAAAGI/USYyGNuZ0wo/s220/photo_rect.jpg'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8232276077748806216.post-816521301788127711</id><published>2008-01-03T22:17:00.000-08:00</published><updated>2010-09-05T20:00:44.019-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Annoyances'/><category scheme='http://www.blogger.com/atom/ns#' term='Sybase'/><title type='text'>Sybase Annoyances</title><content type='html'>One of the things that I really hated about Sybase is its lack of support for the use of the &lt;b&gt;top&lt;/b&gt; keyword in subquries, for example:&lt;br /&gt;&lt;br /&gt;&lt;pre class="sourcecodesection nowrap"&gt;SELECT&lt;br /&gt;    field1,&lt;br /&gt;    (SELECT TOP 1 field1 FROM table2 WHERE id = table1.id) myfield&lt;br /&gt;FROM&lt;br /&gt;    table1&lt;/pre&gt;&lt;br /&gt;This works in SQL Server but not in Sybase.&lt;br /&gt;&lt;br /&gt;This is as of Sybase ASE 12.5, I don't know if this changed in Sybase ASE 15, so, if you have any idea about it please share this with us in the comments section.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8232276077748806216-816521301788127711?l=waldev.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://waldev.blogspot.com/feeds/816521301788127711/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://waldev.blogspot.com/2008/01/sybase-annoyances.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8232276077748806216/posts/default/816521301788127711'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8232276077748806216/posts/default/816521301788127711'/><link rel='alternate' type='text/html' href='http://waldev.blogspot.com/2008/01/sybase-annoyances.html' title='Sybase Annoyances'/><author><name>Wal</name><uri>http://www.blogger.com/profile/04090512767164140583</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://4.bp.blogspot.com/-l3XeciMtu3k/TmhzpLAd6CI/AAAAAAAAAGI/USYyGNuZ0wo/s220/photo_rect.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8232276077748806216.post-8341482145333425767</id><published>2008-01-03T09:18:00.000-08:00</published><updated>2008-01-05T03:53:52.894-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Keyboard Shortcuts'/><category scheme='http://www.blogger.com/atom/ns#' term='FireFox'/><category scheme='http://www.blogger.com/atom/ns#' term='HowTos'/><title type='text'>How to Open The Current Page in a New Tab in FireFox</title><content type='html'>In FireFox when you click Ctrl + T, a new tab is opened showing a blank page. But what if you want to open a new tab that shows the current page, it's simple but you'll have to do it in two steps:&lt;br /&gt;1. Press Alt + D (notice that the current page address will be highlighted in the address bar)&lt;br /&gt;2. Now, Press Alt + Enter (a new tab will be opened showing the current page)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8232276077748806216-8341482145333425767?l=waldev.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://waldev.blogspot.com/feeds/8341482145333425767/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://waldev.blogspot.com/2008/01/open-current-page-in-new-tab-in-firefox.html#comment-form' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8232276077748806216/posts/default/8341482145333425767'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8232276077748806216/posts/default/8341482145333425767'/><link rel='alternate' type='text/html' href='http://waldev.blogspot.com/2008/01/open-current-page-in-new-tab-in-firefox.html' title='How to Open The Current Page in a New Tab in FireFox'/><author><name>Wal</name><uri>http://www.blogger.com/profile/04090512767164140583</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://4.bp.blogspot.com/-l3XeciMtu3k/TmhzpLAd6CI/AAAAAAAAAGI/USYyGNuZ0wo/s220/photo_rect.jpg'/></author><thr:total>6</thr:total></entry></feed>
