<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0"><channel><atom:link rel="hub" href="http://tumblr.superfeedr.com/" xmlns:atom="http://www.w3.org/2005/Atom"/><description>vanguard, noun - any creative group active in the innovation and application of new concepts and techniques in a given field (especially in the arts) 

confusion, noun - a mental state characterized by a lack of clear and orderly thought and behavior; “a confusion of impressions”</description><title>Vanguard Against Confusion</title><generator>Tumblr (3.0; @vanguard-against-confusion)</generator><link>http://vanguard-against-confusion.tumblr.com/</link><item><title>Crowd-Sourced Relevancy Ranking in Bing or why Google is wrong</title><description>&lt;p&gt;Let me start by saying I&amp;#8217;m a Google fanboy and a half. I love that company and use all their products. My life revolves around my Nexus One and it&amp;#8217;s deep and tight integration with Google&amp;#8217;s stack which is my extremely large clustered brain-annex that&amp;#8217;s constantly available with a few taps of my touchscreen.&lt;/p&gt;
&lt;p&gt;Google is awesome and great and I love them to death. That said, they are wrong about Bing copying their results. &lt;/p&gt;
&lt;p&gt;See these first-party posts for details:&lt;/p&gt;
&lt;p&gt;&lt;a target="_self" href="http://www.bing.com/community/site_blogs/b/search/archive/2011/02/01/thoughts-on-search-quality.aspx"&gt;Thoughts On Search Quality (Bing Blog)&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://googleblog.blogspot.com/2011/02/microsofts-bing-uses-google-search.html"&gt;Bing uses Google&amp;#8217;s Results &amp;#8212; and denies it (Google Blog)&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Why Microsoft is not in the wrong: Crowd Sourcing is not stealing&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;What Microsoft has done is create a genuinely useful process for improving the relevancy of the Bing search engine. It&amp;#8217;s not a groundbreaking technique. Like most web based application in the world, they just monitor your behaviour. You type &amp;#8216;foo&amp;#8217; into their search box, and are presented with 10 results on your first screen. You click #2 result. This is recorded. The same thing happens for 10,000 other users. Eventually Bing figures out that the #2 result really should be the #1 result, and ups it&amp;#8217;s rank.&lt;/p&gt;
&lt;p&gt;This is no different than Amazon&amp;#8217;s suggestion engine &amp;#8216;Users who viewed this product ultimately bought this other product&amp;#8217;. &lt;/p&gt;
&lt;p&gt;This process doesn&amp;#8217;t depend on Google. This process would work purely as a way of improving rank within the Bing system, with no outside influences. It also doesn&amp;#8217;t really require a special toolbar to make it happen. You could collect that data through their normal web interface just as easily. &lt;/p&gt;
&lt;p&gt;This is not an issue of spyware, cheating, or copying. It&amp;#8217;s just Bing using crowd-sourced data to determine relevancy. It&amp;#8217;s very smart. It&amp;#8217;s not dishonest. Get over it Google.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;How to improve search result relevancy?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;I find this interesting because, currently Google faces a huge challenge: Reducing the amount of spam that is polluting their results. Bing clearly has a tactic for that, though it may or may not be completely effective. The value of the click through data is only as good as the user who clicked on the result. If the majority of people searching for &amp;#8220;Foo&amp;#8221; clicked on say, option #3 instead of #2 in our previous example, and if #3 was a spam site, then #3 would get ranked up. Bad news!&lt;/p&gt;
&lt;p&gt;Looks like Bing is trusting our judgement as a user community. We know what&amp;#8217;s relevant and show it by clicking through. If we choose a spam site as our main result, Cest La Vie!!&lt;/p&gt;
&lt;p&gt;But how can we improve search relevancy, and reduce false positives in our result set? The answer so far seems to be &amp;#8220;curate the web&amp;#8221;, or like Bing, use a &amp;#8220;mechanical turk&amp;#8221;, aka click stream crowd-sourced relevancy, assuming that people will be able to express their preference as a whole and emerge the correct answer over time.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Our solution: Contextual Search&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;My company has a different solution: Working at a higher level of abstraction than words and documents. We have built a novel search tool that allows users to search for contexts, not documents, and make decisions on contexts. &lt;/p&gt;
&lt;p&gt;Find me &amp;#8220;license&amp;#8221; where the document has &amp;#8220;drivers license&amp;#8221; in it, but not where it has &amp;#8220;fishing license&amp;#8221;, unless it also has something I want in it like &amp;#8220;drivers license&amp;#8221;. Traditional boolean search, which operates against an inverted index of terms to documents (which is what both Bing and Google offer), does not provide for this kind of decision making. It&amp;#8217;s impossible without changing how the data is indexed, and that&amp;#8217;s not anything these guys are going to be doing anytime soon. They have too much invested in their current methodology to change. &lt;/p&gt;
&lt;p&gt;We&amp;#8217;re hoping to launch a public search site sometime this year that presents our novel approach to improving relevancy in search. I look forward to seeing how it performs compared to Google and Bing. &lt;/p&gt;
&lt;p&gt;More on that later, when it&amp;#8217;s closer to reality. &lt;/p&gt;
&lt;p&gt;&lt;strong&gt;What do you think, world at large?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Does anyone have any other ideas about search relevancy? What are some other tactics one might employ, beyond Curating or Crowdsourcing? How else to make the spam go away?&lt;/p&gt;</description><link>http://vanguard-against-confusion.tumblr.com/post/3067650350</link><guid>http://vanguard-against-confusion.tumblr.com/post/3067650350</guid><pubDate>Wed, 02 Feb 2011 06:57:00 -0800</pubDate><category>bing</category><category>google</category><category>search</category><category>relevancy</category><category>crowd sourced</category><category>ranking</category><dc:creator>thoward37</dc:creator></item><item><title>migration</title><description>&lt;p&gt;moving &lt;a title="Vanguard Against Confusion at Tumblr" target="_blank" href="http://vanguard-against-confusion.tumblr.com/"&gt;here&lt;/a&gt; from &lt;a title="Vanguard Against Confusion at Blogger.com" target="_blank" href="http://vanguard-against-confusion.blogspot.com/"&gt;there&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I decided to migrate the blog off of Blogger.com and onto Tumblr, for no particular reason other than Tumblr is simply cooler and has more features.&lt;/p&gt;
&lt;p&gt;Some of tools I used were:&lt;/p&gt;

&lt;p&gt;Import from Blogger to Tumblr Tool &lt;/p&gt;
&lt;p&gt;&lt;a href="http://terrymhung.com/jtran/tumblr/import-blogger-to-tumblr.php"&gt;&lt;a href="http://terrymhung.com/jtran/tumblr/import-blogger-to-tumblr.php"&gt;http://terrymhung.com/jtran/tumblr/import-blogger-to-tumblr.php&lt;/a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Syntax Highlighter from Alex Gorbatchev&lt;/p&gt;
&lt;p&gt;&lt;a href="http://alexgorbatchev.com/SyntaxHighlighter/"&gt;&lt;a href="http://alexgorbatchev.com/SyntaxHighlighter/"&gt;http://alexgorbatchev.com/SyntaxHighlighter/&lt;/a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://disqus.com"&gt;Disqus.com&lt;/a&gt; for comments support&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.howtogeek.com/howto/22032/add-traditional-comments-to-tumblr-blogs-with-disqus/"&gt;&lt;a href="http://www.howtogeek.com/howto/22032/add-traditional-comments-to-tumblr-blogs-with-disqus/"&gt;http://www.howtogeek.com/howto/22032/add-traditional-comments-to-tumblr-blogs-with-disqus/&lt;/a&gt;&lt;/a&gt;&lt;/p&gt;</description><link>http://vanguard-against-confusion.tumblr.com/post/2519538837</link><guid>http://vanguard-against-confusion.tumblr.com/post/2519538837</guid><pubDate>Wed, 29 Dec 2010 17:02:00 -0800</pubDate><category>blogger.com</category><category>tumblr.com</category><category>migration</category><dc:creator>thoward37</dc:creator></item><item><title>010 Editor: My new favourite tool</title><description>&lt;p&gt;&lt;span&gt;During the course of my work, I use a hex editor a lot.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Specifically, I use a hex editor most for reverse engineering binary file formats that have no documentation or for fixing corrupted files, and the like. One thing that I&amp;#8217;ve always wanted, was some way to view the binary contents as structured data.. Like &amp;#8220;Starting at this byte offset, consider the next four bytes to be an integer, and show me that integer, then, using that integer, take that many bytes immediately following it, consider them to be string data, and decode as UTF8 or EBCDIC&amp;#8230; etc.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;All that is fairly complex, and generally well beyond the facilities of anything short of a fairly low level and full-featured programming language.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;Well, looks like the folks over at &lt;a href="http://www.sweetscape.com/"&gt;SweetScape&lt;/a&gt; realized this is a workflow that at least SOME people need to have&amp;#8230; and so they built the perfect tool for doing that. It&amp;#8217;s called the &lt;a href="http://www.sweetscape.com/010editor/"&gt;010 Editor&lt;/a&gt;. It has this great feature called Binary Templates as well as scripts. It&amp;#8217;s the bomb, and it&amp;#8217;s accelerated my reverse engineering work by at least an order of magnitude.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;I&amp;#8217;m buying a license, and anyone know know me will realize that&amp;#8217;s a pretty big deal. I&amp;#8217;m a big fan of FOSS and generally try to use as much of it as possible, avoiding commercial apps&amp;#8230; but this is a big exception. SweetScape is a small company run by a father and son team. Hardly &amp;#8220;The Man&amp;#8221;. Lowell and Graeme Sweet &amp;#8212; you rock the block and treat the bytes right. ;)&lt;/span&gt;&lt;/p&gt;</description><link>http://vanguard-against-confusion.tumblr.com/post/2515367207</link><guid>http://vanguard-against-confusion.tumblr.com/post/2515367207</guid><pubDate>Mon, 02 Aug 2010 16:49:00 -0700</pubDate><category>010editor</category><category>a better way</category><category>binary templates</category><category>hex editor</category><category>reverse engineering</category><category>SweetScape</category><category>tools</category><dc:creator>thoward37</dc:creator></item><item><title>Big Data</title><description>&lt;p&gt;&lt;span&gt;When I was a kid &amp;#8212; 7 years old &amp;#8212; I got my first computer. At that time, using a computer meant programming a computer. My Commodore VIC20 didn&amp;#8217;t have a storage device when I got it (I got a tape drive later on). So, if you turned it off, the in memory program was gone. You had to type it in again the next time you started up the computer.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;I remember learning about programming. One of my first programs was just:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;span&gt;10 PRINT &amp;#8220;TROY&amp;#8221;&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;I was delighted to see my name show up on the screen. The computer knew me now. I was being reflected back from the screen in glowing pixels. I&amp;#8217;d taken a little bit of me, and put it into the computer, and the computer was letting me look at it, and learn about myself, and learn about the computer, at the same time. I was hooked.&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;strong&gt;10 FOR I = 1 TO 10&lt;br/&gt;20 PRINT &amp;#8220;TROY&amp;#8221;&lt;br/&gt;30 NEXT&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;This blew my mind. While there was something magical about seeing my name on the screen the first time. Seeing it ten times was just really really exciting. Something about the quantity was just really cool&amp;#8230; and how FAST it happened. It put my name up there ten times as fast as it put it up there one time!! Thrilling!!&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;strong&gt;10 PRINT &amp;#8220;TROY&amp;#8221;&lt;br/&gt;20 GOTO 10&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;The screen was filled with my name&amp;#8230;. and it didn&amp;#8217;t stop. I got up and I ran to tell my mom. I drug her downstairs into the basement and babbled on about the computer and how it was just going and going, and how I did it. GOTO mom&amp;#8230; GOTO! My excitement was more than I could contain. I was officially a rocket scientist now. My excitement, like the program was &lt;strong&gt;INFINITE!!!&lt;/strong&gt; I created something that was endless, infinitely long. There was no way to count how many times it put my name on the screen. My computer, at that moment, knew only one thing &amp;#8212; my name, my program, and it would run it forever until I told it to stop. Happily. This was a kind of love and dedication that was far beyond what any person could ever give. There was something really deep here, between me and the computer.&lt;/p&gt;
&lt;p&gt;Fast forward now, 24 years later. I&amp;#8217;m a software engineer for a living now, and have been for a while. At my company, I recently got promoted to a fancier title &amp;#8220;Director of Software Development&amp;#8221;, and all the responsibility for success lies on my shoulders. People answer my job advertisements with the salutation &amp;#8220;Mr. Howard&amp;#8221;. That part freaks me out.&lt;/p&gt;
&lt;p&gt;I&amp;#8217;m still excited by big data. That infinite loop of &lt;strong&gt;&lt;span&gt;TROY&lt;/span&gt;&lt;/strong&gt;s on my screen was just the start. Now I design systems that process terabytes of data at a time on hundreds of servers. One of the most fascinating parts of my job these days is still the same as when I was a kid. I love hitting &amp;#8220;Run&amp;#8221; on a unit test, and seeing what happens. I feel good when it&amp;#8217;s successful once. My next step, almost without fail, is to see what happens when it runs ten times in a row.. Then 100&amp;#8230; Then 1000&amp;#8230; 10000&amp;#8230; 100000&amp;#8230; I just keep adding zeros until the thing breaks down, or until I get bored with it.&lt;/p&gt;
&lt;p&gt;Big Data is still exciting, still fascinating. I&amp;#8217;ve now given my computer programs more interesting sample material to work from, and so their world view has expanded. Now instead of only knowing me and my name, my programs know all the details of the personal and business lives of thousands of people whose email is processed by the programs. I think my computer still loves me more than any of them though. Secretly, somewhere in there, I know there&amp;#8217;s an infinite loop on a background thread that&amp;#8217;s just cycling over the string &amp;#8220;TROY&amp;#8221;&amp;#8230; forever.&lt;/p&gt;</description><link>http://vanguard-against-confusion.tumblr.com/post/2515456304</link><guid>http://vanguard-against-confusion.tumblr.com/post/2515456304</guid><pubDate>Fri, 12 Feb 2010 20:31:00 -0800</pubDate><category>big data</category><dc:creator>thoward37</dc:creator></item><item><title>An Infinite Stream Of Bytes</title><description>&lt;p&gt;No, I&amp;#8217;m not about to wax poetic about the deep ontological issues raised in The Matrix, or speak meaningfully about how transient the modern world of communication is and how the artifacts of our lifetime have become ephemeral such that our posterity will not be able to remember us, even if they wanted to.&lt;br/&gt;&lt;br/&gt;Instead I&amp;#8217;m going to post a code snippet that solves an annoying little scenario that comes up every now and again when writing parsers. &lt;br/&gt;&lt;br/&gt;Basically, it goes like this: &lt;br/&gt;&lt;br/&gt;You&amp;#8217;re writing a parser, and you need to check every byte in a stream of bytes coming from a file/network/etc.. You might need to read forward or read backward a little, to match a multi-byte pattern or value within &lt;em&gt;n&lt;/em&gt; bytes of another value. You figure instead of &amp;#8220;peeking and seeking&amp;#8221; against the stream (what it&amp;#8217;s read-only!?!?), your parser can just stored the state, and still only look at a single byte at a time. That&amp;#8217;s great and all, and you do a quick implementation using stream.ReadByte, which seems to work&amp;#8230; &lt;br/&gt;&lt;br/&gt;Except it&amp;#8217;s slow. You know from experience that block reads are way faster, and you want to read a block of data that&amp;#8217;s say 1k or 4k from your stream, and then parse that, fetch another block, parse that, etc&amp;#8230; but what if your pattern straddles two blocks? What if the first byte of a two byte sequence is the last byte in a block and the next block&amp;#8217;s first byte is the second character? Now your parser needs to stop what it&amp;#8217;s doing, exit the loop, go grab some more data, then restart it&amp;#8217;s iteration over that.. You could build all that behaviour into your parser (for every parser that you write).. but it&amp;#8217;s non-trival to deal with. In fact it&amp;#8217;s a real pain in the butt to refactor a parser to work that way. &lt;br/&gt;&lt;br/&gt;Also, you think to yourself &amp;#8220;Man&amp;#8230; It would be SOOOOooooo much nicer if I could just write a foreach loop, and like get every byte in the stream in one bit long iteration&amp;#8230; Why doesn&amp;#8217;t System.IO.Stream implement IEnumerable?!?&amp;#8221; It totally makes sense that it should&amp;#8230;&lt;br/&gt;&lt;br/&gt;Anyhow, story&amp;#8217;s over. Here&amp;#8217;s the code to solve it:&lt;br/&gt;&lt;br/&gt;&lt;/p&gt;
&lt;pre class="brush:csharp"&gt;&lt;br/&gt;public static IEnumerable&amp;lt;byte&amp;gt; GetBytesFromStream(Stream stream)&lt;br/&gt;{&lt;br/&gt;    const int blockSize = 1024;&lt;br/&gt;&lt;br/&gt;    byte[] buffer = new byte[blockSize];&lt;br/&gt;    int bytesRead;&lt;br/&gt;&lt;br/&gt;    while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) &amp;gt; 0)&lt;br/&gt;    {&lt;br/&gt;        for (int i = 0; i &amp;lt; bytesRead; i++)&lt;br/&gt;        {&lt;br/&gt;            yield return buffer[i];&lt;br/&gt;        }&lt;br/&gt;    }&lt;br/&gt;}&lt;br/&gt;&lt;/pre&gt;
&lt;p&gt;&lt;br/&gt;&lt;br/&gt;And in case it&amp;#8217;s not obvious, I&amp;#8217;ll explain what this little guy does. It does a block read from the stream (adjust your blocksize to suit or make it a parameter), iterates over the block, uses the &lt;strong&gt;yield&lt;/strong&gt; keyword to return bytes via the IEnumerable&amp;lt;T&amp;gt; interface. The &lt;strong&gt;while&lt;/strong&gt; loop checks the return value of stream.Read() to see if it returns zero, which means, basically, the stream is done (EOF). If there was a partial read (e.g. less than your blocksize buffer) bytesRead will be the amount that DID successfully read, and so your &lt;strong&gt;for&lt;/strong&gt; loop that is iterating over the block uses bytesRead to ensure we only return valid data (if we had used buffer.Length or blockSize, and had a partial read, the stuff after the &amp;#8220;new data&amp;#8221; would be data from the last read. NOT COOL!).&lt;br/&gt;&lt;br/&gt;You could stick this method in your utility class if you&amp;#8217;d like, or make a wrapper class that wraps Stream and implements IEnumerable&amp;lt;byte&amp;gt;&amp;#8230; whatever you want. Maybe you want to be all modern and cool and make it an extension method for Stream. &lt;br/&gt;&lt;br/&gt;Here&amp;#8217;s an example wrapper class:&lt;br/&gt;&lt;br/&gt;&lt;/p&gt;
&lt;pre class="brush:csharp"&gt;&lt;br/&gt;public class EnumerableStream : Stream, IEnumerable&amp;lt;byte&amp;gt;&lt;br/&gt;{&lt;br/&gt;    private readonly Stream _baseStream;&lt;br/&gt;&lt;br/&gt;    public EnumerableStream(Stream stream)&lt;br/&gt;    {&lt;br/&gt;        _baseStream = stream;&lt;br/&gt;    }&lt;br/&gt;&lt;br/&gt;    public IEnumerator&amp;lt;byte&amp;gt; GetEnumerator()&lt;br/&gt;    {&lt;br/&gt;        var bytes = GetBytesFromStream(_baseStream);&lt;br/&gt;        return bytes.GetEnumerator();&lt;br/&gt;    }&lt;br/&gt;&lt;br/&gt;    IEnumerator IEnumerable.GetEnumerator()&lt;br/&gt;    {&lt;br/&gt;        return GetEnumerator();&lt;br/&gt;    }&lt;br/&gt;&lt;br/&gt;    private static IEnumerable&amp;lt;byte&amp;gt; GetBytesFromStream(Stream stream)&lt;br/&gt;    {&lt;br/&gt;        const int blockSize = 1024;&lt;br/&gt;&lt;br/&gt;        byte[] buffer = new byte[blockSize];&lt;br/&gt;        int bytesRead;&lt;br/&gt;&lt;br/&gt;        while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) &amp;gt; 0)&lt;br/&gt;        {&lt;br/&gt;            for (int i = 0; i &amp;lt; bytesRead; i++)&lt;br/&gt;            {&lt;br/&gt;                yield return buffer[i];&lt;br/&gt;            }&lt;br/&gt;        }&lt;br/&gt;    }&lt;br/&gt;&lt;br/&gt;    public override bool CanRead&lt;br/&gt;    {&lt;br/&gt;        get { return _baseStream.CanRead; }&lt;br/&gt;    }&lt;br/&gt;&lt;br/&gt;    public override bool CanSeek&lt;br/&gt;    {&lt;br/&gt;        get { return _baseStream.CanSeek; }&lt;br/&gt;    }&lt;br/&gt;&lt;br/&gt;    public override bool CanWrite&lt;br/&gt;    {&lt;br/&gt;        get { return _baseStream.CanWrite; }&lt;br/&gt;    }&lt;br/&gt;&lt;br/&gt;    public override void Flush()&lt;br/&gt;    {&lt;br/&gt;        _baseStream.Flush();&lt;br/&gt;    }&lt;br/&gt;&lt;br/&gt;    public override long Length&lt;br/&gt;    {&lt;br/&gt;        get { return _baseStream.Length; }&lt;br/&gt;    }&lt;br/&gt;&lt;br/&gt;    public override long Position&lt;br/&gt;    {&lt;br/&gt;        get&lt;br/&gt;        {&lt;br/&gt;            return _baseStream.Position;&lt;br/&gt;        }&lt;br/&gt;        set&lt;br/&gt;        {&lt;br/&gt;            _baseStream.Position = value;&lt;br/&gt;        }&lt;br/&gt;    }&lt;br/&gt;&lt;br/&gt;    public override int Read(byte[] buffer, int offset, int count)&lt;br/&gt;    {&lt;br/&gt;        return _baseStream.Read(buffer, offset, count);&lt;br/&gt;    }&lt;br/&gt;&lt;br/&gt;    public override long Seek(long offset, SeekOrigin origin)&lt;br/&gt;    {&lt;br/&gt;        return _baseStream.Seek(offset, origin);&lt;br/&gt;    }&lt;br/&gt;&lt;br/&gt;    public override void SetLength(long value)&lt;br/&gt;    {&lt;br/&gt;        _baseStream.SetLength(value);&lt;br/&gt;    }&lt;br/&gt;&lt;br/&gt;    public override void Write(byte[] buffer, int offset, int count)&lt;br/&gt;    {&lt;br/&gt;        _baseStream.Write(buffer, offset, count);&lt;br/&gt;    }&lt;br/&gt;}&lt;br/&gt;&lt;/pre&gt;
&lt;p&gt;&lt;br/&gt;&lt;br/&gt;And an example of the extension method way&amp;#8230; &lt;br/&gt;&lt;br/&gt;&lt;/p&gt;
&lt;pre class="brush:csharp"&gt;&lt;br/&gt;public static class StreamExtensions&lt;br/&gt;{&lt;br/&gt;    public static IEnumerable&amp;lt;byte&amp;gt; GetBytes(this Stream stream)&lt;br/&gt;    {&lt;br/&gt;        const int blockSize = 1024;&lt;br/&gt;&lt;br/&gt;        byte[] buffer = new byte[blockSize];&lt;br/&gt;        int bytesRead;&lt;br/&gt;&lt;br/&gt;        while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) &amp;gt; 0)&lt;br/&gt;        {&lt;br/&gt;            for (int i = 0; i &amp;lt; bytesRead; i++)&lt;br/&gt;            {&lt;br/&gt;                yield return buffer[i];&lt;br/&gt;            }&lt;br/&gt;        }&lt;br/&gt;    }&lt;br/&gt;}&lt;br/&gt;&lt;/pre&gt;
&lt;p&gt;&lt;br/&gt;&lt;br/&gt;Enjoy!&lt;img src="https://blogger.googleusercontent.com/tracker/7907591936376660385-2813907589416068744?l=vanguard-against-confusion.blogspot.com" height="1" width="1"/&gt;&lt;/p&gt;</description><link>http://vanguard-against-confusion.tumblr.com/post/2516580054</link><guid>http://vanguard-against-confusion.tumblr.com/post/2516580054</guid><pubDate>Fri, 23 Oct 2009 01:27:00 -0700</pubDate><category>Stream</category><category>refactoring</category><category>string</category><category>byte</category><category>c</category><category>a better way</category><category>code snippet</category><category>.NET</category><category>IEnumerable</category><dc:creator>thoward37</dc:creator></item><item><title>Unicode string detection</title><description>&lt;p&gt;I had the need to detect wether or not a given string (in .Net/C#) was unicode or not.. Specifically filenames. I had a situation where a filename might be passed to me, that could possibly contian unicode. If it DID contained unicode characters, I needed to run GetShortPathName and get the 8.3 filename for the file, before passing it into a legacy component that couldn&amp;#8217;t handle unicode names&amp;#8230; &lt;br/&gt;&lt;br/&gt;Well, a &amp;#8220;big hammer approach&amp;#8221; might just call GetShortPathName on every filename, just to be sure&amp;#8230; But that&amp;#8217;s a costly API call if your having to do this a million times a second. &lt;br/&gt;&lt;br/&gt;So, long story short, I wrote this little function to detect unicode in a c# .Net string:&lt;br/&gt;&lt;br/&gt;&lt;/p&gt;
&lt;pre class="brush:csharp"&gt;&lt;br/&gt;        public static bool IsUnicode(string s)&lt;br/&gt;        {&lt;br/&gt;            return s != Marshal.PtrToStringAnsi(Marshal.StringToHGlobalAnsi(s));&lt;br/&gt;        }&lt;br/&gt;&lt;/pre&gt;
&lt;p&gt;&lt;br/&gt;&lt;br/&gt;Now homework for all you kiddies out there&amp;#8230; Is this code a memory leak? If so, what should you do to fix it? If not, why not?&lt;/p&gt;
&lt;p&gt;&lt;img width="1" height="1" src="https://blogger.googleusercontent.com/tracker/7907591936376660385-989399965465663002?l=vanguard-against-confusion.blogspot.com"/&gt;&lt;/p&gt;</description><link>http://vanguard-against-confusion.tumblr.com/post/2516579480</link><guid>http://vanguard-against-confusion.tumblr.com/post/2516579480</guid><pubDate>Thu, 23 Jul 2009 02:52:00 -0700</pubDate><category>globalization</category><category>hGlobal</category><category>leak</category><category>memory</category><category>string</category><category>c</category><category>detection</category><category>hackish</category><category>unicode</category><category>code snippet</category><category>Marshal</category><category>.NET</category><dc:creator>thoward37</dc:creator></item><item><title>Switch To..., Retry, Cancel</title><description>&lt;p&gt;Well, as much as I hate to admit it, I was recently working on  VB6 application that uses Office COM automation. It needed to have Word and Excel do a few things while the main application waited.&lt;br/&gt;&lt;br/&gt;Every time a user clicked on the UI, you&amp;#8217;d get that beautiful dialog &amp;#8220;This application cannot be completed because the other application is busy. Choose &amp;#8216;Switch To&amp;#8217; to activate the busy application and correct the problem.&lt;br/&gt;&lt;br/&gt;This is particularly onerous because if the user does not click switch to, basically, both programs, the VB6 app AND the Office app sit around waiting for the user to do something. Yay!&lt;br/&gt;&lt;br/&gt;Luckily I found the quick and dirty answer to this. Somewhere&amp;#8230; Anywhere before the point that you make the COM call, just insert these two lines:&lt;br/&gt;&lt;br/&gt;&lt;/p&gt;
&lt;pre class="brush:vb"&gt;&lt;br/&gt;App.OleRequestPendingTimeout = 2147483647&lt;br/&gt;App.OleServerBusyTimeout = 2147483647&lt;br/&gt;&lt;/pre&gt;

&lt;p&gt;&lt;br/&gt;&lt;br/&gt;&amp;#8230; and you never have to see that dialog again. &lt;br/&gt;&lt;br/&gt;Enjoy!&lt;/p&gt;
&lt;p&gt;&lt;img width="1" height="1" src="https://blogger.googleusercontent.com/tracker/7907591936376660385-5719123520127879251?l=vanguard-against-confusion.blogspot.com"/&gt;&lt;/p&gt;</description><link>http://vanguard-against-confusion.tumblr.com/post/2516578975</link><guid>http://vanguard-against-confusion.tumblr.com/post/2516578975</guid><pubDate>Tue, 06 Jan 2009 22:59:00 -0800</pubDate><category>VB6</category><category>Ole Automation Procedures</category><category>COM</category><dc:creator>thoward37</dc:creator></item><item><title>How to get information about your current culture.</title><description>&lt;p&gt;Instead of doing a college survery and asking a bunch of probing questions about the lives of twenty-somethings, there&amp;#8217;s an easier way to get information about your current culture. Just look at CultureInfo.CurrentCulture.&lt;br/&gt;&lt;br/&gt;Here&amp;#8217;s a quick program that explains how to do that. This can be very useful in debugging and troubleshooting how your program behaves on machines that are setup for other laungages or regions. &lt;br/&gt;&lt;br/&gt;&lt;/p&gt;
&lt;pre class="brush:csharp"&gt;&lt;br/&gt;using System;&lt;br/&gt;using System.Collections.Generic;&lt;br/&gt;using System.Text;&lt;br/&gt;using System.Globalization;&lt;br/&gt;&lt;br/&gt;namespace ConsoleApplication1&lt;br/&gt;{&lt;br/&gt;    class Program&lt;br/&gt;    {&lt;br/&gt;        static void Main(string[] args) &lt;br/&gt;        {&lt;br/&gt;            CultureInfo currentCulture = CultureInfo.CurrentCulture;&lt;br/&gt;&lt;br/&gt;            Console.WriteLine("CultureInfo");&lt;br/&gt;            Console.WriteLine("-----------");&lt;br/&gt;            Console.WriteLine("DisplayName: {0}", currentCulture.DisplayName);&lt;br/&gt;            Console.WriteLine("Name: {0}", currentCulture.Name);&lt;br/&gt;            Console.WriteLine("LCID: {0}", currentCulture.LCID);&lt;br/&gt;            Console.WriteLine();&lt;br/&gt;&lt;br/&gt;            Console.WriteLine("NumberFormatInfo");&lt;br/&gt;            Console.WriteLine("----------------");&lt;br/&gt;            Console.WriteLine("Decimal Seperator: {0}", currentCulture.NumberFormat.NumberDecimalSeparator);&lt;br/&gt;            Console.Write("Digits: ");&lt;br/&gt;&lt;br/&gt;            foreach (string s in currentCulture.NumberFormat.NativeDigits)&lt;br/&gt;            {&lt;br/&gt;                Console.Write(s + " ");&lt;br/&gt;            }&lt;br/&gt;&lt;br/&gt;            Console.WriteLine();&lt;br/&gt;        }&lt;br/&gt;    }&lt;br/&gt;} &lt;br/&gt;&lt;br/&gt;&lt;/pre&gt;
&lt;p&gt;&lt;br/&gt;&lt;br/&gt;&lt;br/&gt;Base output should look like:&lt;br/&gt;&lt;br/&gt;&lt;/p&gt;
&lt;pre class="brush:text"&gt;&lt;br/&gt;CultureInfo&lt;br/&gt;-----------&lt;br/&gt;DisplayName: English (United States)&lt;br/&gt;Name: en-US&lt;br/&gt;LCID: 1033&lt;br/&gt;&lt;br/&gt;&lt;br/&gt;&lt;br/&gt;NumberFormatInfo&lt;br/&gt;----------------&lt;br/&gt;Decimal Seperator: .&lt;br/&gt;Digits: 0 1 2 3 4 5 6 7 8 9&lt;br/&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src="https://blogger.googleusercontent.com/tracker/7907591936376660385-3971820214247531209?l=vanguard-against-confusion.blogspot.com" height="1" width="1"/&gt;&lt;/p&gt;</description><link>http://vanguard-against-confusion.tumblr.com/post/2516578311</link><guid>http://vanguard-against-confusion.tumblr.com/post/2516578311</guid><pubDate>Thu, 15 May 2008 17:40:00 -0700</pubDate><category>globalization</category><category>c</category><category>CultureInfo</category><category>CurrentCulture</category><category>.NET</category><dc:creator>thoward37</dc:creator></item><item><title>Refactoring a big if block into a simple command processor using attributes</title><description>&lt;p&gt;Recently someone had a problem where they had some massive control block full of if statements looking at a string, dispatching one of a variety of functions. The if block was massive. Hundreds of if statments, hundreds of magic strings. &lt;br/&gt;&lt;br/&gt;Interestingly all the functions had the same signature&amp;#8230; So I gave him this example of how to use attributes on the methods to specify the corresponding token, then we use Reflection to scan the assembly for all the functions with that attribute, then create a function table keyed by thier token, to privde fast lookup. This example shows how to creat an object instance and then invoke the method via reflection, but this could be made much simpler if the methods were all static and the function protoype was part of an interface instead of just a unspoken convention. &lt;br/&gt;&lt;br/&gt;Here&amp;#8217;s the &amp;#8220;Before&amp;#8221; example from the original question&amp;#8230;&lt;br/&gt;&lt;br/&gt;&lt;/p&gt;
&lt;pre class="brush:csharp"&gt;&lt;br/&gt;string tag;&lt;br/&gt;string cmdLine;&lt;br/&gt;State state;&lt;br/&gt;string outData;&lt;br/&gt;&lt;br/&gt;...&lt;br/&gt;&lt;br/&gt;if (token == "ABCSearch") {&lt;br/&gt;    ABC abc = new ABC();&lt;br/&gt;    abc.SearchFor(tag, state, cmdLine, ref outData);&lt;br/&gt;}&lt;br/&gt;else if (token == "JklmDoSomething") {&lt;br/&gt;    JKLM jklm = new JKLM();&lt;br/&gt;    jklm.Dowork1(tag, state, cmdLine, ref outData);&lt;br/&gt;}&lt;br/&gt;&lt;/pre&gt;
&lt;p&gt;&lt;br/&gt;&lt;em&gt;&lt;br/&gt;A couple of notes:&lt;br/&gt;&lt;/em&gt;&lt;/p&gt;
&lt;ul&gt;&lt;em&gt;&lt;br/&gt;&lt;li&gt;There is no correlation between the token and the class name (ABC, JKLM, &amp;#8230;) or the method (SearchFor, Dowork1).&lt;/li&gt;
&lt;li&gt;The methods do have the same signature: &lt;br/&gt;void func(string tag, State state, string cmdLine, ref string outData)&lt;/li&gt;
&lt;li&gt;The if ()&amp;#8230; block is 500+ lines and growing&lt;/li&gt;
&lt;/em&gt; 
&lt;/ul&gt;&lt;p&gt;&lt;em&gt;&lt;br/&gt;&lt;/em&gt;&lt;br/&gt;&lt;br/&gt;And here is my example command processor (as a console app):&lt;br/&gt;&lt;br/&gt;&lt;/p&gt;
&lt;pre class="brush:csharp"&gt;&lt;br/&gt;using System;&lt;br/&gt;using System.Collections.Generic;&lt;br/&gt;using System.Reflection;&lt;br/&gt;&lt;br/&gt;namespace ConsoleApplication2&lt;br/&gt;{&lt;br/&gt;    public class Program&lt;br/&gt;    {&lt;br/&gt;        static void Main(string[] args)&lt;br/&gt;        {&lt;br/&gt;            while(true)&lt;br/&gt;            {&lt;br/&gt;                Console.Write("[e(x)ecute, (t)okens, (q)uit] -&amp;gt; ");&lt;br/&gt;                string s = Console.ReadKey().KeyChar.ToString().ToLower();&lt;br/&gt;                Console.WriteLine();&lt;br/&gt;&lt;br/&gt;                switch (s)&lt;br/&gt;                {&lt;br/&gt;                case "q":&lt;br/&gt;                    Console.WriteLine("Finished.");&lt;br/&gt;                    return;&lt;br/&gt;&lt;br/&gt;                case "t":&lt;br/&gt;                    Console.WriteLine("Known tokens:");&lt;br/&gt;                    foreach (string tokenName in CommandProcessor.GetTokens())&lt;br/&gt;                    {&lt;br/&gt;                        Console.WriteLine(tokenName);&lt;br/&gt;                    }&lt;br/&gt;                    break;&lt;br/&gt;&lt;br/&gt;                case "x":&lt;br/&gt;                    string token = string.Empty;&lt;br/&gt;                    string tag = string.Empty;&lt;br/&gt;                    string cmdLine = string.Empty;&lt;br/&gt;                    string state = string.Empty;&lt;br/&gt;&lt;br/&gt;                    Console.Write("token: ");&lt;br/&gt;                    token = Console.ReadLine();&lt;br/&gt;                    Console.Write("tag: ");&lt;br/&gt;                    tag = Console.ReadLine();&lt;br/&gt;                    Console.Write("cmdLine: ");&lt;br/&gt;                    cmdLine = Console.ReadLine();&lt;br/&gt;                    Console.Write("state: ");&lt;br/&gt;                    state = Console.ReadLine();&lt;br/&gt;&lt;br/&gt;                    try&lt;br/&gt;                    {&lt;br/&gt;                        string output = CommandProcessor.DoCommand(token, tag, cmdLine, State.GetStateFromString(state));&lt;br/&gt;                        Console.WriteLine("Output:");&lt;br/&gt;                        Console.WriteLine(output);&lt;br/&gt;                    }&lt;br/&gt;                    catch (TokenNotFoundException ex)&lt;br/&gt;                    {&lt;br/&gt;                        Console.WriteLine(ex.Message);&lt;br/&gt;                    }&lt;br/&gt;                    catch (Exception ex)&lt;br/&gt;                    {&lt;br/&gt;                        Console.WriteLine("Unknown error occured during execution. Exception was: " + ex.Message);&lt;br/&gt;                    }&lt;br/&gt;                    break;&lt;br/&gt;&lt;br/&gt;                default:&lt;br/&gt;                    Console.WriteLine("Unknown command: {0}", s);&lt;br/&gt;                    break;&lt;br/&gt;                }&lt;br/&gt;            }&lt;br/&gt;        }&lt;br/&gt;    }&lt;br/&gt;&lt;br/&gt;    public class CommandProcessor&lt;br/&gt;    {&lt;br/&gt;        // our dictionary of method calls.&lt;br/&gt;        internal static Dictionary availableFunctions = new Dictionary();&lt;br/&gt;&lt;br/&gt;        static CommandProcessor()&lt;br/&gt;        {&lt;br/&gt;            SetupMethodCallDictionary();&lt;br/&gt;        }&lt;br/&gt;&lt;br/&gt;        private static void SetupMethodCallDictionary()&lt;br/&gt;        {&lt;br/&gt;            // get the current assembly.&lt;br/&gt;            Assembly assembly = Assembly.GetExecutingAssembly();&lt;br/&gt;&lt;br/&gt;            // cycle through the types in the assembly&lt;br/&gt;            foreach (Type type in assembly.GetTypes())&lt;br/&gt;            {&lt;br/&gt;                // cycle through the methods on each type&lt;br/&gt;                foreach (MethodInfo method in type.GetMethods())&lt;br/&gt;                {&lt;br/&gt;                    // look for Token attributes on the methods.&lt;br/&gt;                    object[] tokens = method.GetCustomAttributes(typeof(TokenAttribute), true);&lt;br/&gt;&lt;br/&gt;                    if (tokens.Length &amp;gt; 0)&lt;br/&gt;                    {&lt;br/&gt;                        // cycle through the token attributes (allowing multiple attributes&lt;br/&gt;                        // leaves room for backwards compatibility if you change your tokens&lt;br/&gt;                        // or consolidate functionality of the methods. etc.&lt;br/&gt;                        foreach (TokenAttribute token in tokens)&lt;br/&gt;                        {&lt;br/&gt;                            // look for the token in the dictionary, if it's not there add it.. &lt;br/&gt;                            MethodInfo foundMethod = default(MethodInfo);&lt;br/&gt;                            if (availableFunctions.TryGetValue(token.TokenName, out foundMethod))&lt;br/&gt;                            {&lt;br/&gt;                                // if there is more than one function registered for the same&lt;br/&gt;                                // token, just keep the last one found.&lt;br/&gt;                                availableFunctions[token.TokenName] = method;&lt;br/&gt;                            }&lt;br/&gt;                            else&lt;br/&gt;                            {&lt;br/&gt;                                // add to the table. &lt;br/&gt;                                availableFunctions.Add(token.TokenName, method);&lt;br/&gt;                            }&lt;br/&gt;                        }&lt;br/&gt;                    }&lt;br/&gt;                }&lt;br/&gt;            }&lt;br/&gt;        }&lt;br/&gt;&lt;br/&gt;        public static string DoCommand(string token, string tag, string cmdLine, State state)&lt;br/&gt;        {&lt;br/&gt;            // the data returned from the command&lt;br/&gt;            string outData = string.Empty;&lt;br/&gt;            MethodInfo method = default(MethodInfo);&lt;br/&gt;&lt;br/&gt;            // see if we have a method for that token&lt;br/&gt;            if (availableFunctions.TryGetValue(token, out method))&lt;br/&gt;            {&lt;br/&gt;                // if so, create an instance of the object, and then execute the method,&lt;br/&gt;                // unless it's static.. in which case just execute the method. &lt;br/&gt;                object instance = null;&lt;br/&gt;                if (!method.IsStatic)&lt;br/&gt;                {&lt;br/&gt;                    // this just invokes the default constructor... if you need to pass &lt;br/&gt;                    // parameters use one of the other overloads.&lt;br/&gt;                    instance = Activator.CreateInstance(method.ReflectedType);&lt;br/&gt;                }&lt;br/&gt;&lt;br/&gt;                object[] args = new object[] { tag, state, cmdLine, outData };&lt;br/&gt;&lt;br/&gt;                method.Invoke(instance, args);&lt;br/&gt;                outData = (string)args[3];&lt;br/&gt;            }&lt;br/&gt;            else&lt;br/&gt;            {&lt;br/&gt;                throw new TokenNotFoundException(string.Format("Token {0} not found. Cannot execute.", token));&lt;br/&gt;            }&lt;br/&gt;            return outData;&lt;br/&gt;        }&lt;br/&gt;&lt;br/&gt;        public static IEnumerable GetTokens()&lt;br/&gt;        {&lt;br/&gt;            foreach (KeyValuePair entry in availableFunctions)&lt;br/&gt;            {&lt;br/&gt;                yield return entry.Key;&lt;br/&gt;            }&lt;br/&gt;        }&lt;br/&gt;    }&lt;br/&gt;&lt;br/&gt;    public class State&lt;br/&gt;    {&lt;br/&gt;        public State(string text)&lt;br/&gt;        {&lt;br/&gt;            _text = text;&lt;br/&gt;        }&lt;br/&gt;&lt;br/&gt;        private string _text;&lt;br/&gt;&lt;br/&gt;        public string Text&lt;br/&gt;        {&lt;br/&gt;            get { return _text; }&lt;br/&gt;            set { _text = value; }&lt;br/&gt;        }&lt;br/&gt;&lt;br/&gt;        public static State GetStateFromString(string state)&lt;br/&gt;        {&lt;br/&gt;            // implement parsing of string to build State object here. &lt;br/&gt;            return new State(state);&lt;br/&gt;        }&lt;br/&gt;    }&lt;br/&gt;&lt;br/&gt;    [AttributeUsage(AttributeTargets.Method)]&lt;br/&gt;    public class TokenAttribute : Attribute&lt;br/&gt;    {&lt;br/&gt;        public TokenAttribute(string tokenName)&lt;br/&gt;        {&lt;br/&gt;            _tokenName = tokenName;&lt;br/&gt;        }&lt;br/&gt;&lt;br/&gt;        private string _tokenName;&lt;br/&gt;&lt;br/&gt;        public string TokenName&lt;br/&gt;        {&lt;br/&gt;            get { return _tokenName; }&lt;br/&gt;            set { _tokenName = value; }&lt;br/&gt;        }&lt;br/&gt;    }&lt;br/&gt;&lt;br/&gt;    [global::System.Serializable]&lt;br/&gt;    public class TokenNotFoundException : Exception&lt;br/&gt;    {&lt;br/&gt;        //&lt;br/&gt;        // For guidelines regarding the creation of new exception types, see&lt;br/&gt;        // &lt;a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconerrorraisinghandlingguidelines.asp"&gt;http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconerrorraisinghandlingguidelines.asp&lt;/a&gt;&lt;br/&gt;        // and&lt;br/&gt;        // &lt;a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/csharp07192001.asp"&gt;http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/csharp07192001.asp&lt;/a&gt;&lt;br/&gt;        //&lt;br/&gt;        public TokenNotFoundException() { }&lt;br/&gt;        public TokenNotFoundException(string message) : base(message) { }&lt;br/&gt;        public TokenNotFoundException(string message, Exception inner) : base(message, inner) { }&lt;br/&gt;        protected TokenNotFoundException(&lt;br/&gt;        System.Runtime.Serialization.SerializationInfo info,&lt;br/&gt;        System.Runtime.Serialization.StreamingContext context)&lt;br/&gt;        : base(info, context) { }&lt;br/&gt;    }&lt;br/&gt;&lt;br/&gt;    public class ABC&lt;br/&gt;    {&lt;br/&gt;        [Token("ABCSearch")]&lt;br/&gt;        public void SearchFor(string tag, State state, string cmdLine, ref string outData)&lt;br/&gt;        {&lt;br/&gt;            // do some stuff.&lt;br/&gt;            outData =&lt;br/&gt;            string.Format("You called ABC.SearchFor. Parameters were [tag: {0}, state: {1}, cmdLine: {2}]", tag, state.Text, cmdLine);&lt;br/&gt;&lt;br/&gt;       }&lt;br/&gt;    }&lt;br/&gt;&lt;br/&gt;    public class JKLM &lt;br/&gt;    {&lt;br/&gt;        [Token("JklmDoSomething")]&lt;br/&gt;        public void Dowork1(string tag, State state, string cmdLine, ref string outData)&lt;br/&gt;        {&lt;br/&gt;            // do some other stuff. &lt;br/&gt;            outData =&lt;br/&gt;            string.Format("You called JKLM.Dowork1. Parameters were [tag: {0}, state: {1}, cmdLine: {2}]", tag, state.Text, cmdLine);&lt;br/&gt;        }&lt;br/&gt;    }&lt;br/&gt;}&lt;br/&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img width="1" height="1" src="https://blogger.googleusercontent.com/tracker/7907591936376660385-8283090266498421816?l=vanguard-against-confusion.blogspot.com"/&gt;&lt;/p&gt;</description><link>http://vanguard-against-confusion.tumblr.com/post/2516578687</link><guid>http://vanguard-against-confusion.tumblr.com/post/2516578687</guid><pubDate>Thu, 15 May 2008 16:54:00 -0700</pubDate><category>attributes</category><category>refactoring</category><category>c</category><category>command processor</category><category>.NET</category><category>reflection</category><dc:creator>thoward37</dc:creator></item><item><title>Filtering a network stream using a wrapper</title><description>&lt;p&gt;So not that long ago, someone posted a question asking how to deal with a certain situation. The situation is such that there is a network file stream coming from somewhere, that has certain data you want to keep, and certain data you don&amp;#8217;t want to keep. Control blocks, extra header information, weirdo protocol, too much data coming back form an API, etc..&lt;br/&gt;&lt;br/&gt;My suggestion was to create a simple container object (aka wrapper) to the existing network stream, that operates the same as the network stream, but does the necessary filtering.&lt;br/&gt;&lt;br/&gt;Here&amp;#8217;s an example of how you&amp;#8217;d use it, and and example base class implementation of for the filters follows it. In the actual problem case example, he was dealing with a NetworkStream that contained Xml data in irregular chunks, with control blocks as fixed headers. Each header indicates how much XmlData follows. The filter will remove the headers as needed, presenting a simple stream of Xml data to the XmlReader to parse.&lt;br/&gt;&lt;br/&gt;I&amp;#8217;ve left out the concrete implementation that actually parses the stream, and here you just have the FilteredNetworkStream base class and an idea of how to use it once you implement it. All that&amp;#8217;s left for the implementer is to override the abstract method FilterBeforeRead, which contains the customized filtering logic for the particular situation.&lt;br/&gt;&lt;br/&gt;&lt;br/&gt;&lt;/p&gt;
&lt;pre class="brush:csharp"&gt;&lt;br/&gt;using (NetworkStream inputStream = GetNetworkStreamFromSomewhere())&lt;br/&gt;using (StreamWriter outputStream = new StreamWriter(@"C:\Path\To\File.xml", false))&lt;br/&gt;{&lt;br/&gt;&lt;br/&gt;    XmlReader reader = XmlReader.Create(new FilteredNetworkStream(inputStream));&lt;br/&gt;    while (reader.Read())&lt;br/&gt;    {&lt;br/&gt;&lt;br/&gt;        // method returns empty string if current data is discardable&lt;br/&gt;        string outputData = GetDesiredDataFromReader(reader);&lt;br/&gt;&lt;br/&gt;        if (!string.IsNullOrEmpty(outputData))&lt;br/&gt;        {&lt;br/&gt;&lt;br/&gt;            // save desired data to local file&lt;br/&gt;            outputStream.Write(outputData);&lt;br/&gt;        }&lt;br/&gt;    }&lt;br/&gt;}&lt;br/&gt;&lt;/pre&gt;
&lt;p&gt;&lt;br/&gt;&lt;br/&gt;Here&amp;#8217;s the base class:&lt;br/&gt;&lt;br/&gt;&lt;/p&gt;
&lt;pre class="brush:csharp"&gt;&lt;br/&gt;public abstract class FilteredNetworkStream : Stream&lt;br/&gt;{&lt;br/&gt;    public FilteredNetworkStream(NetworkStream baseStream)&lt;br/&gt;    {&lt;br/&gt;        _baseStream = baseStream;&lt;br/&gt;    }&lt;br/&gt;&lt;br/&gt;    protected NetworkStream _baseStream;&lt;br/&gt;    public abstract void FilterBeforeRead();&lt;br/&gt;&lt;br/&gt;    #region Stream Implementation&lt;br/&gt;&lt;br/&gt;    public override bool CanRead&lt;br/&gt;    {&lt;br/&gt;        get { return _baseStream.CanRead; }&lt;br/&gt;    }&lt;br/&gt;&lt;br/&gt;    public override bool CanSeek&lt;br/&gt;    {&lt;br/&gt;        get { return _baseStream.CanSeek; }&lt;br/&gt;    }&lt;br/&gt;&lt;br/&gt;    public override bool CanWrite&lt;br/&gt;    {&lt;br/&gt;        get { return _baseStream.CanWrite; }&lt;br/&gt;    }&lt;br/&gt;&lt;br/&gt;    public override void Flush()&lt;br/&gt;    {&lt;br/&gt;        _baseStream.Flush();&lt;br/&gt;    }&lt;br/&gt;&lt;br/&gt;    public override long Length&lt;br/&gt;    {&lt;br/&gt;        get { return _baseStream.Length; }&lt;br/&gt;    }&lt;br/&gt;&lt;br/&gt;    public override long Position&lt;br/&gt;    {&lt;br/&gt;        get&lt;br/&gt;        {&lt;br/&gt;            return _baseStream.Position;&lt;br/&gt;        }&lt;br/&gt;        set&lt;br/&gt;        {&lt;br/&gt;            _baseStream.Position = value;&lt;br/&gt;        }&lt;br/&gt;    }&lt;br/&gt;&lt;br/&gt;    public override int Read(byte[] buffer, int offset, int count)&lt;br/&gt;    {&lt;br/&gt;        this.FilterBeforeRead();&lt;br/&gt;        return _baseStream.Read(buffer, offset, count);&lt;br/&gt;    }&lt;br/&gt;&lt;br/&gt;    public override long Seek(long offset, SeekOrigin origin)&lt;br/&gt;    {&lt;br/&gt;        return _baseStream.Seek(offset, origin);&lt;br/&gt;    }&lt;br/&gt;&lt;br/&gt;    public override void SetLength(long value)&lt;br/&gt;    {&lt;br/&gt;        _baseStream.SetLength(value);&lt;br/&gt;    }&lt;br/&gt;&lt;br/&gt;    public override void Write(byte[] buffer, int offset, int count)&lt;br/&gt;    {&lt;br/&gt;        _baseStream.Write(buffer, offset, count);&lt;br/&gt;    }&lt;br/&gt;&lt;br/&gt;    #endregion &lt;br/&gt;}&lt;br/&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img width="1" height="1" src="https://blogger.googleusercontent.com/tracker/7907591936376660385-5178336965474929773?l=vanguard-against-confusion.blogspot.com"/&gt;&lt;/p&gt;</description><link>http://vanguard-against-confusion.tumblr.com/post/2516577813</link><guid>http://vanguard-against-confusion.tumblr.com/post/2516577813</guid><pubDate>Thu, 15 May 2008 16:18:00 -0700</pubDate><category>Stream</category><category>abstract</category><category>c</category><category>container</category><category>NetworkStream</category><category>pattern</category><category>.NET</category><category>Filter</category><category>wrapper</category><dc:creator>thoward37</dc:creator></item><item><title>Getting the field list returned from an ad-hoc Sql query</title><description>&lt;p&gt;So, recently I needed to make an application that allowed a user to enter an arbitrary SQL query, and elsewhere in the UI I needed to display a drop-down with the fields that this arbitrary query returned.&lt;/p&gt;
&lt;p&gt;This poses a small problem. It&amp;#8217;s very simple if the user is doing simple queries, that don&amp;#8217;t take long to execute. You could just run the query, then, take the first result, and get the list of fields. Well.. This works for simple queries that return small result sets, but we needed to put in queries that potentially return as many as 48 million results, using complex queries including joins between multi-million rowed tables, aggregates, and that sort of thing..&lt;/p&gt;
&lt;p&gt;In other words, the queries are slow. Really slow. They create a lot of UI lag when I go to get the field names for the drop down box.&lt;/p&gt;
&lt;p&gt;My first attempt was to take the query and wrap it up like this:&lt;/p&gt;
&lt;pre class="brush:sql"&gt;SELECT TOP(1) * FROM ( &lt;em&gt;// original query here //&lt;/em&gt; ) fieldNamesTable&lt;/pre&gt;
&lt;p&gt;My thinking was that if I specified that I only wanted the first record it&amp;#8217;s be really quick, even with a complex query. This is true. It&amp;#8217;s must faster, but it&amp;#8217;s still slow. Too slow. A lot of UI lag still remained.&lt;/p&gt;
&lt;p&gt;So, my second attempt worked much better. I wrapped the query again, but now it looks like:&lt;/p&gt;
&lt;pre class="brush:sql"&gt;SELECT * FROM ( &lt;em&gt;// original query here //&lt;/em&gt; ) fieldNamesTable WHERE 1 = 0&lt;/pre&gt;
&lt;p&gt;Instead of specifying I wanted the first record, I put a phrase in the &lt;strong&gt;WHERE&lt;/strong&gt; clause that will always be false. The SQL Server&amp;#8217;s query execution engine realizes that, and so it knows that the query will never be able to return data. So it immediately returns with 0 results. But I get the field names!! This is SUPER fast!&lt;/p&gt;
&lt;p&gt;Enjoy, &lt;br/&gt;Troy&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blogger.googleusercontent.com/tracker/7907591936376660385-3417186343500805364?l=vanguard-against-confusion.blogspot.com" height="1" width="1"/&gt;&lt;/p&gt;</description><link>http://vanguard-against-confusion.tumblr.com/post/2516577543</link><guid>http://vanguard-against-confusion.tumblr.com/post/2516577543</guid><pubDate>Tue, 29 Apr 2008 18:05:00 -0700</pubDate><category>Microsoft Sql Server</category><category>MSSQL</category><category>SQL</category><dc:creator>thoward37</dc:creator></item><item><title>An amusing bought with the DataGridView control</title><description>&lt;p&gt;As usual, WinForms GUI programming is a terrible PIA. Even worse is the flagship of all controls the great beast known as the DataGridView. Working with the DataGridView bends your mind like offensive cutlery at Uri Geller&amp;#8217;s dinner table.&lt;/p&gt;
&lt;p&gt;During my most recent encounter with this control of the third kind, I needed to use a ComboBox column, and that column needed to have an effect on the contents of the other controls. Normally, you could just hook up a cellvaluechanged event or something of that nature, but that doesn&amp;#8217;t work out on a ComboBox control in a DataGridView column&amp;#8230; There&amp;#8217;s no event that would fire when a user selected an item in the dropdown. Only after the user selected the item, then refocused on some other control.&lt;/p&gt;
&lt;p&gt;That was annoying. Too many clicks for the user! When I select the item in the dropdown, the row should react, I shouldn&amp;#8217;t have to click elsewhere.&lt;/p&gt;
&lt;p&gt;So here&amp;#8217;s a quick example of what I did to get that working. In the example, we handle the datagridview&amp;#8217;s &lt;strong&gt;EditControlShowing&lt;/strong&gt; event, then grab a reference to the combobox, then unwire any previous events we may have hooked up to &lt;strong&gt;SelectionChangeCommitted&lt;/strong&gt;, then wire the event. In the &lt;strong&gt;SelectionChangeCommitted&lt;/strong&gt; event we call &lt;strong&gt;_dataGridView.EndEdit()&lt;/strong&gt; to effect the other rows.&lt;/p&gt;
&lt;p&gt;Enjoy!&lt;/p&gt;
&lt;pre class="brush:csharp"&gt;&lt;br/&gt;public class Example&lt;br/&gt;{&lt;br/&gt; /// &lt;br/&gt; /// Constructor&lt;br/&gt; /// &lt;br/&gt; public Example()&lt;br/&gt; {&lt;br/&gt; _dataGridView = new DataGridView();&lt;br/&gt;&lt;br/&gt; // setup the datagridview here.&lt;br/&gt; DataGridViewComboBoxColumn fooColumn = new DataGridViewComboBoxColumn();&lt;br/&gt; fooColumn.Name = "Foo";&lt;br/&gt; fooColumn.ValueType = typeof(String);&lt;br/&gt; fooColumn.HeaderText = "Foo";&lt;br/&gt; fooColumn.Items.Add("Bar");&lt;br/&gt; fooColumn.Items.Add("Baz");&lt;br/&gt; fooColumn.Items.Add("Fizz");&lt;br/&gt; fooColumn.Items.Add("Buzz");&lt;br/&gt; fooColumn.Items.Add("FizzBuzz");&lt;br/&gt; fooColumn.DefaultCellStyle.NullValue = "Bar";&lt;br/&gt;&lt;br/&gt; _dataGridView.Columns.Add(fooColumn);&lt;br/&gt;&lt;br/&gt; // hook up editing control showing event&lt;br/&gt; _dataGridView.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(_dataGridView_EditingControlShowing);&lt;br/&gt;&lt;br/&gt; // create a delegate for the method that will handle the event &lt;br/&gt; _comboBoxSelectDelegate = new EventHandler(combo_SelectionChangeCommitted);&lt;br/&gt; }&lt;br/&gt;&lt;br/&gt; private DataGridView _dataGridView;&lt;br/&gt; private EventHandler _comboBoxSelectDelegate;&lt;br/&gt;&lt;br/&gt; void _dataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)&lt;br/&gt; {&lt;br/&gt; // get the control from the event args.&lt;br/&gt;&lt;br/&gt; ComboBox combo = e.Control as ComboBox;           &lt;br/&gt;&lt;br/&gt;&lt;br/&gt; if (combo != null)&lt;br/&gt; {&lt;br/&gt; // remove the event subscription if it exists.&lt;br/&gt; combo.SelectionChangeCommitted -= comboSelectDelegate;                &lt;br/&gt;&lt;br/&gt; // add a subscription to the event&lt;br/&gt; combo.SelectionChangeCommitted += comboSelectDelegate;                &lt;br/&gt; }&lt;br/&gt; }&lt;br/&gt;&lt;br/&gt; void  combo_SelectionChangeCommitted(object sender, EventArgs e)&lt;br/&gt; {&lt;br/&gt; // handle the event, and end edit mode&lt;br/&gt; _dataGridView.EndEdit();&lt;br/&gt; }&lt;br/&gt;}&lt;br/&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img width="1" height="1" src="https://blogger.googleusercontent.com/tracker/7907591936376660385-4349570558317487858?l=vanguard-against-confusion.blogspot.com"/&gt;&lt;/p&gt;</description><link>http://vanguard-against-confusion.tumblr.com/post/2516577245</link><guid>http://vanguard-against-confusion.tumblr.com/post/2516577245</guid><pubDate>Mon, 24 Mar 2008 17:36:00 -0700</pubDate><category>combo</category><category>SelectionChangeCommitted</category><category>events</category><category>c</category><category>DataGridView</category><category>box</category><category>DataGridViewComboBoxColumn</category><category>combobox</category><category>.NET</category><category>EditingControlShowing</category><dc:creator>thoward37</dc:creator></item><item><title>Black Box OMG it's addictive.</title><description>&lt;p&gt;BlackBox - A simple puzzle game where you shoot rays of light into a black box, and determine the location of atoms inside the box based on the entry and exit points of the ray. &lt;br/&gt;&lt;br/&gt;Seriously addicting.&lt;br/&gt;&lt;br/&gt;Here is a &lt;a href="http://en.wikipedia.org/wiki/Black_Box_%28game%29"&gt;Wiki article about it&lt;/a&gt; and an online-playable &lt;a href="http://home.flash.net/~gpb/bbox/bbox.html"&gt;Flash version&lt;/a&gt;.&lt;/p&gt;
&lt;img src="https://blogger.googleusercontent.com/tracker/7907591936376660385-3421959773665591509?l=vanguard-against-confusion.blogspot.com" height="1" width="1"/&gt;</description><link>http://vanguard-against-confusion.tumblr.com/post/2516576836</link><guid>http://vanguard-against-confusion.tumblr.com/post/2516576836</guid><pubDate>Sat, 08 Mar 2008 00:30:00 -0800</pubDate><category>game</category><category>logic</category><category>blackbox</category><category>black</category><category>addictive</category><category>box</category><category>puzzle</category><dc:creator>thoward37</dc:creator></item><item><title>Sharing Menu Items between ToolStrips on a Windows Form</title><description>&lt;p&gt;So, you may have found yourself building a nice user-friendly, somewhat complicated Windows Forms application, that had lots of drop-down menus and right click context menus, and what not.. You may have naively assumed that you could *share* your menu items, so that you have a consistent set of options, icons, and more importantly event handlers for a particular menu item or set of menu items.&lt;br/&gt;&lt;br/&gt;&lt;br/&gt;Well, I did&amp;#8230; I had a Tools drop-down menu with some basic functions that I wanted to also be accessible from a right-click content menu on a treeview. Redundant? Sure&amp;#8230; Convenient? Definately.&lt;br/&gt;&lt;br/&gt;There&amp;#8217;s a little annoying detail that says that a ToolStripMenuItem can&amp;#8217;t be &amp;#8220;owned&amp;#8221; by more than one ToolStrip. In other words, it can only be in one place at a time. So, when you do something like this:&lt;br/&gt;&lt;br/&gt;toolsToolStripMenuItem.DropDownItems.Add(myMenuItem);&lt;br/&gt;treeNodeContextToolStrip.Items.Add(myMenuItem);&lt;br/&gt;&lt;br/&gt;The menu item in question suddenly disappears from the tools menu, and appears in the content menu&amp;#8230; Hmm&amp;#8230;&lt;br/&gt;&lt;br/&gt;So, in order to share the menu item, I came up with this hackish solution&amp;#8230;.I handled the Opening event for the two menu strips and in each one, &amp;#8220;took ownership&amp;#8221; of the menuitem. So, through sleight-of-hand it seems to exist in one place at a time. We can only get away with this because, ToolStrips, being modal, only show one at a time.&lt;br/&gt;&lt;br/&gt;So, here&amp;#8217;s a simple sample:&lt;br/&gt;&lt;br/&gt;&lt;strong&gt;Hot-Swap Menu Item Sample&lt;/strong&gt;&lt;/p&gt;
&lt;pre class="brush:csharp"&gt;&lt;br/&gt;private void treeNodeContextMenuStrip_Opening(object sender, CancelEventArgs e)&lt;br/&gt;{&lt;br/&gt;    treeNodeContextMenuStrip.Items.Insert(3, myToolStripMenuItem);&lt;br/&gt;}&lt;br/&gt;&lt;br/&gt;private void toolsToolStripMenuItem_DropDownOpening(object sender, EventArgs e)&lt;br/&gt;{&lt;br/&gt;    this.toolsToolStripMenuItem.DropDownItems.Insert(5, this.myToolStripMenuItem);&lt;br/&gt;}&lt;br/&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img width="1" height="1" src="https://blogger.googleusercontent.com/tracker/7907591936376660385-6119549536072900174?l=vanguard-against-confusion.blogspot.com"/&gt;&lt;/p&gt;</description><link>http://vanguard-against-confusion.tumblr.com/post/2516576574</link><guid>http://vanguard-against-confusion.tumblr.com/post/2516576574</guid><pubDate>Wed, 13 Feb 2008 18:52:00 -0800</pubDate><category>ContentMenuStrip</category><category>hackish</category><category>Windows</category><category>Forms</category><category>ToolStrip</category><category>ToolStripMenuItem</category><category>.NET</category><dc:creator>thoward37</dc:creator></item><item><title>ASP.NET Gridview SelectIndexChanged event not firing</title><description>&lt;p&gt;If, you find yourself in the unfortunate position of having a dynamically created Gridview Control in your ASP.NET page AND needing to handle the SelectedIndexChanged event&amp;#8230; as I was, you may find yourself banging your head against the monitor trying to understand why the event isn&amp;#8217;t getting fired. &lt;br/&gt;&lt;br/&gt;&lt;br/&gt;Well, dear reader, that&amp;#8217;s because you didn&amp;#8217;t set the Gridview.ID property, and when the  callback for the event is fired, it does that by ID/parameters.. The parameters are there, but the ID isn&amp;#8217;t.. So it can&amp;#8217;t find your control, so it can&amp;#8217;t fire the event. &lt;br/&gt;&lt;br/&gt;So, always assign the ID of dynamically generated ASP.NET controls!&lt;/p&gt;
&lt;img src="https://blogger.googleusercontent.com/tracker/7907591936376660385-8230588255100681186?l=vanguard-against-confusion.blogspot.com" height="1" width="1"/&gt;</description><link>http://vanguard-against-confusion.tumblr.com/post/2516576302</link><guid>http://vanguard-against-confusion.tumblr.com/post/2516576302</guid><pubDate>Thu, 15 Nov 2007 15:50:00 -0800</pubDate><category>Gridview</category><category>events</category><category>SelectedIndexChanged</category><category>ASP.NET</category><category>Control</category><dc:creator>thoward37</dc:creator></item><item><title>Changing Visual Studio Item Templates</title><description>&lt;p&gt;So, Visual Studio is pretty great right? It makes a lot of things really easy, really automated&amp;#8230; saves a lot of typing, etc&amp;#8230; However, there are still some areas where I find myself repetatively doing the same thing in certain scenarios.&lt;br/&gt;&lt;br/&gt;For example &amp;#8212; Making a new class.&lt;br/&gt;&lt;br/&gt;Now, I usually just right-click on my project list, Add&amp;#8230;, New Item, then I&amp;#8217;m presented with an array of the available &lt;em&gt;Item Templates&lt;/em&gt;. So, I&amp;#8217;ll select Class, rename it, and then I&amp;#8217;m presented with this:&lt;br/&gt;&lt;br/&gt;&lt;/p&gt;
&lt;pre class="brush:csharp"&gt;&lt;br/&gt;using System;&lt;br/&gt;using System.Collections.Generic;&lt;br/&gt;using System.Text;&lt;br/&gt;&lt;br/&gt;namespace MyNamespace&lt;br/&gt;{&lt;br/&gt; class Class1&lt;br/&gt; {&lt;br/&gt; }&lt;br/&gt;}&lt;/pre&gt;
&lt;p&gt;&lt;br/&gt;And of course, the first thing I will do, is change the class name, set it to public, create an empty constructor, define code regions to keep things organized&amp;#8230; and end up with something like this:&lt;br/&gt;&lt;br/&gt;&lt;/p&gt;
&lt;pre class="brush:csharp"&gt;&lt;br/&gt;using System;&lt;br/&gt;using System.Collections.Generic;&lt;br/&gt;using System.Text;&lt;br/&gt;&lt;br/&gt;namespace MyNamespace&lt;br/&gt;{&lt;br/&gt; public class Class1&lt;br/&gt; {&lt;br/&gt;&lt;br/&gt; #region Constructors&lt;br/&gt; public Class1()&lt;br/&gt; {&lt;br/&gt; Init();&lt;br/&gt; }&lt;br/&gt;&lt;br/&gt; /// &lt;br/&gt; /// Initializes field values.&lt;br/&gt; /// &lt;br/&gt; private void Init()&lt;br/&gt; {&lt;br/&gt; }&lt;br/&gt; #endregion Constructors&lt;br/&gt;&lt;br/&gt; #region Fields&lt;br/&gt; #endregion Fields&lt;br/&gt;&lt;br/&gt; #region Properties&lt;br/&gt; #endregion Properties&lt;br/&gt;&lt;br/&gt; #region Public Methods&lt;br/&gt; #endregion Public Methods&lt;br/&gt;&lt;br/&gt; #region Private Methods&lt;br/&gt; #endregion Private Methods&lt;br/&gt; }&lt;br/&gt;}&lt;/pre&gt;
&lt;p&gt;&lt;br/&gt;WHOA! That takes quite a few minutes of typing&amp;#8230; FOR EVERY CLASS I WRITE!!&lt;/p&gt;
&lt;p&gt;So, I decided to go figure out how those templates work.&lt;br/&gt;&lt;br/&gt;&lt;strong&gt;Where are those files at?&lt;/strong&gt;&lt;br/&gt;&lt;br/&gt;The templates are stored in your Visual Studio installation directory. If you&amp;#8217;re like me, and running a fairly recent version of Visual Studio 2005, installed with default configuration, your install directory will probably be:&lt;/p&gt;
&lt;pre class="brush:plain"&gt;C:\Program Files\Microsoft Visual Studio 8&lt;/pre&gt;
&lt;p&gt;and the templates are stored in:&lt;/p&gt;
&lt;pre class="brush:plain"&gt;C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\ItemTemplates&lt;/pre&gt;
&lt;p&gt;Under that directory you&amp;#8217;ll find subdirectories for each classification of template (CSharp, JSharp, VisualBasic, etc.. ), and in each of those subdirectories, you&amp;#8217;ll find a zip file for each template.&lt;/p&gt;
&lt;p&gt;For what I want to fix, I am looking for this file:&lt;/p&gt;
&lt;pre class="brush:plain"&gt;C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\ItemTemplates\CSharp\1033\Class.zip&lt;br/&gt;&lt;/pre&gt;
&lt;p&gt;&lt;span&gt;&lt;em&gt;*sidenote: Notice that the last subdirectory in the path is the language code 1033 (English) and if you&amp;#8217;ve installed windows/vs with a different language, this will be different.. this is one reason you may not see your default templates when working across languages (ie, windows is in spanish, and visual studio is installed from the English language version, but is configured to be in Spanish.. templates will be missing!!&lt;/em&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;br/&gt;In the Class.zip file, you will find two files; Class.cs and Class.vstemplate&lt;br/&gt;&lt;br/&gt;&lt;strong&gt;Class.vstemplate&lt;/strong&gt;&lt;br/&gt;Class.vstemplate is just an XML file. This contians information about the template, such as what GUID to lookup for the icon to display in the wizard screen, what the sorting order is, what assemblies it references, etc.. For the most part, unless your doing something more complicated than what I want to do, you won&amp;#8217;t need to edit this. One tag to pay attention to is:&lt;/p&gt;
&lt;pre class="brush:xml"&gt;&amp;lt;ProjectItem ReplaceParameters="true"&amp;gt;Class.cs&amp;lt;/ProjectItem&amp;gt;&lt;/pre&gt;
&lt;p&gt;This tag says that VS should make a new project item based on Class.cs, and it should parse the file and replace the parameter tokens in it with the appropriate values&amp;#8230; So let&amp;#8217;s look at Class.cs:&lt;/p&gt;
&lt;p&gt;&lt;br/&gt;&lt;strong&gt;Class.cs&lt;/strong&gt;&lt;br/&gt;Class.cs is the templated code file. The default file looks like this:&lt;/p&gt;
&lt;pre class="brush:csharp"&gt;&lt;br/&gt;using System;&lt;br/&gt;using System.Collections.Generic;&lt;br/&gt;using System.Text;&lt;br/&gt;&lt;br/&gt;namespace $rootnamespace$&lt;br/&gt;{&lt;br/&gt; class $safeitemrootname$&lt;br/&gt; {&lt;br/&gt; }&lt;br/&gt;}&lt;/pre&gt;
&lt;p&gt;&lt;br/&gt;So this is the normal new empty class, and the tokens &lt;em&gt;$rootnamespace&lt;/em&gt; and &lt;em&gt;$safeitemrootname$&lt;/em&gt; are what will get replaced when VS parses the file and passes in the parameters.&lt;/p&gt;
&lt;p&gt;&lt;br/&gt;Well, I don&amp;#8217;t know anything about those parameters&amp;#8230; So I&amp;#8217;m not going to mess with them. However, I did go make a list of the parameters I found in the default templates ( I could not find a list of these parameters on the net anywhere&amp;#8230;)&lt;/p&gt;
&lt;ul&gt;&lt;li&gt;$rootnamespace$&lt;/li&gt;
&lt;li&gt;$safeitemrootname$&lt;/li&gt;
&lt;li&gt;$registeredorganization$&lt;/li&gt;
&lt;li&gt;$year$&lt;/li&gt;
&lt;li&gt;$guid1$&lt;/li&gt;
&lt;li&gt;$ContentTags$&lt;/li&gt;
&lt;li&gt;$MasterPage$&lt;/li&gt;
&lt;li&gt;$fileinputname$&lt;/li&gt;
&lt;li&gt;$classname$&lt;/li&gt;
&lt;li&gt;$safeitemname$&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;&lt;br/&gt;So I modified Class.cs to be:&lt;/p&gt;
&lt;pre class="brush:csharp"&gt;&lt;br/&gt;using System;&lt;br/&gt;using System.Collections.Generic;&lt;br/&gt;using System.Text;&lt;br/&gt;&lt;br/&gt;namespace $rootnamespace$&lt;br/&gt;{&lt;br/&gt; public class $safeitemrootname$&lt;br/&gt; {&lt;br/&gt; #region Constructors&lt;br/&gt; public $safeitemrootname$()&lt;br/&gt; {&lt;br/&gt; Init();&lt;br/&gt; }&lt;br/&gt;&lt;br/&gt; /// &amp;lt;summary&amp;gt;&lt;br/&gt; /// Initializes field values.&lt;br/&gt; /// &amp;lt;/summary&amp;gt;&lt;br/&gt; private void Init()&lt;br/&gt; {&lt;br/&gt; }&lt;br/&gt; #endregion Constructors&lt;br/&gt;&lt;br/&gt; #region Fields&lt;br/&gt; #endregion Fields&lt;br/&gt;&lt;br/&gt; #region Properties&lt;br/&gt; #endregion Properties&lt;br/&gt;&lt;br/&gt; #region Public Methods&lt;br/&gt; #endregion Public Methods&lt;br/&gt;&lt;br/&gt; #region Private Methods&lt;br/&gt; #endregion Private Methods&lt;br/&gt; }&lt;br/&gt;}&lt;/pre&gt;
&lt;p&gt;Yay! my fingers are saved&amp;#8230; but what you say? all my base are NOT belong to us? True. Someone set us up the cache.&lt;/p&gt;
&lt;p&gt;&lt;br/&gt;&lt;strong&gt;Refreshing the Visual Studio Template Cache&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;br/&gt;The template files are cached in a folder called TemplateItemCache in the same location as the template folder. So..&lt;/p&gt;
&lt;ol&gt;&lt;br/&gt;&lt;li&gt;Close all Visual Studio Windows&lt;/li&gt;
&lt;li&gt;Clear contents of TemplateCache folder&lt;/li&gt;
&lt;li&gt;Open a DOS prompt (normal one, or Visual Studio 2005 Command Prompt)&lt;/li&gt;
&lt;li&gt;Run devenv /installvstemplates &lt;em&gt;(if that doesn&amp;#8217;t work, run devenv /setup)&lt;/em&gt;&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;&lt;br/&gt;Now, when you restart Visual Studio, your new templates will be installed&amp;#8230;&lt;/p&gt;
&lt;p&gt;&lt;br/&gt;BUT! the fun doesn&amp;#8217;t stop here! The same templating structure for items also applies to projects! But that&amp;#8217;s next post&amp;#8230;&lt;/p&gt;
&lt;p&gt;&lt;img width="1" height="1" src="https://blogger.googleusercontent.com/tracker/7907591936376660385-2371666848626402393?l=vanguard-against-confusion.blogspot.com"/&gt;&lt;/p&gt;</description><link>http://vanguard-against-confusion.tumblr.com/post/2516576006</link><guid>http://vanguard-against-confusion.tumblr.com/post/2516576006</guid><pubDate>Thu, 11 Oct 2007 21:05:00 -0700</pubDate><category>customize</category><category>Visual Studio</category><category>c</category><category>programming</category><category>templates</category><category>.NET</category><category>IDE</category><dc:creator>thoward37</dc:creator></item><item><title>CodeSnippet: PrintObject</title><description>&lt;p&gt;Following up on the same idea as the ListEnum code snippet, this is a method I often use to print the properties of an object to the console.&lt;br/&gt;&lt;br/&gt;I write a number of small command-line utilities, and typically, I create a &amp;#8220;property bag&amp;#8221; type of object that contains all of the possible command-line options. Just before execution, I like to display the options to the user, so that they know that the program knows what they meant by the command-line options.&lt;br/&gt;&lt;br/&gt;Here&amp;#8217;s the code that does that:&lt;br/&gt;&lt;br/&gt;&lt;/p&gt;
&lt;pre class="brush:csharp"&gt;&lt;br/&gt;private static void printObject(object obj)&lt;br/&gt;{   &lt;br/&gt;    PropertyInfo[] pia = obj.GetType().GetProperties();&lt;br/&gt;    foreach (PropertyInfo pi in pia)&lt;br/&gt;    { &lt;br/&gt;        Console.WriteLine(&lt;br/&gt;            pi.Name.PadRight(16, ' ') + &lt;br/&gt;            ": " + &lt;br/&gt;            pi.GetValue(obj, null).ToString());&lt;br/&gt;    }&lt;br/&gt;}&lt;/pre&gt;
&lt;p&gt;&lt;br/&gt;&lt;br/&gt;&lt;br/&gt;This could, of course, be used for any scenario where you want to inspect the values of the properties on an object. If you were so inclined, this could easily be expanded to have a lot more detail, handle arrays, print the type name of the object, etc..  I have various permutations of this method that do some or all of that as needed. I have considered turning this into a class with all those fiddly bits configurable, but haven&amp;#8217;t gotten around to it yet.. If I ever do, I&amp;#8217;ll post it here!&lt;/p&gt;
&lt;p&gt;&lt;img width="1" height="1" src="https://blogger.googleusercontent.com/tracker/7907591936376660385-3035605349484599722?l=vanguard-against-confusion.blogspot.com"/&gt;&lt;/p&gt;</description><link>http://vanguard-against-confusion.tumblr.com/post/2516575657</link><guid>http://vanguard-against-confusion.tumblr.com/post/2516575657</guid><pubDate>Tue, 10 Jul 2007 16:30:00 -0700</pubDate><category>c</category><category>programming</category><category>properties</category><category>object</category><category>code snippet</category><category>.NET</category><category>reflection</category><dc:creator>thoward37</dc:creator></item><item><title>IComparable and Egocentrism</title><description>&lt;p&gt;Today, on the ride home from work on the MAX train (local light-rail here in Portland, OR), I overheard a girl talking to some young Hispanic men. She was babbling on in a typically &amp;#8220;White American&amp;#8221; way, about cultural differences, and how &amp;#8220;We&amp;#8217;re really more alike than we are different.&amp;#8221; and that popular media tries to force differences down our cultural throats through advertisements and TV (evil incarnate).&lt;br/&gt;&lt;br/&gt;While her stance is in many ways similar to my own thinking, I still felt compelled to consider how I would respond if I were having the conversation with her&amp;#8230; It would go something like this&amp;#8230;&lt;br/&gt;&lt;br/&gt;Why do we put such a fine point on our differences? Why do we go to war over skin colours, eating habits, clothing choices, and other such nonsense? Because human beings are intrinsically scared shitless of sameness. Internally, we must compare everything. We are so bound up in the process of comparison logic, that it permeates our every action. Is this bad or good? Better or worse? Bigger or smaller? Subordinate or superordinate? Our base class is IComparable.&lt;br/&gt;&lt;br/&gt;These thoughts consume our lowest level drives.. To be a good person.. To get ahead in life&amp;#8230; To be comfortable (as opposed to NOT comfortable, and any degree of comfortable is better than any degree of uncomfortable). To be powerful.. not just powerful, but specifically more powerful than you were before, or more powerful than the other guy.&lt;br/&gt;&lt;br/&gt;So we focus on our differences, because through our differences we can find something, ANYTHING to make us special, better, to return 1 on our .CompareTo() call for at least one property.&lt;br/&gt;&lt;br/&gt;This got me to thinking about the implementation of IComparable in .NET/C#. Isn&amp;#8217;t it quite egocentric? To presume that the scope of knowledge within a single object type is sufficient to allow it to be compared to any other type? To consider that I know how to compare myself to any other thing, even if I don&amp;#8217;t know what that thing is? That notion is quite absurd. What I find interesting about the implementation is the .CompareTo() takes an untyped object as a parameter. Doesn&amp;#8217;t it follow that an object of a given type should only be able to compare itself to something else of the same type? That in order to compare to an object of some other type it must at least be able to be converted to that type first, so that it can be compared on equal terms?&lt;br/&gt;&lt;br/&gt;There&amp;#8217;s a lot of discussion about that implementation. It could be argued that it&amp;#8217;s valid, but nonetheless, it&amp;#8217;s completely egocentric. How do you resolve a scenario, where  both foo.CompareTo(bar) and bar.CompareTo(foo) both return 1? Which one sorts higher in the call to SortedList.Sort()? or do they simply not change position relative to one another ever? So first come first serve?&lt;br/&gt;&lt;br/&gt;What if IComparable worked differently? I envision it this way&amp;#8230; Image a static object called System.Judge. System.Judge has a method .Compare which takes any two objects that implement IComparable. The interface for IComparable requires the object to maintain a property .CompareValues which contains a list of all the values it maintains that it is willing to offer up during comparison, organized by Type, Name, Value. The Judge accesses foo.CompareValues.Types to get a list of types that it is willing to be compared to. Judge calls that from both objects, until it finds a list of compatible types to start comparison with. For all comparable matching types, a comparison result is achieved, and then an average of comparison is evaluated, and the object with the highest average of comparison success is considered the victor. The .CompareTo call would naturally be nested calls on the various IComparable types presented until finally a value type with a fixed, built-in comparision method is found and stops the nesting compare calls.&lt;br/&gt;&lt;br/&gt;This sytem would of course be more complicated, and require a lot more processing for each call, resulting in much slower performance.. Ah but the logic would be sound, and that, my friends, is much more valuable than processing time.&lt;br/&gt;&lt;br/&gt;Good night.&lt;/p&gt;
&lt;img src="https://blogger.googleusercontent.com/tracker/7907591936376660385-2327275669456518043?l=vanguard-against-confusion.blogspot.com" height="1" width="1"/&gt;</description><link>http://vanguard-against-confusion.tumblr.com/post/2516575345</link><guid>http://vanguard-against-confusion.tumblr.com/post/2516575345</guid><pubDate>Fri, 08 Jun 2007 05:58:00 -0700</pubDate><category>IComparable</category><category>a better way</category><category>egocentrism</category><category>.NET</category><dc:creator>thoward37</dc:creator></item><item><title>Disable Design-Time Support in Visual Studio</title><description>&lt;p&gt;I recently wrote a class in c# that inherits from System.Diagnostics.Process. This class abstracts a shelling-to-disk process that I need to do. Something like this:&lt;/p&gt;
&lt;pre class="brush:csharp"&gt;public class MyShellTask : Process&lt;br/&gt;{&lt;br/&gt;...&lt;br/&gt;}&lt;/pre&gt;
&lt;p&gt;&lt;br/&gt;One thing that bugged me to no end is that, in Visual Studio, when you double click the file in the Solution Explorer, it considered it &amp;#8220;designable&amp;#8221; even though there was no designer. So that means I got a empty page every time, telling me that it was not designable, with a link to &amp;#8220;View Code&amp;#8221;. Well, &amp;#8220;View Code&amp;#8221; is what I wanted, not &amp;#8220;View Designer&amp;#8221;, when I double-clicked!&lt;br/&gt;&lt;br/&gt;So after getting very frustrated, I did the natural thing.. I googled looking for an answer. I had a notion that I could control this behaviour through Attribute tags on the class if only I knew the right one. Having made designable components before I was familiar with the attributes used for that. I tried fiddling about with Intellisense, Googling, all to no avail.. Nothing worked! Nothing showed up in my Google searches! Good God! What to do now?&lt;br/&gt;&lt;br/&gt;Fiddle some more&amp;#8230; until finally I found the right attribute:&lt;/p&gt;
&lt;pre class="brush:csharp"&gt;[System.ComponentModel.DesignerCategory("")]&lt;br/&gt;public class MyShellTask : Process&lt;br/&gt;{&lt;br/&gt;...&lt;br/&gt;}&lt;/pre&gt;
&lt;p&gt;&lt;br/&gt;&lt;br/&gt;Note that you must call this with an empty string (don&amp;#8217;t believe the intellisense comment, an empty constructor call will NOT do the same as calling the constructor with an empty string.) This sets it to a non-category that it doesn&amp;#8217;t know how to deal with, and so doesn&amp;#8217;t offer designer support to you!&lt;br/&gt;&lt;br/&gt;&lt;br/&gt;This also helps custom installer class for use with your Visual Studio Setup projects, which exhibit the same annoying VS UI problems&amp;#8230; ie:&lt;/p&gt;
&lt;pre class="brush:csharp"&gt;/// &lt;br/&gt;/// Custom Installer actions for this project.&lt;br/&gt;/// &lt;br/&gt;[RunInstaller(true)]&lt;br/&gt;[System.ComponentModel.DesignerCategory("")]&lt;br/&gt;public partial class MyInstaller : Installer&lt;br/&gt;{&lt;br/&gt;   ...&lt;br/&gt;}&lt;/pre&gt;
&lt;p&gt;&lt;br/&gt;&lt;br/&gt;Hope that helps someone! Now there will be at least ONE hit if someone googles up &amp;#8220;disable design-time support&amp;#8221; or &amp;#8220;disable designer support&amp;#8221; like I did!&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blogger.googleusercontent.com/tracker/7907591936376660385-139882525296533812?l=vanguard-against-confusion.blogspot.com" height="1" width="1"/&gt;&lt;/p&gt;</description><link>http://vanguard-against-confusion.tumblr.com/post/2516575007</link><guid>http://vanguard-against-confusion.tumblr.com/post/2516575007</guid><pubDate>Fri, 01 Jun 2007 00:52:00 -0700</pubDate><category>Visual Studio</category><category>Attribute</category><category>System.Diagnostics.Process</category><category>System.Configuration.Install.Installer</category><category>System.ComponentModel.DesignerCategory</category><category>designer</category><category>inheritance</category><dc:creator>thoward37</dc:creator></item><item><title>Java, NetBeans, and Templates, OH MY!</title><description>&lt;p&gt;Well, having recently sparked an interest in moving towards a Open Source, cross platform, but still as cool as c#/VS2005 development platform, I of course landed in the middle of NetBeans 5.5 and Java.&lt;br/&gt;&lt;br/&gt;Having never programmed in Java before, but understanding it&amp;#8217;s really similar to c# (or I should say c# is really similar to Java), I immediately started fiddling about as if I were writing c# code. So, it&amp;#8217;s easy to get past typing uppercase String, not lowercase, and also not too hard to grok &amp;#8220;extends&amp;#8221; instead of &amp;#8220;:&amp;#8221; for inheritance. The one-class-per-file thing, well, I guess it will just make me a more organized programmer, however annoying it is. But the things that really erked me was properties.&lt;br/&gt;&lt;br/&gt;In c# I can do this:&lt;/p&gt;
&lt;pre class="brush:csharp"&gt;...&lt;br/&gt;private string _name;&lt;br/&gt;&lt;br/&gt;public string Name&lt;br/&gt;{&lt;br/&gt;  get&lt;br/&gt;  {&lt;br/&gt;      return this._name;&lt;br/&gt;  }&lt;br/&gt;  set&lt;br/&gt;  {&lt;br/&gt;      this._name = value;&lt;br/&gt;  }&lt;br/&gt;}&lt;br/&gt;...&lt;/pre&gt;
&lt;p&gt;&lt;br/&gt;but in Java, that looks like:&lt;/p&gt;
&lt;pre class="brush:java"&gt;...&lt;br/&gt;private String _name;&lt;br/&gt;&lt;br/&gt;public String getName()&lt;br/&gt;{&lt;br/&gt;  return this._name;&lt;br/&gt;}&lt;br/&gt;&lt;br/&gt;public void setName(string value)&lt;br/&gt;{&lt;br/&gt;  this._name = value;&lt;br/&gt;}&lt;br/&gt;...&lt;/pre&gt;
&lt;p&gt;&lt;br/&gt;&lt;br/&gt;Wow. Extremely obnoxious. Furthermore, I have finally gotten myself broken in with the VS2005 IDE to type &amp;#8220;prop&amp;#8221; + TAB to get a nice template for my properties. Well, since there is no such thing in Java, this macro also does not exist. So, I proceeded to make a NetBeans code template called &amp;#8220;prop&amp;#8221;, which functions the same way the VS2005 &amp;#8220;prop&amp;#8221; code snippet does.&lt;br/&gt;&lt;br/&gt;So, for all you c# coders who are venturing into the foreign lands of Java, here&amp;#8217;s a little tutorial on how to add this little cultural comfort into NetBeans.&lt;br/&gt;&lt;br/&gt;&lt;br/&gt;&lt;/p&gt;
&lt;p&gt;Property Code Template Installation Instructions:&lt;/p&gt;

&lt;ol&gt;&lt;li&gt;Select menu item &amp;#8220;Tools-&amp;gt;Options&amp;#8221;.&lt;/li&gt;
&lt;li&gt;Click on &amp;#8220;Editor&amp;#8221; sidebar button.&lt;/li&gt;
&lt;li&gt;Click on &amp;#8220;Code Templates&amp;#8221; tab.&lt;/li&gt;
&lt;li&gt;Select &amp;#8220;Java&amp;#8221; from languages combo-box.&lt;/li&gt;
&lt;li&gt;Click &amp;#8220;New&amp;#8221;, and then enter &amp;#8220;prop&amp;#8221; as the Abbreviation in the dialog.&lt;/li&gt;
&lt;li&gt;Click &amp;#8220;Ok&amp;#8221;.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Make sure &amp;#8220;prop&amp;#8221; is the selected template, and in the text box below the list, enter these lines:&lt;/p&gt;
&lt;pre class="brush:csharp"&gt;  private ${int} ${_prop};&lt;br/&gt;&lt;br/&gt;  public ${int} get${Property}()&lt;br/&gt;  {&lt;br/&gt;      return this.${_prop};&lt;br/&gt;  }&lt;br/&gt;&lt;br/&gt;  public void set${Property}(${int} value)&lt;br/&gt;  {&lt;br/&gt;          this.${_prop} = value;&lt;br/&gt;  }&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;Select &amp;#8220;Tab&amp;#8221; from &amp;#8220;Expand On&amp;#8221; combo box.&lt;/li&gt;
&lt;li&gt;Click &amp;#8220;OK&amp;#8221;.&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;Now you&amp;#8217;ve got it installed.. Feel free to go to the code and give it a whirl! Have a look at the other macros in the list to see what&amp;#8217;s built in, and once you figure out the syntax of the template notation, make your own templates!&lt;/p&gt;
&lt;p&gt;&lt;img width="1" height="1" src="https://blogger.googleusercontent.com/tracker/7907591936376660385-4248725817275536988?l=vanguard-against-confusion.blogspot.com"/&gt;&lt;/p&gt;</description><link>http://vanguard-against-confusion.tumblr.com/post/2516574665</link><guid>http://vanguard-against-confusion.tumblr.com/post/2516574665</guid><pubDate>Wed, 09 May 2007 05:53:00 -0700</pubDate><category>cross platform</category><category>templates</category><category>net beans</category><category>java</category><category>open source</category><dc:creator>thoward37</dc:creator></item></channel></rss>

