<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="wordpress/2.3.1" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	>

<channel>
	<title>Kai's Blog</title>
	<link>http://bka-bonn.de/wordpress</link>
	<description>This is C++, we all have our segfaults.</description>
	<pubDate>Wed, 23 Feb 2011 11:05:16 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.3.1</generator>
	<language>en</language>
			<item>
		<title>App needs to be executed with admin privileges</title>
		<link>http://bka-bonn.de/wordpress/index.php/2011/02/23/app-needs-to-be-executed-with-admin-privileges/</link>
		<comments>http://bka-bonn.de/wordpress/index.php/2011/02/23/app-needs-to-be-executed-with-admin-privileges/#comments</comments>
		<pubDate>Wed, 23 Feb 2011 11:02:14 +0000</pubDate>
		<dc:creator>Kai</dc:creator>
		
		<category><![CDATA[.NET]]></category>

		<category><![CDATA[General Programming]]></category>

		<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://bka-bonn.de/wordpress/index.php/2011/02/23/app-needs-to-be-executed-with-admin-privileges/</guid>
		<description><![CDATA[It has been some weeks ago when I needed to find a way to make to be executed with full rights. There are several reason why you got to do so e.g. get write-access to C:
It was my aim to show that nice UAC Dialog from Windows itself when the rights are needed. 
And this [...]]]></description>
			<content:encoded><![CDATA[<p>It has been some weeks ago when I needed to find a way to make to be executed with full rights. There are several reason why you got to do so e.g. get write-access to C:</p>
<p>It was my aim to show that nice UAC Dialog from Windows itself when the rights are needed. </p>
<p>And this is how it works:</p>
<p>Create a manifest for you executable which is simple xml:</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span> <span style="font-weight: bold; color: black;">?&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;assembly</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;urn:schemas-microsoft-com:asm.v1&quot;</span> <span style="color: #000066;">manifestVersion</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;assemblyIdentity</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0.0.0&quot;</span>
<span style="color: #000066;">processorArchitecture</span>=<span style="color: #ff0000;">&quot;X86&quot;</span>
<span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;yourApp&quot;</span>
<span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;win32&quot;</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;description<span style="font-weight: bold; color: black;">&gt;</span></span></span>Your App Description<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/description<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;trustInfo</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;urn:schemas-microsoft-com:asm.v3&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;security<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;requestedPrivileges<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;requestedExecutionLevel</span> <span style="color: #000066;">level</span>=<span style="color: #ff0000;">&quot;requireAdministrator&quot;</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/requestedPrivileges<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/security<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/trustInfo<span style="font-weight: bold; color: black;">&gt;</span></span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/assembly<span style="font-weight: bold; color: black;">&gt;</span></span></span></pre></div></div>

</blockquote>
<p>this is the important line:</p>

<div class="wp_syntax"><div class="code"><pre class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;requestedExecutionLevel</span> <span style="color: #000066;">level</span>=<span style="color: #ff0000;">&quot;requireAdministrator&quot;</span> <span style="font-weight: bold; color: black;">/&gt;</span></span></pre></div></div>

<p>Now you can rename it to (YourApp).manifest. The .NET Framework when executing the file will see the Manifest and handle its contents.</p>
<p>Or you can, which I regard as better, embed the .manifest file into you executable.</p>
<blockquote><p>mt -manifest YourApp.exe.manifest -outputresource:YourApp.exe</p></blockquote>
<p>The <strong>mt.exe</strong> come with Visual Studio SDK which can be downloaded at Microsoft&#8217;s.</p>
<p>To simplify this I created a <strong>Post-Build event</strong> which looks like this:</p>
<blockquote><p>&#8220;C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\mt.exe&#8221; -manifest &#8220;$(ProjectDir)$(TargetName).exe.manifest&#8221;  –outputresource:&#8221;$(TargetDir)$(TargetFileName)&#8221;;#1</p></blockquote>
<p>An important note:<br />
If your assembly is strong named, you will be unable to embed the manifest into it as it would invalidate the strong naming.</p>
<p>Some source:</p>
<blockquote><p>
Every assembly, whether static or dynamic, contains a collection of data that describes how the elements in the assembly relate to each other. The assembly manifest contains this assembly metadata. An assembly manifest contains all the metadata needed to specify the assembly&#8217;s version requirements and security identity, and all metadata needed to define the scope of the assembly and resolve references to resources and classes. The assembly manifest can be stored in either a PE file (an .exe or .dll) with Microsoft intermediate language (MSIL) code or in a standalone PE file that contains only assembly manifest information. (http://msdn.microsoft.com/en-us/library/bb756929.aspx)</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://bka-bonn.de/wordpress/index.php/2011/02/23/app-needs-to-be-executed-with-admin-privileges/feed/</wfw:commentRss>
		</item>
		<item>
		<title>UpperCase when comparing strings</title>
		<link>http://bka-bonn.de/wordpress/index.php/2011/02/23/uppercase-when-comparing-strings/</link>
		<comments>http://bka-bonn.de/wordpress/index.php/2011/02/23/uppercase-when-comparing-strings/#comments</comments>
		<pubDate>Wed, 23 Feb 2011 10:42:22 +0000</pubDate>
		<dc:creator>Kai</dc:creator>
		
		<category><![CDATA[.NET]]></category>

		<guid isPermaLink="false">http://bka-bonn.de/wordpress/index.php/2011/02/23/uppercase-when-comparing-strings/</guid>
		<description><![CDATA[I today discovered something I wanted to share with you:
When normalizing strings, it is highly recommended that you use ToUpperInvariant instead of ToLowerInvariant because Microsoft has optimized the code for performing uppercase comparisons.
ToUpperInvariant is preferred because it makes all characters round-trip. See msdn.microsoft.com/en-us/library/bb386042.aspx. For comparisons, write
"a".Equals("A",StringComparison.OrdinalIgnoreCase)
In 99,9 percent of all cases it doesn&#8217;t matter that [...]]]></description>
			<content:encoded><![CDATA[<p>I today discovered something I wanted to share with you:</p>
<p>When normalizing strings, it is highly recommended that you use ToUpperInvariant instead of ToLowerInvariant because Microsoft has optimized the code for performing uppercase comparisons.</p>
<p>ToUpperInvariant is preferred because it makes all characters round-trip. See msdn.microsoft.com/en-us/library/bb386042.aspx. For comparisons, write
<pre lang="c++">"a".Equals("A",StringComparison.OrdinalIgnoreCase)</pre>
<p>In 99,9 percent of all cases it doesn&#8217;t matter that much.</p>
<p>I tried benchmarking ToUpperInvariant vs ToLowerInvariant. I cannot find any difference in their performance under .NET 2.0 or 3.5. Certainly not anything that warrant &#8220;highly recommending&#8221; using one over the other.</p>
]]></content:encoded>
			<wfw:commentRss>http://bka-bonn.de/wordpress/index.php/2011/02/23/uppercase-when-comparing-strings/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Typecasting: Inside the compiler</title>
		<link>http://bka-bonn.de/wordpress/index.php/2010/08/24/150/</link>
		<comments>http://bka-bonn.de/wordpress/index.php/2010/08/24/150/#comments</comments>
		<pubDate>Tue, 24 Aug 2010 10:21:43 +0000</pubDate>
		<dc:creator>Kai</dc:creator>
		
		<category><![CDATA[C++]]></category>

		<category><![CDATA[typecasting]]></category>

		<guid isPermaLink="false">http://bka-bonn.de/wordpress/index.php/2010/08/24/150/</guid>
		<description><![CDATA[I was unsure how Type Casting happens without loss of data inside the compiler.
For example:

 int i = 10;
 UINT k = (UINT) k;

 float fl = 10.123;
 UINT  ufl = (UINT) fl; // data loss here?

 char *p = "bla blub";
 unsigned char *up = (unsigned char *) p;


Now I was wondering how [...]]]></description>
			<content:encoded><![CDATA[<p>I was unsure how Type Casting happens without loss of data inside the compiler.</p>
<p>For example:</p>
<blockquote><pre lang="c++">
 int i = 10;
 UINT k = (UINT) k;

 float fl = 10.123;
 UINT  ufl = (UINT) fl; // data loss here?

 char *p = "bla blub";
 unsigned char *up = (unsigned char *) p;
</pre>
</blockquote>
<p>Now I was wondering how the compiler does handle this type of typecasting?</p>
<p>Well, first of all I got to say that a cast is an <em>explicit request to convert a value of one type to a value of another type</em>. A cast will also always produce a new object, which is a temporary returned by the cast operator. Casting to a reference type, however, will not create a new object. The object referenced by the value is reinterpreted as a reference of a different type.</p>
<p>Now to your question. Note that there are <strong>two major types of conversions</strong>:</p>
<ul>
<li><strong>Promotions</strong>: This type can be thought of casting from a possibly more narrow type to a wider type. Casting from char to int, short to int, float to double are all promotions.</li>
<li><strong>Conversions</strong>: These allow casting from long to int, int to unsigned int and so forth. They can in principle cause lost of information. There are rules for what happens if you assign a -1 to an unsigned typed object for example. In some cases, a wrong conversion can result in undefined behavior. If you assign a double larger than what a float can store to a float, the behavior is not defined.</li>
</ul>
<p>Let&#8217;s have a look at the casts:</p>
<blockquote><pre lang="c++">
int i = 10;
unsigned int k = (unsigned int) i; // :1

float fl = 10.123;
unsigned int  ufl = (unsigned int) fl; // :2

char *p = "bla blub";
unsigned char *up = (unsigned char *) p; // :3
</pre>
</blockquote>
<p>   <strong>1. </strong>This cast causes a conversion to happen. No loss of data happens, since 10 is guaranteed to be stored by an unsigned int. If the integer were negative, the value would basically wrap around the maximal value of an unsigned int (see 4.7/2).</p>
<p>   <strong>2. </strong>The value 10.123 is truncated to 10. Here, it does cause lost of information, obviously. As 10 fits into an unsigned int, the behavior is defined.</p>
<p>   <strong>3. </strong>This actually requires more attention. First, there is a deprecated conversion from a string literal to char*. But let&#8217;s ignore that here. (see here). More importantly, what does happen if you cast to an unsigned type? Actually, the result of that is unspecified per 5.2.10/7 (note the semantics of that cast is the same as using reinterpret_cast in this case, since that is the only C++ cast being able to do that):</p>
<p>    A pointer to an object can be explicitly converted to a pointer to an object of different type. Except that converting an rvalue of type “pointer to T1” to the type &#8220;pointer to T2&#8243; (where T1 and T2 are object types and where the alignment requirements of T2 are no stricter than those of T1) and back to its original type yields the original pointer value, the result of such a pointer conversion is unspecified.</p>
<p>So you are only safe to use the pointer after you cast back to char * again.</p>
]]></content:encoded>
			<wfw:commentRss>http://bka-bonn.de/wordpress/index.php/2010/08/24/150/feed/</wfw:commentRss>
		</item>
		<item>
		<title>foreach loop with index</title>
		<link>http://bka-bonn.de/wordpress/index.php/2010/05/19/foreach-loop-with-index/</link>
		<comments>http://bka-bonn.de/wordpress/index.php/2010/05/19/foreach-loop-with-index/#comments</comments>
		<pubDate>Wed, 19 May 2010 13:49:23 +0000</pubDate>
		<dc:creator>Kai</dc:creator>
		
		<category><![CDATA[.NET]]></category>

		<category><![CDATA[c# stuff helper]]></category>

		<guid isPermaLink="false">http://bka-bonn.de/wordpress/index.php/2010/05/19/foreach-loop-with-index/</guid>
		<description><![CDATA[I was fed up with declaring a &#8220;helper&#8221; variable for every foreach in which I needed a counter.
The solution is very cool I think:
My list of strings I&#8217;d like to iterate:


List&#60;string&#62; myStrings = new List&#60;string&#62;&#40;&#41;
            &#123;
         [...]]]></description>
			<content:encoded><![CDATA[<p>I was fed up with declaring a &#8220;helper&#8221; variable for every foreach in which I needed a counter.<br />
The solution is very cool I think:</p>
<p>My list of strings I&#8217;d like to iterate:</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="csharp">List&lt;string&gt; myStrings = <span style="color: #008000;">new</span> List&lt;string&gt;<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #808080;">&quot;abc&quot;</span>, 
                <span style="color: #808080;">&quot;def&quot;</span>,
                <span style="color: #808080;">&quot;xyz&quot;</span>
            <span style="color: #000000;">&#125;</span>;</pre></div></div>

</blockquote>
<p>Old style:</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="csharp">            <span style="color: #FF0000;">int</span> index = <span style="color: #FF0000;">0</span>;
            <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> s <span style="color: #0600FF;">in</span> myStrings<span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;{0}: {1}&quot;</span>, index++, s<span style="color: #000000;">&#41;</span>;
            <span style="color: #000000;">&#125;</span></pre></div></div>

</blockquote>
<p>cool way using .net 3.5&#8217;s lambda syntax:</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="csharp">            <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>var o <span style="color: #0600FF;">in</span> myStrings.<span style="color: #0000FF;">OfType</span>&lt;object&gt;<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Select</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>x, i<span style="color: #000000;">&#41;</span> =&gt; <span style="color: #008000;">new</span> <span style="color: #000000;">&#123;</span> x, i <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;{0}: {1}&quot;</span>, o.<span style="color: #0000FF;">i</span>, o.<span style="color: #0000FF;">x</span><span style="color: #000000;">&#41;</span>;  
            <span style="color: #000000;">&#125;</span></pre></div></div>

</blockquote>
]]></content:encoded>
			<wfw:commentRss>http://bka-bonn.de/wordpress/index.php/2010/05/19/foreach-loop-with-index/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Dr Watson has died</title>
		<link>http://bka-bonn.de/wordpress/index.php/2009/10/29/dr-watson-has-died/</link>
		<comments>http://bka-bonn.de/wordpress/index.php/2009/10/29/dr-watson-has-died/#comments</comments>
		<pubDate>Thu, 29 Oct 2009 13:58:12 +0000</pubDate>
		<dc:creator>Kai</dc:creator>
		
		<category><![CDATA[General Programming]]></category>

		<category><![CDATA[Software]]></category>

		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://bka-bonn.de/wordpress/index.php/2009/10/29/dr-watson-has-died/</guid>
		<description><![CDATA[If you need to get a crashdump running Windows Vista you&#8217;ll look for DRWTSN32.EXE for ever and a day. Unfortunately, it&#8217;s in vain, Dr Watson died…
Nevertheless that app has been very important for collateral quality control.
Vista has increased the intricacy of everything a lot. In general Vista is not saving minidumps, but keeps account of [...]]]></description>
			<content:encoded><![CDATA[<p>If you need to get a crashdump running Windows Vista you&#8217;ll look for DRWTSN32.EXE for ever and a day. <strong>Unfortunately, it&#8217;s in vain, Dr Watson died…</strong></p>
<p>Nevertheless that app has been very important for collateral quality control.</p>
<p>Vista has increased the intricacy of everything a lot. In general <strong>Vista is not saving minidumps, but keeps account of every crash or error report.</strong> If an app is WER registrated a crashdump is stored as well - if it&#8217;s not no crashdump is at dumpfolder. Neither here nor there.</p>
<p>This is how it works on Vista (as fair as I could figure out):</p>
<ul>
<li>WER just makes a crashdump for WER signed apps respectively if the WER server requests a report</li>
<li>   To get a minidump the following regesty value has to be set:<br />
     <em> HKLM\SOFTWARE\Microsoft\Windows\Windows Error Reporting\ (DWORD) </em> create a key named &#8220;<em>ForceQueue</em>&#8221; and set it to 1.</li>
<li>   Dumps are getting stored in user directory <em>C:\Users\TheUserName\AppData\Local\Temp</em> and <em>C:\ProgramData\Microsoft\Windows\WER\ReportQueue</em>. File extension is <em>*.mdmp</em>.</li>
<li>You can have an overview at <em>Control Panel-> maintenance -> Problems &#038; Solutions -> Check Problems</em></li>
</ul>
<p>You also can find several instructions on the web howto install Dr Watson on Vista. There also seems to a tool called <em>DrVista</em> that might help you - which I didn&#8217;t try&#8230;</p>
<p><img src="http://www.bbc.co.uk/bbcfour/drama/images/holmes5.jpg" alt="Watson" /></p>
]]></content:encoded>
			<wfw:commentRss>http://bka-bonn.de/wordpress/index.php/2009/10/29/dr-watson-has-died/feed/</wfw:commentRss>
		</item>
		<item>
		<title>How to read a technical book to remember most of it?</title>
		<link>http://bka-bonn.de/wordpress/index.php/2009/08/12/how-to-read-a-technical-book-to-remember-most-of-it/</link>
		<comments>http://bka-bonn.de/wordpress/index.php/2009/08/12/how-to-read-a-technical-book-to-remember-most-of-it/#comments</comments>
		<pubDate>Wed, 12 Aug 2009 09:34:46 +0000</pubDate>
		<dc:creator>Kai</dc:creator>
		
		<category><![CDATA[Books]]></category>

		<guid isPermaLink="false">http://bka-bonn.de/wordpress/index.php/2009/08/12/how-to-read-a-technical-book-to-remember-most-of-it/</guid>
		<description><![CDATA[First of all I&#8217;d like to say that there&#8217;s of course not a winning concept for everyone.
There are many technical books that become thicker and thicker and the pressure from the technical society is more and more to read them and remember many concepts described in them. But it&#8217;s so hard to do it.
How do [...]]]></description>
			<content:encoded><![CDATA[<p>First of all I&#8217;d like to say that there&#8217;s of course not a winning concept for everyone.</p>
<p>There are many technical books that become thicker and thicker and the pressure from the technical society is more and more to read them and remember many concepts described in them. But it&#8217;s so hard to do it.</p>
<p>How do you remember all that stuff? Lots of people <strong>use some cards</strong> with basic info so that they can remember the details when they look at it.</p>
<p>Sometimes I read a chapter, wait a while, and go back <strong>through the chapter with a highlighter</strong>. Then, if I ever have to go back in the book, I can just read the highlighted nuggets. But that&#8217;s just something you can do with your own books&#8230;</p>
<p>Generally, technical books come with lots of little example snippets of code or exercises. I always <strong>try (if there&#8217;s sufficient time) to do them</strong>. But after you&#8217;ve done them, think about where you can apply those concepts in code you&#8217;ve already written. Go back and refactor with those ideas in mind. Once you see how it can work in a real live project, it <strong>will be burned into your brain</strong>.</p>
<p>A really good coder once told me&#8230;If you read something in a technical book you don&#8217;t understand, <strong>do something with that concept 5 times in code</strong>.<br />
Make 5 separate, little, non-related one-off projects. By the second time around you will probably understand it, and the other 3 are just to get your fingers used to typing it. Don&#8217;t proceed further in the book until you do.</p>
<p>If you do not practice it you will not learn it except you&#8217;re <em>rain man</em> but nobody is <em>rain man</em> except for <em>rain man</em>. <img src='http://bka-bonn.de/wordpress/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p><img src="http://www.bka-bonn.de/pic/rain_man.jpg" border="0"></p>
<p>The learning by doing principle is good but my experience tells me that it is not enough. Moreover, remembering is no longer mandatory. <strong>Accessibility of resources is such that finding them is more important than learning them.</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://bka-bonn.de/wordpress/index.php/2009/08/12/how-to-read-a-technical-book-to-remember-most-of-it/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Short precious code snippet</title>
		<link>http://bka-bonn.de/wordpress/index.php/2009/08/12/short-precious-code-snippet/</link>
		<comments>http://bka-bonn.de/wordpress/index.php/2009/08/12/short-precious-code-snippet/#comments</comments>
		<pubDate>Tue, 11 Aug 2009 22:45:09 +0000</pubDate>
		<dc:creator>Kai</dc:creator>
		
		<category><![CDATA[.NET]]></category>

		<category><![CDATA[C++]]></category>

		<category><![CDATA[LINQ]]></category>

		<category><![CDATA[snippet]]></category>

		<guid isPermaLink="false">http://bka-bonn.de/wordpress/index.php/2009/08/12/short-precious-code-snippet/</guid>
		<description><![CDATA[I was recently talking about linq features with a friend. Now I saw somebody wanted to break a loop after 50 iterations. Trivial but also for that there&#8217;s a precious solution using linq.


int processed = 0;
foreach&#40;ListViewItem lvi in listView.Items&#41;
&#123;
   //do stuff
   ++processed;
   if &#40;processed == 50&#41; break;
&#125;


use linq


foreach&#40;ListViewItem lvi [...]]]></description>
			<content:encoded><![CDATA[<p>I was recently talking about linq features with a friend. Now I saw somebody wanted to break a loop after 50 iterations. Trivial but also for that there&#8217;s a precious solution using linq.</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="csharp"><span style="color: #FF0000;">int</span> processed = <span style="color: #FF0000;">0</span>;
<span style="color: #0600FF;">foreach</span><span style="color: #000000;">&#40;</span>ListViewItem lvi <span style="color: #0600FF;">in</span> listView.<span style="color: #0000FF;">Items</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
   <span style="color: #008080; font-style: italic;">//do stuff</span>
   ++processed;
   <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>processed == <span style="color: #FF0000;">50</span><span style="color: #000000;">&#41;</span> <span style="color: #0600FF;">break</span>;
<span style="color: #000000;">&#125;</span></pre></div></div>

</blockquote>
<p>use linq</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="csharp"><span style="color: #0600FF;">foreach</span><span style="color: #000000;">&#40;</span>ListViewItem lvi <span style="color: #0600FF;">in</span> listView.<span style="color: #0000FF;">Items</span>.<span style="color: #0000FF;">Take</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">50</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">//do stuff</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

</blockquote>
<p>or, you&#8217;re right &#8220;old&#8221; style would be</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="csharp"><span style="color: #0600FF;">for</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> i=<span style="color: #FF0000;">0</span>; i &lt; listView.<span style="color: #0000FF;">Items</span>.<span style="color: #0000FF;">Count</span> &amp;&amp; i &lt;= <span style="color: #FF0000;">50</span>; i++<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
   ListViewItem lvi = listView.<span style="color: #0000FF;">Items</span><span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>;
  <span style="color: #008080; font-style: italic;">//do stuff</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

</blockquote>
]]></content:encoded>
			<wfw:commentRss>http://bka-bonn.de/wordpress/index.php/2009/08/12/short-precious-code-snippet/feed/</wfw:commentRss>
		</item>
		<item>
		<title>E-Mail obfuscation - a disputed question</title>
		<link>http://bka-bonn.de/wordpress/index.php/2009/08/11/e-mail-obfuscation-a-disputed-question/</link>
		<comments>http://bka-bonn.de/wordpress/index.php/2009/08/11/e-mail-obfuscation-a-disputed-question/#comments</comments>
		<pubDate>Tue, 11 Aug 2009 15:15:52 +0000</pubDate>
		<dc:creator>Kai</dc:creator>
		
		<category><![CDATA[General Programming]]></category>

		<category><![CDATA[Internet]]></category>

		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://bka-bonn.de/wordpress/index.php/2009/08/11/e-mail-obfuscation-a-disputed-question/</guid>
		<description><![CDATA[Many users and forum programs in attempt to make automatic e-mail address harversting harder conseal them via obfuscation - @ is replaced with &#8220;at&#8221; and . is replaces with &#8220;dot&#8221;, so
bill.gates@microsoft.com
now becomes
bil dot gates at microsoft dot com
I&#8217;m not an expert in regular expressions and I&#8217;m really curious - does such obfuscation really make automatic [...]]]></description>
			<content:encoded><![CDATA[<p>Many users and forum programs in attempt to make automatic e-mail address harversting harder conseal them via obfuscation - @ is replaced with &#8220;at&#8221; and . is replaces with &#8220;dot&#8221;, so</p>
<blockquote><p>bill.gates@microsoft.com</p></blockquote>
<p>now becomes</p>
<blockquote><p>bil dot gates at microsoft dot com</p></blockquote>
<p>I&#8217;m not an expert in regular expressions and I&#8217;m really curious - does such obfuscation really make automatic harvesting harder? Is it really much harder to automatically identify such obfuscated addresses?</p>
<p>For example, if every email address on a large community site is reversed in the markup and rendered properly with CSS, or token-replaced (@ becomes &#8216;at&#8217;), or any other predictable method, the harvesters will just write a thin adapter for your site.</p>
<p>Think of it this way: <strong>if it only takes you one line of code to &#8220;scramble&#8221; them sitewide, it will only take the harvester one line of code to &#8220;unscramble&#8221; them</strong> for your site. Roughly speaking.</p>
<p>What concept is the right? Do more complex obfuscation or consider about new ways?</p>
<p><img src="http://www.bka-bonn.de/pic/obfuse.jpg" border="0"></p>
<p>Obfuscation techniques fall in the same category than captchas. <strong>They are not reliable and tend to hurt regular users more than bots.</strong></p>
<p>Javascript obfuscation seems to be praised, but is no silver bullet: it is not that hard today to automate a browser for email sniffing. If it can be displayed in a browser, it can be harvested. You could even imagine a bot that&#8217;s taking screenshots of a browser window and using <em>OCR</em> to extract addresses to beat your million-dollar-obfuscation-technique.</p>
<p>Depending on where and why you want to obfuscate emails, those techniques could be useful:</p>
<ul>
<li><strong>Restrict email visibility:</strong> you may hide emails on your website/forum to anonymous users, to new users (with little to no activity or posts to date) or even hide them completely and replace email contact between members with a built-in private messaging feature.</li>
<li><strong>Use a dedicated spam-filtered email:</strong> you will get spammed, but it will be limited to this particular address. This is a good trade-off when you need to expose the email address to any user.</li>
<li><strong>Use a contact form:</strong> while bots are pretty good at filling forms, it turns out that they are too good at filling forms. Hidden field techniques can filter most of the spam coming through your contact form.</li>
</ul>
<p>One common way of hiding email from bots and spammers is to <strong>create an image containing the email address</strong>. <em>Facebook does this, for instance.</em> Now, using images for email is inherently bad for accessibility, because text readers will not be able to read it. But even otherwise, there are several free character recognition programs that do a pretty good of decoding such email-images.</p>
<p><strong>At least you have always to keep in brain that if it&#8217;s difficult for the spammers it&#8217;s as well your users to identify the email address.</strong> <a href="http://en.wikipedia.org/wiki/Address_munging" target="_blanc">A nice article from wikipedia on Email obfuscation or address munging</a> you&#8217;d pay regard to. </p>
<p><strong>The real question is whether the extra effort will be put in by harvesters and if the (major? minor?) barrier to the harvesters is worth the possible problems for your users.</strong></p>
<p>&#8212;</p>
<p>Finally this article is as so many about <strong>fighting spam</strong> - In my opinion, spam has become such a problem and so many databases have been turned over that we&#8217;re beyond hiding our addresses. Instead,<strong> consider of more efficient ways of classifying and blocking spam</strong>.</p>
]]></content:encoded>
			<wfw:commentRss>http://bka-bonn.de/wordpress/index.php/2009/08/11/e-mail-obfuscation-a-disputed-question/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Firefox and its market share</title>
		<link>http://bka-bonn.de/wordpress/index.php/2009/07/01/firefox-and-its-market-share/</link>
		<comments>http://bka-bonn.de/wordpress/index.php/2009/07/01/firefox-and-its-market-share/#comments</comments>
		<pubDate>Wed, 01 Jul 2009 14:12:24 +0000</pubDate>
		<dc:creator>Kai</dc:creator>
		
		<category><![CDATA[Internet]]></category>

		<guid isPermaLink="false">http://bka-bonn.de/wordpress/index.php/2009/07/01/firefox-and-its-market-share/</guid>
		<description><![CDATA[Until Wednesday noon over 3.8 million users worldwide downloaded the new version of Firefox from the Internet. Firefox 3.5 is more than twice as fast working as the previous version, and above all more stable running. The software now uses better the performance, the modern computer with multi-core processors available. According to the developers over [...]]]></description>
			<content:encoded><![CDATA[<p>Until Wednesday noon over 3.8 million users worldwide downloaded the new version of Firefox from the Internet. Firefox 3.5 is more than twice as fast working as the previous version, and above all more stable running. The software now uses better the performance, the modern computer with multi-core processors available. According to the developers over 5000 new freatures are in the sources, of course most only will be recognized at second glance.</p>
<p>Nevertheless it&#8217;s amazing (or maybe it should make me blue) that the market share of Firefox is still just about 22 percent. In comparison to that the market share of the Internet Explorer is about 65 percent. </p>
<p>It seems to me that besides a lot of companies whose hands are tied using another browser caused by a partnership or a similar contract with Microsoft there must be a great number of normal pc users that seem to like IE&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://bka-bonn.de/wordpress/index.php/2009/07/01/firefox-and-its-market-share/feed/</wfw:commentRss>
		</item>
		<item>
		<title>LINQ to Amazon</title>
		<link>http://bka-bonn.de/wordpress/index.php/2009/07/01/linq-to-amazon/</link>
		<comments>http://bka-bonn.de/wordpress/index.php/2009/07/01/linq-to-amazon/#comments</comments>
		<pubDate>Wed, 01 Jul 2009 13:55:41 +0000</pubDate>
		<dc:creator>Kai</dc:creator>
		
		<category><![CDATA[.NET]]></category>

		<guid isPermaLink="false">http://bka-bonn.de/wordpress/index.php/2009/07/01/linq-to-amazon/</guid>
		<description><![CDATA[As you know you can query with LINQ lots of different sourcse. It&#8217;s possible to query, project and filter data in arrays, enumerable classes, XML (XLINQ), relational database, and third party data sources. Last year I among others wrote something about LINQ &#038; XML.
After doing some query we get results of a as a collection [...]]]></description>
			<content:encoded><![CDATA[<p>As you know you can query with LINQ lots of different sourcse. It&#8217;s possible to query, project and filter data in arrays, enumerable classes, XML (XLINQ), relational database, and third party data sources. Last year I among others wrote something about <a href="http://bka-bonn.de/wordpress/index.php/2008/03/11/xml-the-linq-way/" target="_blanc">LINQ &#038; XML</a>.</p>
<p>After doing some query we get results of a as a collection of in-memory objects that can be enumerated using a standard iterator function such as C#&#8217;s foreach. </p>
<p>I found a funny Provider called <strong>Linq to Amazon</strong> which allows querying Amazon for books using Linq! It uses Linq&#8217;s extensibility to allow for language-integrated queries against a book catalog. The Linq query gets converted to REST URLs supported by Amazon&#8217;s web services. These services return XML. The results are converted from XML to .NET objects using Linq to XML.</p>
<p>For the moment, let&#8217;s look at the client code:</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="csharp">var query =
  from book <span style="color: #0600FF;">in</span> <span style="color: #008000;">new</span> Amazon.<span style="color: #0000FF;">BookSearch</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
  where
    book.<span style="color: #0000FF;">Title</span>.<span style="color: #0000FF;">Contains</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;darkness and light&quot;</span><span style="color: #000000;">&#41;</span> &amp;&amp;
    <span style="color: #000000;">&#40;</span>book.<span style="color: #0000FF;">Publisher</span> == <span style="color: #808080;">&quot;Hitchcock&quot;</span><span style="color: #000000;">&#41;</span> &amp;&amp;
    <span style="color: #000000;">&#40;</span>book.<span style="color: #0000FF;">Price</span> &lt;= <span style="color: #FF0000;">25</span><span style="color: #000000;">&#41;</span> &amp;&amp;
    <span style="color: #000000;">&#40;</span>book.<span style="color: #0000FF;">Condition</span> == BookCondition.<span style="color: #008000;">New</span><span style="color: #000000;">&#41;</span>
  select book;</pre></div></div>

</blockquote>
<p>I think this code speaks for itself! This is Linq to Amazon code. It expresses a query against Amazon, but does not execute it&#8230; The query will be executed when we start enumerating the results.</p>
<p>The following piece of code converts from Linq to Amazon to Linq to Objects:</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="csharp">var sequence = query.<span style="color: #0000FF;">ToSequence</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</pre></div></div>

</blockquote>
<p>The <code>var</code> might remind you to the times of Flash, VB or JavaScript. <strong>But those times were baaad, very bad</strong>. Nowadays we&#8217;re strongly typed or even better generic:</p>
<p>A good alternative is:</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="csharp">IEnumerable&lt;SomeOtherClass&gt; results = ...</pre></div></div>

</blockquote>
<p>The returned catalogue can now be grouped, filtered more detailed or just thrown away. Do whatever you like todo <img src='http://bka-bonn.de/wordpress/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p>&#8212;</p>
<p>There are many more, more or less usefull ones:</p>
<ul>
<li>LINQ to SharePoint</li>
<li>LINQ to Amazon</li>
<li>LINQ to Active Directory (LDAP)</li>
<li>LINQ to NHibernate</li>
<li>LINQ to MySQL / Oracle / SQLite</li>
<li>LINQ to Flickr</li>
</ul>
<p>Have fun <img src='http://bka-bonn.de/wordpress/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://bka-bonn.de/wordpress/index.php/2009/07/01/linq-to-amazon/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>

