﻿<?xml version="1.0" encoding="UTF-8"?>
<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/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Programming By A Tool &#187; String</title>
	<atom:link href="http://byatool.com/tag/string/feed/" rel="self" type="application/rss+xml" />
	<link>http://byatool.com</link>
	<description>This is my recorded stumbling through ASP.Net, Framework 3.5, C# 3.0, Ajax, Javascript, and Love.  Stay, learn, destroy.  It's your life.</description>
	<lastBuildDate>Thu, 03 May 2012 17:28:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Duck Typing my way to a Universal String Convert</title>
		<link>http://byatool.com/utilities/duck-typing-my-way-to-a-universal-string-convert/</link>
		<comments>http://byatool.com/utilities/duck-typing-my-way-to-a-universal-string-convert/#comments</comments>
		<pubDate>Mon, 17 Nov 2008 14:48:15 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[Utilities]]></category>
		<category><![CDATA[Dynamic]]></category>
		<category><![CDATA[Generics]]></category>
		<category><![CDATA[Reflection]]></category>
		<category><![CDATA[String]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=94</guid>
		<description><![CDATA[So being the beacon of ignorance in the fog of brilliance, I had no idea what Duck Typing was. In short it&#8217;s the idea of assuming a class&#8217;s type based on the methods it holds. Say you have 5 different classes, none of which share an inheritance tree. Now let&#8217;s say you have a method [...]]]></description>
			<content:encoded><![CDATA[<p>So being the beacon of ignorance in the fog of brilliance, I had no idea what Duck Typing was.  In short it&#8217;s the idea of assuming a class&#8217;s type based on the methods it holds. </p>
<p>Say you have 5 different classes, none of which share an inheritance tree.  Now let&#8217;s say you have a method that takes in any object and uses the SeanIsAwsome method on the object.  You could make sure that every object sent in has that method by using an interface that all the objects sent in share.</p>
<pre>    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> SomeMethod(<span style="color: #33cccc;">ISeanRules</span> inParameter)</pre>
<p>What if you want to send in a bunch of objects that don&#8217;t share the same interface/base class? Well you base it on what methods the class holds. If the class has the SeanIsAwsome method, then you call it. If not, well you could throw an exception, do nothing, go jogging, eat bacon, ect. That part is up to you.</p>
<p>The problem was that I wanted to create a universal convert that would take in a string and convert it to the 8 billion (ballpark figure) value types in C#. Now what I wanted to do is use the famous TryParse method so my first hope was that TryParse was a method on an interface they all shared. Yeah no. So next thought was to use the Duck Typing principle and say, &#8220;Hey jackass, you have a TryParse method? Yeah? Good. Use it.&#8221; &#8216;Course I had to find a way to do that. Turns out it wasn&#8217;t too bad.</p>
<pre><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">class</span> ConvertFromString
{
  <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> T? ConvertTo&lt;T&gt;(<span style="color: #0000ff;">this</span> <span style="color: #33cccc;">String</span> numberToConvert) <span style="color: #0000ff;">where</span> T : <span style="color: #0000ff;">struct</span>
  {
    T? returnValue = null;

    <span style="color: #33cccc;">MethodInfo</span> neededInfo = GetCorrectMethodInfo(<span style="color: #0000ff;">typeof</span>(T));
    if (neededInfo != null &amp;&amp; !numberToConvert.IsNullOrEmpty())
    {
      T output = <span style="color: #0000ff;">default</span>(T);
      <span style="color: #0000ff;">object</span>[] paramsArray = <span style="color: #0000ff;">new</span> <span style="color: #0000ff;">object</span>[2] { numberToConvert, output };
      returnValue = <span style="color: #0000ff;">new</span> T();

      <span style="color: #0000ff;">object</span> returnedValue = neededInfo.Invoke(returnValue.Value, paramsArray);

      <span style="color: #0000ff;">if</span> (returnedValue <span style="color: #0000ff;">is</span> <span style="color: #33cccc;">Boolean</span> &amp;&amp; (<span style="color: #33cccc;">Boolean</span>)returnedValue)
      {
        returnValue = (T)paramsArray[1];
      }
      <span style="color: #0000ff;">else</span>
      {
        returnValue = <span style="color: #0000ff;">null</span>;
      }
    }

    <span style="color: #0000ff;">return</span> returnValue;
  }
}</pre>
<p>A whaaaa?</p>
<p>Ok this might look odd, but it&#8217;s really simple. If it doesn&#8217;t look odd then chances are you&#8217;re smarter than me and you shouldn&#8217;t be here anyhow. First part is simple:</p>
<pre>  <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> T? ConvertTo&lt;T&gt;(<span style="color: #0000ff;">this</span> <span style="color: #33cccc;">String</span> numberToConvert) <span style="color: #0000ff;">where</span> T : <span style="color: #0000ff;">struct</span></pre>
<p>Due to this being a extension method (Opps forgot to tell you that part) I have to declare this static method in a static class. Basically I am taking in a string and returning a nullable version of whatever type I specific in the call.</p>
<pre>    T? returnValue = <span style="color: #0000ff;">null</span>;

    <span style="color: #33cccc;">MethodInfo</span> neededInfo = GetCorrectMethodInfo(<span style="color: #0000ff;">typeof</span>(T));</pre>
<p>Remember that MethodInfo method I had explained in the last post? The next part you might remember too, but I&#8217;m not betting on it.</p>
<pre>    <span style="color: #0000ff;">if</span> (neededInfo != <span style="color: #0000ff;">null</span> &amp;&amp; !numberToConvert.IsNullOrEmpty())
    {
      T output = <span style="color: #0000ff;">default</span>(T);
      <span style="color: #0000ff;">object</span>[] paramsArray = <span style="color: #0000ff;">new</span> <span style="color: #0000ff;">object</span>[2] { numberToConvert, output };
      returnValue = <span style="color: #0000ff;">new</span> T();

      <span style="color: #0000ff;">object</span> returnedValue = neededInfo.Invoke(returnValue.Value, paramsArray);</pre>
<p>Ok so I have the method info, now I have to create the list of parameters to send in with the invoke. Pretty straight forward. If things went well, returnedValue should be a boolean. However, just incase:</p>
<pre>    <span style="color: #0000ff;">if</span> (returnedValue is <span style="color: #33cccc;">Boolean</span> &amp;&amp; (<span style="color: #33cccc;">Boolean</span>)returnedValue)
    {
      returnValue = (T)paramsArray[1];
    }
    <span style="color: #0000ff;">else</span>
    {
      returnValue = <span style="color: #0000ff;">null</span>;
    }</pre>
<p>So if the returnValue is true and boolean, then the tryParse was successful. With that in mind, I still have to get the converted value. If it had not been successful than I am just going to return null since this is just meant for converting and not for whether or not it could be. It is just assumed that if it&#8217;s null, then it could not be converted at this time.</p>
<p>And now for the use:</p>
<pre>  <span style="color: #0000ff;">decimal</span>? converted = someString.ConvertTo&lt;<span style="color: #0000ff;">decimal</span>&gt;()</pre>
<p>An boom, you have a universal convert. YAY!</p>
<pre>  <span style="color: #0000ff;">using</span> System;
  <span style="color: #0000ff;">using</span> System.Reflection;</pre>
]]></content:encoded>
			<wfw:commentRss>http://byatool.com/utilities/duck-typing-my-way-to-a-universal-string-convert/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Random String of Specified Length with Enumerable</title>
		<link>http://byatool.com/utilities/random-string-of-specified-length-with-enumerable/</link>
		<comments>http://byatool.com/utilities/random-string-of-specified-length-with-enumerable/#comments</comments>
		<pubDate>Thu, 26 Jun 2008 16:30:00 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[Utilities]]></category>
		<category><![CDATA[.Net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Random]]></category>
		<category><![CDATA[String]]></category>

		<guid isPermaLink="false">http://rockcityghost.wordpress.com/2008/06/26/random-string-of-specified-length-with-enumerable/</guid>
		<description><![CDATA[using System; using System.Collections.Generic; using System.Linq; using System.Text; public static String RandomString(Int32 length) { Random randomGenerator; String returnValue; randomGenerator = new Random(); returnValue = new string(Enumerable.Range(0, length).Select(i =&#62; (char)('A' + randomGenerator.Next(0, 25))).ToArray()); return returnValue; } What this does: Enumerable.Range basically says, &#8220;Give me a collection of numbers from the first parameter to the second&#8221;, or [...]]]></description>
			<content:encoded><![CDATA[<pre><strong>
<span style="color: #33cccc;">using</span> System;
<span style="color: #33cccc;">using</span> System.Collections.Generic;
<span style="color: #33cccc;">using</span> System.Linq;
<span style="color: #33cccc;">using</span> System.Text;

<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #33cccc;">String</span> RandomString(<span style="color: #33cccc;">Int32</span> length)
{
 <span style="color: #33cccc;">Random</span> randomGenerator;
 <span style="color: #33cccc;">String</span> returnValue;

  randomGenerator = <span style="color: #0000ff;">new</span> <span style="color: #33cccc;">Random</span>();

  returnValue = <span style="color: #0000ff;">new</span> <span style="color: #0000ff;">string</span>(<span style="color: #33cccc;">Enumerable</span>.Range(0, length).Select(i =&gt; (<span style="color: #0000ff;">char</span>)(<span style="color: #ff0000;">'A'</span> + randomGenerator.Next(0, 25))).ToArray());

 <span style="color: #0000ff;">return</span> returnValue;
}
</strong></pre>
<p>What this does:</p>
<p>Enumerable.Range basically says, &#8220;Give me a collection of numbers from the first parameter to the second&#8221;, or in this case from 0 to length.</p>
<p>Select basically is a method that takes in an anonymous method (Lamdba expression in this case), goes through each of the items in the collection, and runs the anonymous method for every item in the collection. Not exactly sure what Select does specifically, but it is most likely something like this: (roughish psuedo code)</p>
<pre><strong>
<span style="color: #33cccc;">public</span> <span style="color: #0000ff;">static</span> Array Select(<span style="color: #33cccc;">Func</span> func))
{

 <span style="color: #33cccc;">Array</span> returnValue;

 <span style="color: #0000ff;">foreach</span>(<span style="color: #33cccc;">Int32</span> currentItem <span style="color: #0000ff;">in</span> Items)
 {
    returnValue.Add(func());
 }

 return returnValue;

}
</strong></pre>
<p>Where Func is:</p>
<pre><strong>
<span style="color: #0000ff;">public</span> <span style="color: #33cccc;">Char</span> Func()
{
 return  (<span style="color: #0000ff;">char</span>)(<span style="color: #ff0000;">'A'</span> + randomGenerator.Next(0, 25);
}
</strong></pre>
]]></content:encoded>
			<wfw:commentRss>http://byatool.com/utilities/random-string-of-specified-length-with-enumerable/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

