Viktar Karpach Web Developer Blog about ASP.NET,C#,T-SQL,Javascript,CSS,SilverlightViktar Karpach Web Developer Blog about ASP.NET,C#,T-SQL,Javascript,CSS,Silverlight. New technics and tehnlogies reviews.en-usKarpach Web Developer Bloghttp://www.karpach.com/javascript-properties-get-set.htmhttp://www.karpach.com/javascript-properties-get-set.htmJavascriptGet and Set operators in Javascript<p>I am C# developer, hence I use C# properties a lot. However I just recently discovered that Javascript has properties as well. Get and Set operators supported by Firefox 2.0+, Safari 3.0+, Chrome, Opera 9.5+. However IE still doesn't support properties. May be Microsoft would introduce support in IE10, but so far no luck. Here is a little example of Get and Set declaration:</p> <p><style type="text/css"> .cf { color: black; } .cl { margin: 0px; } .cb1 { color: blue; } .cb2 { color: maroon; } </style></p> <div class="cf"> <pre class="cl"><span class="cb1">var</span> obj = {</pre> <pre class="cl"> &nbsp;</pre> <pre class="cl"> &nbsp;&nbsp; get CustomProperty() {</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp;&nbsp; <span class="cb1">return</span> 0;</pre> <pre class="cl"> &nbsp;&nbsp; },</pre> <pre class="cl"> &nbsp;</pre> <pre class="cl"> &nbsp;&nbsp; set CustomProperty(s) {</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp;&nbsp; alert (<span class="cb2">&quot;It is readonly.&quot;</span>);</pre> <pre class="cl"> &nbsp;&nbsp; }</pre> <pre class="cl"> };</pre> </div> <p>&nbsp;</p> <p>Properties can be useful and scary at the same time. A simple variable assignment is not predictable anymore. For example with a little bit of work you can convert your AJAX calls to something like this:</p> <div class="cf"> <pre class="cl"> ajax.url = <span class="cb1">&quot;script.php&quot;</span>;</pre> <pre class="cl"> &nbsp;</pre> <pre class="cl"> alert(ajax.result);</pre> </div> <p>&nbsp;</p>Sat, 04 Feb 2012 00:00:00 -08002012-02-04T00:00:00-08:00Copyright (c) 2007 Viktar Karpachhttp://www.karpach.com/yslow-and-asp-net-three-years-later.htmhttp://www.karpach.com/yslow-and-asp-net-three-years-later.htmASP.NETYSlow and ASP.NET: 100 points "A" grade year 2012<p>Almost three years ago I wrote an original <a href="http://www.karpach.com/yslow-and-asp-net-100-points-a-grade.htm">YSlow and ASP.NET: 100 points "A" grade is possible</a> article. Since then I changed couple hosting providers, made numerous updates and converted my blog project to .NET 4.0. Today I decided to revise my blog to see if it still has 100 yslow points with final "A" grade.</p> <p>First surprise, as of January 29, 2012, YSlow addon doesn't work in FireFox 9. However YSlow is now available for <a href="https://chrome.google.com/webstore/detail/ninejjcohidippngpapiilnmkgllmakh" target="_blank">Google Chrome</a>, <a href="https://addons.opera.com/addons/extensions/details/yslow/" target="_blank">Opera</a>, <a href="http://d.yimg.com/jc/safari/yslow.safariextz" target="_blank">Safari</a>. It has <a href="http://developer.yahoo.com/yslow/mobile/" target="_blank">bookmarket version</a> and even <a href="http://developer.yahoo.com/yslow/commandline" target="_blank">command line version</a>. I installed Google Chrome Yslow extension and began investigation. Right out of the box most of sections still had A grade. About a week ago I switched from Google App Engine "CDN" to true CDN <a href="http://aws.amazon.com/cloudfront/" target="_blank">Amazon CloudFront</a>. Some files were not in CDN, so I had to take care of those. In general I highly recommend Amazon CloudFront. It is easy to setup and cost just a few bucks a month based on your traffic usage. Amazon CloudFront mimics your local file settings, so you need to make sure that you have compression, expire header and Etags taken care of. I use IIS7 compression instead of a compression module recommended by me in my previous article. Out of the box IIS7 compression doesn't work with AWS CloudFront, so you need to modify C:\Windows\System32\inetsrv\config\applicationHost.config file on your server to include following: </p> <style type="text/css"> .cf { color: black; } .cl { margin: 0px; } .cb1 { color: blue; } .cb2 { color: #a31515; } .cb3 { color: red; } </style> <div class="cf"> <pre class="cl"><span class="cb1">&lt;</span><span class="cb2">serverRuntime</span><span class="cb1"> </span><span class="cb3">enabled</span><span class="cb1">=</span>&quot;<span class="cb1">true</span>&quot;<span class="cb1"> </span><span class="cb3">frequentHitThreshold</span><span class="cb1">=</span>&quot;<span class="cb1">1</span>&quot;<span class="cb1"> </span><span class="cb3">frequentHitTimePeriod</span><span class="cb1">=</span>&quot;<span class="cb1">00:00:20</span>&quot;<span class="cb1">/&gt;</span></pre> <pre class="cl"><span class="cb1">&lt;</span><span class="cb2">httpCompression</span><span class="cb1"> </span><span class="cb3">directory</span><span class="cb1">=</span>&quot;<span class="cb1">%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files</span>&quot;<span class="cb1"> </span><span class="cb3">noCompressionForHttp10</span><span class="cb1">=</span>&quot;<span class="cb1">False</span>&quot;<span class="cb1"> </span><span class="cb3">noCompressionForProxies</span><span class="cb1">=</span>&quot;<span class="cb1">False</span>&quot;<span class="cb1">&gt;</span></pre> <pre class="cl"><span class="cb1">&nbsp; &lt;</span><span class="cb2">scheme</span><span class="cb1"> </span><span class="cb3">name</span><span class="cb1">=</span>&quot;<span class="cb1">gzip</span>&quot;<span class="cb1"> </span><span class="cb3">dll</span><span class="cb1">=</span>&quot;<span class="cb1">%Windir%\system32\inetsrv\gzip.dll</span>&quot;<span class="cb1"> /&gt;</span></pre> <pre class="cl"><span class="cb1">&nbsp; &lt;</span><span class="cb2">dynamicTypes</span><span class="cb1">&gt;</span></pre> <pre class="cl"><span class="cb1">&nbsp; &nbsp; &lt;</span><span class="cb2">add</span><span class="cb1"> </span><span class="cb3">mimeType</span><span class="cb1">=</span>&quot;<span class="cb1">text/*</span>&quot;<span class="cb1"> </span><span class="cb3">enabled</span><span class="cb1">=</span>&quot;<span class="cb1">true</span>&quot;<span class="cb1"> /&gt;</span></pre> <pre class="cl"><span class="cb1">&nbsp; &nbsp; &lt;</span><span class="cb2">add</span><span class="cb1"> </span><span class="cb3">mimeType</span><span class="cb1">=</span>&quot;<span class="cb1">message/*</span>&quot;<span class="cb1"> </span><span class="cb3">enabled</span><span class="cb1">=</span>&quot;<span class="cb1">true</span>&quot;<span class="cb1"> /&gt;</span></pre> <pre class="cl"><span class="cb1">&nbsp; &nbsp; &lt;</span><span class="cb2">add</span><span class="cb1"> </span><span class="cb3">mimeType</span><span class="cb1">=</span>&quot;<span class="cb1">application/x-javascript</span>&quot;<span class="cb1"> </span><span class="cb3">enabled</span><span class="cb1">=</span>&quot;<span class="cb1">true</span>&quot;<span class="cb1"> /&gt;</span></pre> <pre class="cl"><span class="cb1">&nbsp; &nbsp; &lt;</span><span class="cb2">add</span><span class="cb1"> </span><span class="cb3">mimeType</span><span class="cb1">=</span>&quot;<span class="cb1">*/*</span>&quot;<span class="cb1"> </span><span class="cb3">enabled</span><span class="cb1">=</span>&quot;<span class="cb1">false</span>&quot;<span class="cb1"> /&gt;</span></pre> <pre class="cl"><span class="cb1">&nbsp; &lt;/</span><span class="cb2">dynamicTypes</span><span class="cb1">&gt;</span></pre> <pre class="cl"><span class="cb1">&nbsp; &lt;</span><span class="cb2">staticTypes</span><span class="cb1">&gt;</span></pre> <pre class="cl"><span class="cb1">&nbsp; &nbsp; &lt;</span><span class="cb2">add</span><span class="cb1"> </span><span class="cb3">mimeType</span><span class="cb1">=</span>&quot;<span class="cb1">text/*</span>&quot;<span class="cb1"> </span><span class="cb3">enabled</span><span class="cb1">=</span>&quot;<span class="cb1">true</span>&quot;<span class="cb1"> /&gt;</span></pre> <pre class="cl"><span class="cb1">&nbsp; &nbsp; &lt;</span><span class="cb2">add</span><span class="cb1"> </span><span class="cb3">mimeType</span><span class="cb1">=</span>&quot;<span class="cb1">message/*</span>&quot;<span class="cb1"> </span><span class="cb3">enabled</span><span class="cb1">=</span>&quot;<span class="cb1">true</span>&quot;<span class="cb1"> /&gt;</span></pre> <pre class="cl"><span class="cb1">&nbsp; &nbsp; &lt;</span><span class="cb2">add</span><span class="cb1"> </span><span class="cb3">mimeType</span><span class="cb1">=</span>&quot;<span class="cb1">application/javascript</span>&quot;<span class="cb1"> </span><span class="cb3">enabled</span><span class="cb1">=</span>&quot;<span class="cb1">true</span>&quot;<span class="cb1"> /&gt;</span></pre> <pre class="cl"><span class="cb1">&nbsp; &nbsp; &lt;</span><span class="cb2">add</span><span class="cb1"> </span><span class="cb3">mimeType</span><span class="cb1">=</span>&quot;<span class="cb1">*/*</span>&quot;<span class="cb1"> </span><span class="cb3">enabled</span><span class="cb1">=</span>&quot;<span class="cb1">false</span>&quot;<span class="cb1"> /&gt;</span></pre> <pre class="cl"><span class="cb1">&nbsp; &lt;/</span><span class="cb2">staticTypes</span><span class="cb1">&gt;</span></pre> <pre class="cl"><span class="cb1">&lt;/</span><span class="cb2">httpCompression</span><span class="cb1">&gt;</span></pre> </div> <p>&nbsp;</p> <p>Also I modified my cache module to have ETags clean up:</p> <style type="text/css"> .cf { color: black; } .cl { margin: 0px; } .cb1 { color: blue; } .cb2 { color: #2b91af; } </style> <div class="cf"> <pre class="cl"><span class="cb1">public</span> <span class="cb1">void</span> Init(<span class="cb2">HttpApplication</span> context)</pre> <pre class="cl">{&nbsp;&nbsp; </pre> <pre class="cl">&nbsp;&nbsp;&nbsp; ...&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; </pre> <pre class="cl">&nbsp;&nbsp;&nbsp; context.PostReleaseRequestState += <span class="cb1">new</span> <span class="cb2">EventHandler</span>(application_PostReleaseRequestState);</pre> <pre class="cl">}</pre> <pre class="cl"> </pre> <pre class="cl"><span class="cb1">void</span> application_PostReleaseRequestState(<span class="cb1">object</span> sender, <span class="cb2">EventArgs</span> e)</pre> <pre class="cl">{</pre> <pre class="cl">&nbsp;&nbsp;&nbsp; <span class="cb2">HttpContext</span>.Current.Response.Headers.Remove(<span class="cb3">&quot;Server&quot;</span>);</pre> <pre class="cl">&nbsp;&nbsp;&nbsp; <span class="cb2">HttpContext</span>.Current.Response.Headers.Remove(<span class="cb3">&quot;X-AspNet-Version&quot;</span>);</pre> <pre class="cl">&nbsp;&nbsp;&nbsp; <span class="cb2">HttpContext</span>.Current.Response.Headers.Remove(<span class="cb3">&quot;ETag&quot;</span>);</pre> <pre class="cl">}</pre> </div> <p>&nbsp;</p> <p>YSlow added new section "Use Cookie-free Domains". My blog completely failed this one, since google analytics left cookies in top level karpach.com domain. I modified google analytics init script to have: _gaq.push(['_setDomainName', 'www.karpach.com']), so google cookies set for www.karpach.com instead of top level karpach.com. Then subdomain cdn.karpach.com, which points to Amazon CloudFront, became cookieless. Finally I am back to 100 YSlow points. Run Yslow on <a href="http://www.karpach.com/default.aspx?PageType=NoExternal">no externals version of karpach.com</a> to see it yourself.</p>Sun, 29 Jan 2012 00:00:00 -08002012-01-29T00:00:00-08:00Copyright (c) 2007 Viktar Karpachhttp://www.karpach.com/undefined-javascript-variable.htmhttp://www.karpach.com/undefined-javascript-variable.htmJavascriptHow to check if a javascript variable is undefined?<p> How to check if a javascript variable is undefined? This is actually a tricky question. There are three cases: </p> <ol> <li>A variable is declared using var keyword, but were never assigned any value.</li> <li>A variable is declared as function parameter, but when function were invoked parameter was not supplied.</li> <li>A variable is never declared or assigned a value and you are trying to access it.</li> </ol> <p> First two cases lead to <b>undefined value</b> of variable. Here is an example of first case: </p> <style type="text/css"> .cf { color: black; } .cl { margin: 0px; } .cb1 { color: blue; } .cb2 { color: #006400; } .cb3 { color: maroon; } </style> <div class="cf"> <pre class="cl"><span class="cb1">var</span> v; <span class="cb2">// Value not defined</span></pre> <pre class="cl"><span class="cb1">if</span> (v) {</pre> <pre class="cl">&nbsp;&nbsp;&nbsp; console.log(<span class="cb3">&quot;v=&quot;</span> + v);</pre> <pre class="cl">}</pre> <pre class="cl"><span class="cb1">else</span> {</pre> <pre class="cl">&nbsp;&nbsp;&nbsp; console.log(<span class="cb3">&quot;v has undefined value&quot;</span>);</pre> <pre class="cl">}</pre> <pre class="cl">v = <span class="cb3">&quot;some value&quot;</span>;</pre> <pre class="cl"><span class="cb1">if</span> (v) {</pre> <pre class="cl">&nbsp;&nbsp;&nbsp; console.log(<span class="cb3">&quot;v = &quot;</span> + v);</pre> <pre class="cl">}</pre> </div> <br/> <p> <br/> Output: <br/> v has undefined value<br/> v = some value<br/> </p> <p> The second case is similar and you can use if statement as in above code or: </p> <style type="text/css"> .cf { color: black; } .cl { margin: 0px; } .cb1 { color: blue; } .cb2 { color: #006400; } </style> <div class="cf"> <pre class="cl"><span class="cb1">function</span> Add(p1, p2) {</pre> <pre class="cl">&nbsp;&nbsp;&nbsp; p1 = p1 || 0; <span class="cb2">// set value to 0 if value is undefined</span></pre> <pre class="cl">&nbsp;&nbsp;&nbsp; p2 = p2 || 0; <span class="cb2">// set value to 0 if value is undefined</span></pre> <pre class="cl">&nbsp;&nbsp;&nbsp; <span class="cb1">return</span> p1 + p2; <span class="cb2">// would always work</span></pre> <pre class="cl">}</pre> <pre class="cl">&nbsp;</pre> <pre class="cl">console.log(Add(1, 2));</pre> <pre class="cl">console.log(Add(1));</pre> <pre class="cl">console.log(Add());</pre> </div> <p> <br/> Output: <br/> 3<br/> 1<br/> 0<br/> </p> <p> The third case when a variable is never declared. If you try to use such variable you get exception. If statement or default value assignment won't help. </p> <style type="text/css"> .cf { color: black; } .cl { margin: 0px; } .cb1 { color: blue; } .cb2 { color: maroon; } </style> <div class="cf"> <pre class="cl"><span class="cb1">if</span>(<span class="cb1">typeof</span> neverDeclared == <span class="cb2">&quot;undefined&quot;</span>) {</pre> <pre class="cl">&nbsp;&nbsp;&nbsp; console.log(<span class="cb2">&quot;There is something really wrong!&quot;</span>);</pre> <pre class="cl">}</pre> </div> <p> <br/> You would ask, what kind of sick people try to use a variable that was never declared or assigned a value? However I bet this is one of the most frequent javascript exceptions. Yes, simple variables are usually declared, but exception usually happens when you try to pass complex object with undeclared property. Javascript objects can be easily extended with additional properties and this comes at price of frequent undefined variable exceptions. Let me give you an example: </p> <style type="text/css"> .cf { color: black; } .cl { margin: 0px; } .cb1 { color: blue; } .cb2 { color: maroon; } .cb3 { color: #006400; } </style> <div class="cf"> <pre class="cl"><span class="cb1">var</span> obj = { FirstName: <span class="cb2">&quot;Viktar&quot;</span>, LastName: <span class="cb2">&quot;Karpach&quot;</span> };</pre> <pre class="cl">console.log(obj.FirstName);</pre> <pre class="cl">console.log(obj.LastName);</pre> <pre class="cl">console.log(obj.City); <span class="cb3">// Will give exception</span></pre> <pre class="cl">&nbsp;</pre> <pre class="cl"><span class="cb1">var</span> obj = { FirstName: <span class="cb2">&quot;Viktar&quot;</span>, LastName: <span class="cb2">&quot;Karpach&quot;</span> };</pre> <pre class="cl">obj.City = <span class="cb2">&quot;Chicago&quot;</span>;</pre> <pre class="cl">console.log(obj.City); <span class="cb3">// Works!</span></pre> <pre class="cl">&nbsp;</pre> <pre class="cl"><span class="cb1">if</span> (<span class="cb1">typeof</span> obj.State == <span class="cb2">&quot;undefined&quot;</span>) {</pre> <pre class="cl">&nbsp;&nbsp;&nbsp; console.log(<span class="cb2">&quot;State is undefined.&quot;</span>);</pre> <pre class="cl">}</pre> </div> <br/> <p>Undefined variable exception very often happens when you try to consume objects received from third party API or from AJAX requests, always be careful what you consume from partially unreliable resource.</p>Thu, 12 Jan 2012 00:00:00 -08002012-01-12T00:00:00-08:00Copyright (c) 2007 Viktar Karpachhttp://www.karpach.com/dvdfab-volume-label-autohotkey.htmhttp://www.karpach.com/dvdfab-volume-label-autohotkey.htmC# .NETAutoHotkey DVDFab script for volume label conversion<p>From time to time I use free <a target="_blank" href="http://www.dvdfab.com/hd-decrypter.htm">DVDFab HD Decrypter</a> to rip rented movies, so I can watch them later using my Popcorn Hour A210 media streamer. DVDFab is kind enough to pull volume label from DVD disk. However this label has all uppercase letters and words are concatenated by underscores. For example:</p> <p>THE_LONG_GOODBYE</p> <p><img alt="DVDFab vs AutoHotkey" src="http://www.karpach.com/images/uploaded/DVDFab-vs-AutoHotkey.jpg" /></p> <p>&nbsp;</p> <p>I rename those labels to something like this: The Long Goodbye (1973), where year I lookup on IMDB web site. I don't like tedious repetitive tasks, so one day I decided to write a script for <a href="http://www.autohotkey.com/">AutoHotkey</a> to automate this process.</p> <p>It is easy to replace underscores and correct letter case, but year should be retrieved from some kind of database. Google pointed me in a direction of <a target="_blank" href="http://www.imdbapi.com">IMDB API</a>.</p> <p>It might be a little bit challenging to do IMDB API access using just AutoHotkey script engine. Luckily AutoHotkey scripts can load dll libraries and with magic of <a target="_blank" href="http://www.autohotkey.com/forum/topic22923.html">COM.ahk</a> and <a target="_blank" href="http://www.autohotkey.com/forum/topic26191.html">CLR.ahk</a> you can load even .NET libraries. However those magic scripts can't properly execute .NET 3.5/4.0 libraries, so I had to use .NET 2.0. As you probably know .NET 2.0 doesn't have native support for JSON deserialization, so I had to use Newtonsoft.Json for this purpose. I also could use XML output from IMDB API, but I prefer to use JSON, since response is smaller in a size. Here is my code for .NET part of it:</p> <p><style type="text/css"> .cf { color: black; } .cl { margin: 0px; } .cb1 { color: blue; } .cb2 { color: #2b91af; } .cb3 { color: #a31515; } .cb4 { color: #3cb371; } .cb5 { color: green; } </style></p> <div class="cf"> <pre class="cl"><span class="cb1">public</span> <span class="cb1">class</span> <span class="cb2">Server</span></pre> <pre class="cl"> {</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">string</span> GetMovieTitleWithYear(<span class="cb1">string</span> volumeLablel)</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; {</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">var</span> request = <span class="cb2">WebRequest</span>.Create(<span class="cb1">new</span> <span class="cb2">Uri</span>(<span class="cb1">string</span>.Format(<span class="cb3">&quot;http://www.imdbapi.com/?t=</span><span class="cb4">{0}</span><span class="cb3">&quot;</span>, </pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; volumeLablel.Replace(<span class="cb3">'_'</span>, <span class="cb3">'+'</span>))));</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; request.Method = <span class="cb3">&quot;GET&quot;</span>;</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">try</span></pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</pre> <pre class="cl"> &nbsp;</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">var</span> response = request.GetResponse();&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; </pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">var</span> responseStream = response.GetResponseStream();</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">if</span> (responseStream != <span class="cb1">null</span>)</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">var</span> reader = <span class="cb1">new</span> <span class="cb2">StreamReader</span>(responseStream);</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">var</span> responseString = reader.ReadToEnd();</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">var</span> movie = <span class="cb2">JsonConvert</span>.DeserializeObject&lt;<span class="cb2">Movie</span>&gt;(responseString);</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">if</span> (movie != <span class="cb1">null</span> &amp;&amp; !<span class="cb1">string</span>.IsNullOrEmpty(movie.Title))</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">return</span> <span class="cb1">string</span>.Format(<span class="cb3">&quot;</span><span class="cb4">{0}</span><span class="cb3"> (</span><span class="cb4">{1}</span><span class="cb3">)&quot;</span>, movie.Title, movie.Year);</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre> <pre class="cl"> &nbsp;</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">catch</span></pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; { </pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; </pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">return</span> UppercaseWords(volumeLablel.Replace(<span class="cb3">'_'</span>, <span class="cb3">' '</span>).ToLower());</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; </pre> <pre class="cl"> &nbsp;</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">static</span> <span class="cb1">string</span> UppercaseWords(<span class="cb1">string</span> value)</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; {</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">char</span>[] array = value.ToCharArray();</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb5">// Handle the first letter in the string.</span></pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">if</span> (array.Length &gt;= 1)</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">if</span> (<span class="cb1">char</span>.IsLower(array[0]))</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; array[0] = <span class="cb1">char</span>.ToUpper(array[0]);</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb5">// Scan through the letters, checking for spaces.</span></pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb5">// ... Uppercase the lowercase letters following spaces.</span></pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">for</span> (<span class="cb1">int</span> i = 1; i &lt; array.Length; i++)</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">if</span> (array[i - 1] == <span class="cb3">' '</span>)</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">if</span> (<span class="cb1">char</span>.IsLower(array[i]))</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; array[i] = <span class="cb1">char</span>.ToUpper(array[i]);</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">return</span> <span class="cb1">new</span> <span class="cb1">string</span>(array);</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; }</pre> <pre class="cl"> }</pre> </div> <p>&nbsp;</p> <p>Then using <a target="_blank" href="http://json2csharp.com/">JSON 2 C# Service</a> you get following:</p> <p><style type="text/css"> .cf { color: black; } .cl { margin: 0px; } .cb1 { color: blue; } .cb2 { color: #2b91af; } </style></p> <div class="cf"> <pre class="cl"><span class="cb1">public</span> <span class="cb1">class</span> <span class="cb2">Movie</span></pre> <pre class="cl"> { </pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">string</span> Title { <span class="cb1">get</span>; <span class="cb1">set</span>; }&nbsp;&nbsp;&nbsp;&nbsp; </pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">int</span> Year { <span class="cb1">get</span>; <span class="cb1">set</span>; }</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">string</span> Rated { <span class="cb1">get</span>; <span class="cb1">set</span>; }</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">string</span> Released { <span class="cb1">get</span>; <span class="cb1">set</span>; }</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">string</span> Genre { <span class="cb1">get</span>; <span class="cb1">set</span>; }</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">string</span> Director { <span class="cb1">get</span>; <span class="cb1">set</span>; }</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">string</span> Writer { <span class="cb1">get</span>; <span class="cb1">set</span>; }</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">string</span> Actors { <span class="cb1">get</span>; <span class="cb1">set</span>; }</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">string</span> Plot { <span class="cb1">get</span>; <span class="cb1">set</span>; }</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">string</span> Poster { <span class="cb1">get</span>; <span class="cb1">set</span>; }</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">string</span> Runtime { <span class="cb1">get</span>; <span class="cb1">set</span>; }</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">float</span> Rating { <span class="cb1">get</span>; <span class="cb1">set</span>; }</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">int</span> Votes { <span class="cb1">get</span>; <span class="cb1">set</span>; }</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">string</span> ID { <span class="cb1">get</span>; <span class="cb1">set</span>; }</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">string</span> Response { <span class="cb1">get</span>; <span class="cb1">set</span>; }&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; </pre> <pre class="cl"> }</pre> </div> <p>&nbsp;</p> <p>Library is ready. Lets plug it into AutoHotkey:</p> <div class="cf"> <pre class="cl"> #include c:\Program Files (x86)\Autohotkey\Extras\Scripts\CLR.ahk #include c:\Program Files (x86)\Autohotkey\Extras\Scripts\COM.ahk RemoveToolTip: SetTimer, RemoveToolTip, Off ToolTip return #IfWinActive ahk_class QWidget ~Lbutton:: MouseGetPos, xpos,ypos if (xpos&gt;306 and ypos&gt;474 and xpos&lt;605 and ypos&lt;495) { ToolTip, Double click in text box below`nto get IMDB title and year,305,430 SetTimer, RemoveToolTip, 5000 } If (A_PriorHotKey = A_ThisHotKey and A_TimeSincePriorHotkey &lt; 500) { CLR_Start() imdb := CLR_LoadLibrary(&quot;c:\Program Files (x86)\Autohotkey\Extras\Scripts\Karpach.IMDB.dll&quot;) ;Type names must be fully qualified. server:= CLR_CreateObject(imdb,&quot;Karpach.IMDB.Server&quot;) Send ^a Sleep 100 clipboard = Send ^c ClipWait title:=COM_Invoke(server,&quot;GetMovieTitleWithYear&quot;,clipboard) COM_Release(imdb) COM_Release(server) clipboard = %title% Send ^v return } return #IfWinActive </pre> <p>&nbsp;</p> </div> <p>Now whenever you double click in volume label text box content would be converted into proper movie title.</p> <p><a href="http://www.karpach.com/files/DVDFab-Autohotkey-Scripts.zip">AutoHotkey scripts and DLLs</a></p> <p><a href="http://www.karpach.com/files/Karpach.IMDB.zip">IMDB API .NET library source code</a></p>Sun, 25 Dec 2011 00:00:00 -08002011-12-25T00:00:00-08:00Copyright (c) 2007 Viktar Karpachhttp://www.karpach.com/interface-based-factory-pattern.htmhttp://www.karpach.com/interface-based-factory-pattern.htmC# .NETSimple factory pattern based on interfaces<p>The main purpose of factory design patterns is to separate object creation process from current implementation context. During coding you use factory to create objects. Based on incoming parameters factory decides which object to create. You don't know exact type of the object. Usually you know either base abstract class type that created object was inherited from or interface that created object is implementing. Based on this knowledge you can use object (call abstract class methods or interface methods). I found a lot of factories examples with abstract class connection, but none with interface connection. However interface connection is most common in commercial factory design pattern implementations. Many developers use Unity Framework or Castle Project (example of connection based on interface). Below I'll show how to implement factory with interface connection like it is done in Unity Framework or Castle Project.</p> <p>Lets build animal fabric, which would create dogs and cats objects. Let's define ICat and IDog interfaces.</p> <p><style type="text/css"> .cf { color: black; } .cl { margin: 0px; } .cb1 { color: blue; } .cb2 { color: #2b91af; } </style></p> <div class="cf"> <pre class="cl"><span class="cb1">public</span> <span class="cb1">interface</span> <span class="cb2">ICat</span></pre> <pre class="cl"> {&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; </pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">string</span> Meow();</pre> <pre class="cl"> }</pre> <br /> <pre class="cl"><span class="cb1">public</span> <span class="cb1">interface</span> <span class="cb2">IDog</span></pre> <pre class="cl"> {</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">string</span> Bark();</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">string</span> Sit();</pre> <pre class="cl"> }</pre> </div> <p>Now, lets define some Dog and Cat classes that implement ICat and IDog interfaces respectevly.</p> <p><style type="text/css"> .cf { color: black; } .cl { margin: 0px; } .cb1 { color: blue; } .cb2 { color: #2b91af; } .cb3 { color: #a31515; } </style></p> <div class="cf"> <pre class="cl"><span class="cb1">public</span> <span class="cb1">class</span> <span class="cb2">Cat</span>:<span class="cb2">ICat</span></pre> <pre class="cl"> {&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; </pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">string</span> Meow()</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; {</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">return</span> <span class="cb3">&quot;Meow meow meow ...&quot;</span>;</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp;&nbsp; </pre> <pre class="cl"> }</pre> <br /> <pre class="cl"><span class="cb1">public</span> <span class="cb1">class</span> <span class="cb2">Dog</span>: <span class="cb2">IDog</span></pre> <pre class="cl"> {&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; </pre> <pre class="cl"> &nbsp;</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">string</span> Bark()</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; {</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">return</span> <span class="cb3">&quot;Woof woof woof ...&quot;</span>;</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; }</pre> <pre class="cl"> &nbsp;</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">string</span> Sit()</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; {</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">return</span> <span class="cb3">&quot;I am sitting.&quot;</span>;</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; }</pre> <pre class="cl"> &nbsp;</pre> <pre class="cl"> }</pre> </div> <p>Now lets create factory:</p> <p><style type="text/css"> .cf { color: black; } .cl { margin: 0px; } .cb1 { color: blue; } .cb2 { color: #2b91af; } .cb3 { color: #a31515; } </style></p> <div class="cf"> <pre class="cl"><span class="cb1">public</span> <span class="cb1">class</span> <span class="cb2">DefaultFactory</span></pre> <pre class="cl"> {</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">static</span> T Create&lt;T&gt;()</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; {</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">if</span> (<span class="cb1">typeof</span>(T) == <span class="cb1">typeof</span>(<span class="cb2">IDog</span>))</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">return</span> (T)(<span class="cb2">IDog</span>)<span class="cb1">new</span> <span class="cb2">Dog</span>();</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">else</span></pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">if</span> (<span class="cb1">typeof</span>(T) == <span class="cb1">typeof</span>(<span class="cb2">ICat</span>))</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">return</span> (T)(<span class="cb2">ICat</span>)<span class="cb1">new</span> <span class="cb2">Cat</span>();</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">else</span></pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">throw</span> <span class="cb1">new</span> <span class="cb2">NotImplementedException</span>(<span class="cb2">String</span>.Format(<span class="cb3">&quot;Creation of {0} interface is not supported yet.&quot;</span>, <span class="cb1">typeof</span>(T)));</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; }</pre> <pre class="cl"> }</pre> </div> <p>And here how you can use it:</p> <p><style type="text/css"> .cf { color: black; } .cl { margin: 0px; } .cb1 { color: #2b91af; } </style></p> <div class="cf"> <pre class="cl"><span class="cb1">IDog</span> dog = <span class="cb1">DefaultFactory</span>.Create&lt;<span class="cb1">IDog</span>&gt;();</pre> <pre class="cl"><span class="cb1">ICat</span> cat = <span class="cb1">DefaultFactory</span>.Create&lt;<span class="cb1">ICat</span>&gt;();</pre> <pre class="cl"><span class="cb1">Console</span>.WriteLine(dog.Bark());</pre> <pre class="cl"><span class="cb1">Console</span>.WriteLine(dog.Sit());</pre> <pre class="cl"><span class="cb1">Console</span>.WriteLine(cat.Meow());</pre> </div> <p><br /> Output:<br /> <br /> Woof woof woof ...<br /> I am sitting.<br /> Meow meow meow ...</p> <p><a target="_blank" href="http://www.karpach.com/files/Karpach.Samples.AnimalFabric.zip">Source code for sample project above.</a></p>Mon, 05 Sep 2011 00:00:00 -07002011-09-05T00:00:00-07:00Copyright (c) 2007 Viktar Karpachhttp://www.karpach.com/video-links-media-player-chrome-extension.htmhttp://www.karpach.com/video-links-media-player-chrome-extension.htmJavascriptVideo Links in a New Media Player Window<p><img align="left" alt="" src="http://www.karpach.com/images/uploaded/Youtube-Player.png" />Google Chrome extension popups Youtube, IMDB, Vimeo and Twitvid links in new window with just a video player. Video player window can be resized to scale video. Works with online chats that post links dynamically, for example CampFire from 37 Signals.</p> <p>Each video gets its own window, so you can open as many videos as you like. Cntrl-Click to open link regular way.</p> <p>Please leave comments and suggestions.</p> <p><a href="https://chrome.google.com/webstore/detail/njgnenfcakbjnampnknmmolokpcfmbfh"> Install Google Chrome Extension</a></p> <p>After you install extension try to click on sample links below:</p> <p><a href="http://www.imdb.com/video/imdb/vi1639488793/">IMDB</a></p> <p><a href="http://www.youtube.com/watch?v=DDU0bnhfmME&NR=1">Youtube</a></p> <p><a href="http://vimeo.com/11219730">Vimeo</a></p> <p><a href="http://www.twitvid.com/Z6OLO">twitvid</a></p> <p><a href="http://youtu.be/DUQi_R4SgWo">youtu.be</a></p>Thu, 12 May 2011 00:00:00 -07002011-05-12T00:00:00-07:00Copyright (c) 2007 Viktar Karpachhttp://www.karpach.com/how-to-find-column-usage-in-stored-procedure.htmhttp://www.karpach.com/how-to-find-column-usage-in-stored-procedure.htmT-SQLHow to find column usage in stored procedure?<p>Sometimes you want to rename or delete column from a table in MS SQL Server. How to find column name dependency usage in MS SQL Server? There is no clean way of doing this. In management studio you can right click on object and select &quot;View Dependency&quot;, but this would show only object dependency. So you need to write some T-SQL code in order to find dependency. Lets say we need to find dependecy of Title column from the Article table.</p> <p><style type="text/css"> .cf { color: black;} .cl { margin: 0px; } .cb1 { color: blue; } .cb2 { color: #a31515; } </style></p> <div class="cf"> <p class="cl"><span class="cb1">SELECT DISTINCT </span>so.name</p> <p class="cl"><span class="cb1">FROM </span>sys.syscomments sc</p> <p class="cl"><span class="cb1">INNER JOIN </span>sys.objects so <span class="cb1">on </span>sc.id=so.object_id</p> <p class="cl"><span class="cb1">WHERE </span>sc.text <span class="cb1">LIKE </span><span class="cb2">'%Title%'</span></p> </div>Sun, 08 May 2011 00:00:00 -07002011-05-08T00:00:00-07:00Copyright (c) 2007 Viktar Karpachhttp://www.karpach.com/sort-array-lamda-expressionshttp://www.karpach.com/sort-array-lamda-expressionsC# .NETHow to sort lists using lambda expressions?<p>Usually I get needed information from SQL server and then do further processing on server side of my web application. Lets say I have a grid of users. Client can sort it by first name or last name, ascending or descending. It is too much typing to create four comparers for Sort method. Luckily you can use lambda expressions. Below is full example:</p> <p><style type="text/css"> .cf { color: black;} .cl { margin: 0px; } .cb1 { color: blue; } .cb2 { color: #2b91af; } .cb3 { color: #a31515; } </style></p> <div class="cf"> <p class="cl"><span class="cb1">class</span> <span class="cb2">Program</span></p> <p class="cl">{</p> <p class="cl">&nbsp;&nbsp;&nbsp; <span class="cb1">static</span> <span class="cb1">void</span> Main(<span class="cb1">string</span>[] args)</p> <p class="cl">&nbsp;&nbsp;&nbsp; {</p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; <span class="cb2">List</span>&lt;<span class="cb2">User</span>&gt; users = <span class="cb1">new</span> <span class="cb2">List</span>&lt;<span class="cb2">User</span>&gt;(<span class="cb1">new</span> [] {</p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">new</span> <span class="cb2">User</span> {FirstName = <span class="cb3">&quot;Viktar&quot;</span>,LastName = <span class="cb3">&quot;Karpach&quot;</span>},</p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">new</span> <span class="cb2">User</span> {FirstName = <span class="cb3">&quot;Vasya&quot;</span>,LastName = <span class="cb3">&quot;Pupkin&quot;</span>},</p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">new</span> <span class="cb2">User</span> {FirstName = <span class="cb3">&quot;Joe&quot;</span>,LastName = <span class="cb3">&quot;Doe&quot;</span>},</p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">new</span> <span class="cb2">User</span> {FirstName = <span class="cb3">&quot;Jane&quot;</span>,LastName = <span class="cb3">&quot;Doe&quot;</span>}</p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; });</p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb2">Console</span>.WriteLine(<span class="cb3">&quot;Assceding order sorted by first name&quot;</span>);</p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; users.Sort((u1, u2) =&gt; <span class="cb1">string</span>.Compare(u1.FirstName, u2.FirstName, <span class="cb1">false</span>));</p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; PrintList(users);</p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb2">Console</span>.WriteLine(<span class="cb3">&quot;Descending order sorted by first name&quot;</span>);</p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; users.Sort((u1, u2) =&gt; <span class="cb1">string</span>.Compare(u2.FirstName, u1.FirstName, <span class="cb1">false</span>));</p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; PrintList(users);</p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb2">Console</span>.WriteLine(<span class="cb3">&quot;Assceding order sorted by last name&quot;</span>);</p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; users.Sort((u1, u2) =&gt; <span class="cb1">string</span>.Compare(u1.LastName, u2.LastName, <span class="cb1">false</span>));</p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; PrintList(users);</p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb2">Console</span>.WriteLine(<span class="cb3">&quot;Descending order sorted by last name&quot;</span>);</p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; users.Sort((u1, u2) =&gt; <span class="cb1">string</span>.Compare(u2.LastName, u1.LastName, <span class="cb1">false</span>));</p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; PrintList(users);</p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb2">Console</span>.ReadLine();</p> <p class="cl">&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;</p> <p class="cl">&nbsp;</p> <p class="cl">&nbsp;&nbsp;&nbsp; <span class="cb1">static</span> <span class="cb1">void</span> PrintList(<span class="cb2">List</span>&lt;<span class="cb2">User</span>&gt; users)</p> <p class="cl">&nbsp;&nbsp;&nbsp; {</p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">foreach</span> (<span class="cb1">var</span> user <span class="cb1">in</span> users)</p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb2">Console</span>.WriteLine(<span class="cb3">&quot;{0} {1}&quot;</span>,user.FirstName,user.LastName);</p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p> <p class="cl">&nbsp;&nbsp;&nbsp; }</p> <p class="cl">}</p> <p class="cl">&nbsp;</p> <p class="cl"><span class="cb1">public</span> <span class="cb1">class</span> <span class="cb2">User</span></p> <p class="cl">{</p> <p class="cl">&nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">string</span> FirstName { <span class="cb1">get</span>; <span class="cb1">set</span>; }</p> <p class="cl">&nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">string</span> LastName { <span class="cb1">get</span>; <span class="cb1">set</span>; }</p> <p class="cl">}</p> </div> <br/> <p>Results:</p> <p>Assceding order sorted by first name</p> <ol> <li>Jane Doe</li> <li>Joe Doe</li> <li>Vasya Pupkin</li> <li>Viktar Karpach</li> </ol> <p>Descending order sorted by first name</p> <ol> <li>Viktar Karpach</li> <li>Vasya Pupkin</li> <li>Joe Doe</li> <li>Jane Doe</li> </ol> <p>Assceding order sorted by last name</p> <ol> <li>Joe Doe</li> <li>Jane Doe</li> <li>Viktar Karpach</li> <li>Vasya Pupkin</li> </ol> <p>Descending order sorted by last name</p> <ol> <li>Vasya Pupkin</li> <li>Viktar Karpach</li> <li>Joe Doe</li> <li>Jane Doe</li> </ol>Wed, 26 Jan 2011 00:00:00 -08002011-01-26T00:00:00-08:00Copyright (c) 2007 Viktar Karpachhttp://www.karpach.com/firefox-firebug-console-vsdoc.htmhttp://www.karpach.com/firefox-firebug-console-vsdoc.htmJavascriptHow to enable intellisence for FireFox console command?<p>Almost every web developer knows about FireBug console window, but I still see some developers who uses javascript alert command for tracing javascript output. You can actually write to FireBug console window using console command from your javascript. However since this is not a standard javascript command, Visual Studio doesn't have itellisence for it. So, <a target="_blank" href="http://www.karpach.com/files/console-vsdoc.js">here is vsdoc file</a>, which enables intellisence for console command.</p> <p>P.S.<br /> Thanks to <a target="_blank" href="http://stackoverflow.com/questions/2744950/is-there-a-firebug-console-vsdoc-js">David Murdoch</a> for creating it.</p>Sat, 25 Dec 2010 00:00:00 -08002010-12-25T00:00:00-08:00Copyright (c) 2007 Viktar Karpachhttp://www.karpach.com/null-coalescing-operator-for-javascript.htmhttp://www.karpach.com/null-coalescing-operator-for-javascript.htmJavascriptNull coalescing operator for JavaScript<p>Very often in javascript some function parameters are optional. So, code like this become needed:</p> <p><style type="text/css"> .cf { color: black;} .cl { margin: 0px; } .cb1 { color: blue; } </style></p> <div class="cf"> <p class="cl">&nbsp;</p> <p class="cl"><span class="cb1">if</span> (<span class="cb1">typeof</span> something == <span class="cb2">&quot;undefined&quot;</span>)</p> <p class="cl">&nbsp;&nbsp;&nbsp; alert(<span class="cb2">&quot;something is undefined&quot;</span>);</p> </div> <p>&nbsp;</p> <p>However, there is a better, slicker solution. This is a null coalescing operator in javascript &quot;terms&quot;:</p> <div class="cf"> <p class="cl">&nbsp;</p> <p class="cl"><span class="cb1">function</span> DoSomething(element)</p> <p class="cl">{</p> <p class="cl">&nbsp;&nbsp;&nbsp; element = element || document.body;</p> <p class="cl">}</p> <p class="cl">&nbsp;</p> <p class="cl"><span class="cb1">function</span> DoSomething(index)</p> <p class="cl">{</p> <p class="cl">&nbsp;&nbsp;&nbsp; index = index || 0;</p> <p class="cl">}</p> <p class="cl">&nbsp;</p> <p class="cl">&nbsp;</p> </div> <p>In example above element variable gets value of document.body if null (undefined). Similarly index variable gets 0 value if undefined or null.</p>Sun, 12 Dec 2010 00:00:00 -08002010-12-12T00:00:00-08:00Copyright (c) 2007 Viktar Karpachhttp://www.karpach.com/html5-web-forms-attributes.htmhttp://www.karpach.com/html5-web-forms-attributes.htmJavascriptHow you can use HTML5 Web Forms 2.0?<p>Webforms 2.0 (HTML5) introduces many useful features. Things that usually done using javascript now are build in a browser.</p> <p>1. Textbox watermark. Supported by FireFox 3.7+, Safari 4.0+, Chrome 4.0+, iPhone 4.0+</p> <p><style type="text/css"> .cf { color: black;} .cl { margin: 0px; } .cb1 { color: blue; } .cb2 { color: #a31515; } .cb3 { color: red; } </style></p> <div class="cf"> <p class="cl"><span class="cb1">&lt;</span><span class="cb2">label</span> <span class="cb3">for</span><span class="cb1">=&quot;txtUserName&quot;&gt;</span>User Name:<span class="cb1">&lt;/</span><span class="cb2">label</span><span class="cb1">&gt;</span></p> <p class="cl"><span class="cb1">&lt;</span><span class="cb2">input</span> <span class="cb3">id</span><span class="cb1">=&quot;txtUserName&quot;</span> <span class="cb3">name</span><span class="cb1">=&quot;txtUserName&quot;</span> <span class="cb3">type</span><span class="cb1">=&quot;text&quot;</span> <span class="cb3">placeholder</span><span class="cb1">=&quot;Your Name&quot;/&gt;</span>&nbsp;</p> </div> <p><br /> <img alt="Placeholder Example" src="http://www.karpach.com/images/uploaded/user-name.jpg" /></p> <p>2. Default form element autofocus. See Google search as an example. Supported by Safari 4.0+,Chrome 3.0+, Opera 10.0+</p> <p><style type="text/css"> .cf { color: black;} .cl { margin: 0px; } .cb1 { color: blue; } .cb2 { color: #a31515; } .cb3 { color: red; } </style></p> <div class="cf"> <p class="cl"><span class="cb1">&lt;</span><span class="cb2">label</span> <span class="cb3">for</span><span class="cb1">=&quot;txtSearch&quot;&gt;</span>Search:<span class="cb1">&lt;/</span><span class="cb2">label</span><span class="cb1">&gt;</span></p> <p class="cl"><span class="cb1">&lt;</span><span class="cb2">input</span> <span class="cb3">id</span><span class="cb1">=&quot;txtSearch&quot;</span> <span class="cb3">name</span><span class="cb1">=&quot;txtSearch&quot;</span> <span class="cb3">type</span><span class="cb1">=&quot;text&quot;</span> <span class="cb3">autofocus</span><span class="cb1">/&gt;</span></p> </div> <p><img alt="Search autofocus" src="http://www.karpach.com/images/uploaded/search-autofocus.jpg" /></p> <p>3. Form / input autocomplete is partially implemented by all current browsers. <style type="text/css"> .cf { color: black;} .cl { margin: 0px; } .cb1 { color: blue; } .cb2 { color: #a31515; } .cb3 { color: red; } </style></p> <div class="cf"> <p class="cl"><span class="cb1">&lt;</span><span class="cb2">form</span> <span class="cb3">autocomplete</span><span class="cb1">=&quot;off&quot;&gt;</span></p> <p class="cl"><span class="cb1">&lt;</span><span class="cb2">input</span> <span class="cb3">type</span><span class="cb1">=&quot;text&quot;</span> <span class="cb3">id</span><span class="cb1">=&quot;txtSearch&quot;</span> <span class="cb3">autocomplete</span><span class="cb1">=&quot;off&quot;/&gt;</span></p> </div> <p>If you ever tried to implement custom autocomplete, just like google has, then you know, first of all you need to turn off browser's autocomplete.</p> <p>So, some browsers support new attributes, some doesn't. Is there an easy way to find out? Here is a little snippet that can help you to find if certain attribute is supported.</p> <p><style type="text/css"> .cf { color: black;} .cl { margin: 0px; } .cb1 { color: blue; } .cb2 { color: #a31515; } .cb3 { color: red; } .cb4 { color: green; } </style></p> <div class="cf"> <p class="cl"><span class="cb1">&lt;</span><span class="cb2">script</span> <span class="cb3">type</span><span class="cb1">=&quot;text/javascript&quot;</span> <span class="cb3">language</span><span class="cb1">=&quot;javascript&quot;&gt;</span></p> <p class="cl">&nbsp;&nbsp;&nbsp; String.prototype.hasProperty = <span class="cb1">function</span>(property)</p> <p class="cl">&nbsp;&nbsp;&nbsp; {</p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">return</span> property <span class="cb1">in</span> document.createElement(<span class="cb1">this</span>);</p> <p class="cl">&nbsp;&nbsp;&nbsp; };</p> <p class="cl">&nbsp;&nbsp;&nbsp; <span class="cb1">if</span> (!<span class="cb2">'input'</span>.hasProperty(<span class="cb2">'autofocus'</span>))</p> <p class="cl">&nbsp;&nbsp;&nbsp; {</p> <p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb4">// Custom implementation for autofocus</span></p> <p class="cl">&nbsp;&nbsp;&nbsp; }</p> <p class="cl"><span class="cb1">&lt;/</span><span class="cb2">script</span><span class="cb1">&gt;</span></p> </div>Sat, 02 Oct 2010 00:00:00 -07002010-10-02T00:00:00-07:00Copyright (c) 2007 Viktar Karpachhttp://www.karpach.com/regenerate-t4-template-command-promt.htmhttp://www.karpach.com/regenerate-t4-template-command-promt.htmASP.NETHow to regenerate T4 templates from command prompt?<p>I am using T4 templates for web.config templating for different environments (development, production). You can regenerate templates from Visual Studio, but what if you want to regenerate all of them from console. It is very useful if you had some mass change in all your web.tt (in different projects) and now need to regenerate corresponding web.config-s.</p> <p>Here is a little batch script that can help you to do this:</p> <pre style="font-size:12px"> @echo off SETLOCAL ENABLEDELAYEDEXPANSION :: set the working dir (default to current dir) set wdir=%cd% if not (%1)==() set wdir=%1 echo executing transform_all from %wdir% :: create a list of all the T4 templates in the working dir dir %wdir%\web*.tt /b /s &gt; t4list.txt echo the following T4 templates will be transformed: type t4list.txt :: transform all the templates for /f %%d in (t4list.txt) do ( set configfile=%%d set configfile=!configfile:~0,-3!.config echo: \--^&gt; !configfile! &quot;%CommonProgramFiles%\Microsoft Shared\TextTemplating\1.2\TextTransform.exe&quot; -out !configfile! %%d ) echo transformation complete </pre>Fri, 06 Aug 2010 00:00:00 -07002010-08-06T00:00:00-07:00Copyright (c) 2007 Viktar Karpachhttp://www.karpach.com/control-iphone-remotely.htmhttp://www.karpach.com/control-iphone-remotely.htmIPhoneHow to control your iPhone remotely?<p>Remote control of your iPhone might be very useful for testing iPhone friendly web sites. You can use emulators for this purpose. <a target="_blank" href="http://labs.blackbaud.com/NetCommunity/article?artid=662">Blackboud iPhone Browser Simulator</a> is not bad, but still this is not a real iPhone browser.</p> <p>First of all you iPhone needs to be jailbroken. Then you need to install VNC Server for iPhone. Just search for Veency in Cydia, it is free.</p> <p>On you desktop you need VNC Viewer. There are bunch of them, but I use <a target="_blank" href="http://www.uvnc.com/">UltraVNC</a> since it is free. Then you can connect to your iPhone over Wi-Fi, but you can do it thru USB too.</p> <p>For USB connection you need to install <a target="_blank" href="http://www.python.org/download/releases/2.6.4/">python interpreter</a> and <a target="_blank" href="http://marcansoft.com/blog/iphonelinux/usbmuxd/"> usbmuxd</a>. From usbmuxd we need only <a href="http://www.karpach.com/files/usbmuxd.zip">python client part</a>. Run connect.bat, which has following line inside:</p> <p>c:\python26\python.exe tcprelay.py 5900</p> <p>And then you can connect to your iPhone using 127.0.0.1 address.</p> <p><img alt="iPhone remote view" src="http://www.karpach.com/images/uploaded/iPhone-remote.png" /></p>Fri, 09 Jul 2010 00:00:00 -07002010-07-09T00:00:00-07:00Copyright (c) 2007 Viktar Karpachhttp://www.karpach.com/viewstateexception-invalid-viewstate.htmhttp://www.karpach.com/viewstateexception-invalid-viewstate.htmASP.NETHow to fix ViewStateException Invalid ViewState?<p>Usually you don't see Invalid ViewState exception unless you implement some kind of <a href="http://www.karpach.com/ASP-NET-Global-asax-error-logger.htm">ASP.NET error logger</a> in Global.asax.</p> <p>Error usually states something like this:</p> <p><b>Exception:</b> System.Web.HttpException: The state information is invalid for this page and might be corrupted. ---&gt; System.Web.UI.ViewStateException: Invalid viewstate.</p> <p>Those errors are usually generated by some automatic submission form engines. Comments and contact forms are mostly effected by this exception. End user never sees those errors, so invalid viewstate errors don't have any value for your error logger. Let's bypass them:</p> <p><style type="text/css"> .cf { color: black; } .cl { margin: 0px; } .cb1 { color: blue; } .cb2 { color: #2b91af; } </style></p> <div class="cf"> <pre class="cl"><span class="cb1">if</span> (ex <span class="cb1">is</span> <span class="cb2">HttpException</span> &amp;&amp; ex.InnerException <span class="cb1">is</span> <span class="cb2">ViewStateException</span>)</pre> <pre class="cl"> {</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; _app.Response.Redirect(_app.Request.Url.AbsoluteUri)&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; </pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">return</span></pre> <pre class="cl"> } <br type="_moz" /></pre> <pre class="cl"> See my <a href="http://www.karpach.com/ASP-NET-Global-asax-error-logger.htm">error logger article</a> for further details.</pre> </div>Wed, 23 Jun 2010 00:00:00 -07002010-06-23T00:00:00-07:00Copyright (c) 2007 Viktar Karpachhttp://www.karpach.com/xrefresh-alternative.htmhttp://www.karpach.com/xrefresh-alternative.htmASP.NETXRefresh alternative solution<p>I am using two monitors for a web development for the past 4 years. Usually I have website that I am working on a left monitor and my Visual Studio on a right monitor. Usually I do change in Visual Studio then press Shift-F6 (in case of code change) or just Ctrl-S (to save). After that I grab a mouse, move cursor to browser on left monitor click and then F5 for refresh. If you do a little CSS tricks it is to much repetitive movements.</p> <p><img alt="Dual Monitor Web Development" src="http://www.karpach.com/images/uploaded/dual-monitor-web-development.png" /></p> <p>About two years ago I discovered <a target="_blank" href="http://xrefresh.binaryage.com/">XRefresh</a>. I mentioned it in my <a href="http://www.karpach.com/asp-net-coding-performance-tips.htm">ASP.NET development tips</a> article. First I was really excited. It is excellent tool for html and CSS markup tweaking. But if you do a lot of code changes and need to recompile your project more often then markup tweaking then it might not be a best tool for you. XRefresh watches files. If file changes then it refreshes browser. Now imagine if you compile big project and compilation takes 15 sec. XRefresh would notice dll change and would try to refresh browser. However compilation is not done yet and some dll-s are locked. So, XRefresh most likely not going to work for Shift-F6 scenario (recompilation of project). This was quite annoying as you can imagine, so I thought there must be some way to come up with a solution. We may have read all the hardware and <a href="http://www.o2.co.uk/broadband/">O2 uk broadband reviews</a> possible, but if you're screen is not refreshing properly that can be very inconvenient. I am not one to sit around when presented with an inconvenient situation however.</p> <p>So, I found XRefresh alternative solution. I wrote a <b>Visual Studio macros</b>. You can hook up to OnBuildDone event. Then find Firefox process. Move focus to Firefox window and send F5 keystroke to it. Macros below still needs some polishing, but you can get an idea:</p> <p><style type="text/css"> .cf { color: black;} .cl { margin: 0px; } .cln { color: #2b91af; } .cb1 { color: blue; } .cb2 { color: #a31515; } </style></p> <div class="cf"> <pre class="cl"><span class="cb1">Private</span> <span class="cb1">Sub</span> BuildEvents_OnBuildDone(<span class="cb1">ByVal</span> Scope <span class="cb1">As</span> EnvDTE.vsBuildScope, _ <span class="cb1">ByVal</span> Action <span class="cb1">As</span> EnvDTE.vsBuildAction) <span class="cb1">Handles</span> BuildEvents.OnBuildDone</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">Dim</span> projects <span class="cb1">As</span> <span class="cb1">Object</span>() = DTE.ActiveSolutionProjects</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">If</span> projects.Length &gt; 0 <span class="cb1">Then</span></pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">If</span> <span class="cb1">CType</span>(projects(0), Project).ExtenderNames.Length <span class="cb1">Then</span></pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">If</span> <span class="cb1">CType</span>(projects(0), Project).ExtenderNames(0) = <span class="cb2">&quot;WebApplication&quot;</span> <span class="cb1">Then</span></pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">Dim</span> proc <span class="cb1">As</span> System.Diagnostics.Process() = _ System.Diagnostics.Process.GetProcessesByName(<span class="cb2">&quot;firefox&quot;</span>)</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">For</span> i <span class="cb1">As</span> <span class="cb1">Integer</span> = 0 <span class="cb1">To</span> proc.Length - 1</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">If</span> proc(i).MainWindowHandle &lt;&gt; 0 <span class="cb1">Then</span></pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; SetForegroundWindow(<span class="cb1">New</span> HandleRef(<span class="cb1">Nothing</span>, proc(i).MainWindowHandle))</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; System.Windows.Forms.SendKeys.SendWait(<span class="cb2">&quot;{F5}&quot;</span>)</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">End</span> <span class="cb1">If</span></pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">Next</span></pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">End</span> <span class="cb1">If</span></pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">End</span> <span class="cb1">If</span></pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">End</span> <span class="cb1">If</span></pre> <pre class="cl"><span class="cb1">End</span> <span class="cb1">Sub</span></pre> </div> <p>&nbsp;</p> <p>I am open for discussions and any suggestions.</p> <p><a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fwww.karpach.com%2fxrefresh-alternative.htm"><img border="0" alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fwww.karpach.com%2fxrefresh-alternative.htm&amp;border=688299&amp;fgcolor=ffffff&amp;bgcolor=8C3014" /></a></p>Mon, 31 May 2010 00:00:00 -07002010-05-31T00:00:00-07:00Copyright (c) 2007 Viktar Karpach