<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <id>tag:www.refactormycode.com,2007:users224</id>
  <link type="application/atom+xml" href="http://www.refactormycode.com/users/224" rel="self"/>
  <title>nibbles&amp;bits</title>
  <updated>Thu Dec 20 21:13:03 -0800 2007</updated>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor1221</id>
    <published>2007-12-20T21:13:03-08:00</published>
    <title>[C#] On Code to Simplify</title>
    <content type="html">&lt;p&gt;for the sake of readability, i'm more of a lookup man myself.
&lt;br /&gt;it may not be as performant, but it's sure easier to read, and the ultimate addition line is much more understandable/simple.&lt;/p&gt;

&lt;pre&gt;// just for the switch

Dictionary&amp;lt;char,int&amp;gt; dict = new Dictionary&amp;lt;char,int&amp;gt;(26);
dict.Add('A',1);
dict.Add('B',0);
dict.Add('C',5);
dict.Add('D',7);
dict.Add('E',9);
dict.Add('F',13);
dict.Add('G',15);
dict.Add('H',17);
dict.Add('I',19);
dict.Add('J',21);
dict.Add('K',2);
dict.Add('L',4);
dict.Add('M',18);
dict.Add('N',20);
dict.Add('O',11);
dict.Add('P',3);
dict.Add('Q',6);
dict.Add('R',8);
dict.Add('S',12);
dict.Add('T',14);
dict.Add('U',16);
dict.Add('V',10);
dict.Add('W',22);
dict.Add('X',25);
dict.Add('Y',24);
dict.Add('Z',23);
dict.Add('0',1);
dict.Add('1',0);
dict.Add('2',5);
dict.Add('3',7);
dict.Add('4',9);
dict.Add('5',13);
dict.Add('6',15);
dict.Add('7',17);
dict.Add('8',19);
dict.Add('9',21);
// then instead of the big switch, use this:
somma += dict[codice_fiscale[i]];

&lt;/pre&gt;</content>
    <author>
      <name>nibbles&amp;bits</name>
      <email>vbrunner__@hotmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/188-code-to-simplify/refactors/1221" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code102</id>
    <published>2007-10-24T15:13:49-07:00</published>
    <updated>2008-08-14T01:14:02-07:00</updated>
    <title>[C#] I hate to do this, but...</title>
    <content type="html">&lt;p&gt;Let the battle begin!&lt;/p&gt;

&lt;pre&gt;using System;

namespace FizzBuzz
{
    class Program
    {
        static void Main(string[] args)
        {

            for (int i = 1; i &amp;lt; 101; i++)
                Console.WriteLine(string.Format(&amp;quot;{0}{1}{2}&amp;quot;,
                     i % 3 != 0 &amp;amp;&amp;amp; i % 5 != 0 ? i.ToString() : &amp;quot;&amp;quot;, i % 3 == 0 ? &amp;quot;Fizz&amp;quot; : &amp;quot;&amp;quot;, i % 5 == 0 ? &amp;quot;Buzz&amp;quot; : &amp;quot;&amp;quot;));
            Console.ReadKey();
        }
    }
}
&lt;/pre&gt;</content>
    <author>
      <name>nibbles&amp;bits</name>
      <email>vbrunner__@hotmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/102-i-hate-to-do-this-but" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code96</id>
    <published>2007-10-21T02:34:55-07:00</published>
    <updated>2007-11-07T11:54:51-08:00</updated>
    <title>[C#] ExecuteCommandBatch</title>
    <content type="html">&lt;p&gt;It's rather large, but also rather simple.
&lt;br /&gt;The actual implementation has several overloads; it also doesn't seem to roll back every command.&lt;/p&gt;

&lt;pre&gt;    static class SqlExec
    {
        public static void ExecCommandBatch(System.Data.Common.DbConnection conn, System.Data.IsolationLevel isolation, ExceptionFailSafeEventHandler&amp;lt;System.Exception&amp;gt; failSafe, params System.Data.Common.DbCommand[] cmds)
        {
            if ((conn == null))
                throw new System.ArgumentNullException(&amp;quot;conn&amp;quot;);
            if ((cmds.Length &amp;lt; 2))
                throw new System.ArgumentException(&amp;quot;Must supply more than one Command&amp;quot;);
            // TODO: validate isolation?
            bool success = false;
            BatchCommandFailedException excp = null;
            ExceptionFailSafeEventArgs&amp;lt;System.Exception&amp;gt; args = new ExceptionFailSafeEventArgs&amp;lt;System.Exception&amp;gt;();
            int totalAffectedRows = 0;
            int length = cmds.Length - 1;
            int i;
            if ((conn.State != ConnectionState.Open))
                conn.Open();
            DbTransaction trans;
            try
            {
                trans = conn.BeginTransaction(isolation);
            }
            catch (System.Exception ex)
            {
                trans = conn.BeginTransaction();
            }
            for (i = 0; i &amp;lt;= length; i++)
            {
                cmds[i].Connection = conn;
                cmds[i].Transaction = trans;
            }
            try
            {
                for (i = 0; i &amp;lt;= length; i++)
                {
                    totalAffectedRows += cmds[i].ExecuteNonQuery();
                }
                success = true;
            }
            catch (System.Exception ex)
            {
                excp = new BatchCommandFailedException(cmds[i], i, ex);
            }
            finally
            {
                if (success)
                {
                    trans.Commit();
                }
                else
                {
                    trans.Rollback();
                    totalAffectedRows = 0;
                    args.Exception = excp.InnerException;
                    if (failSafe != null)
                        failSafe.Invoke(typeof(SqlExec), args);
                }
                trans.Dispose();
                foreach (DbCommand cmd in cmds)
                {
                    cmd.Dispose();
                }
                conn.Close();
                conn.Dispose();
                if (excp != null)
                    if (args.Throw)
                        throw excp;

            }
            //Return totalAffectedRows?
        }
    }

    public delegate void ExceptionFailSafeEventHandler&amp;lt;T&amp;gt;(object sender, ExceptionFailSafeEventArgs&amp;lt;T&amp;gt; e) where T : System.Exception;
    public class ExceptionFailSafeEventArgs&amp;lt;T&amp;gt; : System.EventArgs where T : System.Exception
    {

        private T _data;
        private bool _throw = true;

        public bool Throw
        {
            get { return _throw; }
            set { _throw = value; }
        }

        public ExceptionFailSafeEventArgs()
            : this(null) { }
        public ExceptionFailSafeEventArgs(T ex)
            : base()
        { _data = ex; }

        public T Exception
        { 
            get { return _data; }
            set { _data = value; }
        }
    }
    [Serializable()]
    public class BatchCommandFailedException : DbException
    {
        public BatchCommandFailedException(string message)
            : base(&amp;quot;Command execution has failed.  Reason:  &amp;quot; + message){ }
        public BatchCommandFailedException(string message, DbCommand cmd)
            : base(string.Format(&amp;quot;Command execution for command '{0}' has failed.  Reason:  &amp;quot; + message, cmd.CommandText)){ }
        public BatchCommandFailedException(DbCommand cmd)
            : base(string.Format(&amp;quot;Command execution for command '{0}' has failed.&amp;quot;, cmd.CommandText)){ }
        public BatchCommandFailedException(DbCommand cmd, int ordinal)
            : base(string.Format(&amp;quot;Command execution for command {0}:  '{1}' has failed.&amp;quot;, ordinal, cmd.CommandText)){ }
        public BatchCommandFailedException(DbCommand cmd, int ordinal, Exception inner)
            : base(string.Format(&amp;quot;Command execution for command {0}:  '{1}' has failed.  Reason:  {2}&amp;quot;, ordinal, cmd.CommandText, inner.Message), inner){ }
        public BatchCommandFailedException(string message, Exception inner)
            : base(message, inner){ }
        public BatchCommandFailedException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
            : base(info, context){ }
    }&lt;/pre&gt;</content>
    <author>
      <name>nibbles&amp;bits</name>
      <email>vbrunner__@hotmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/96-executecommandbatch" rel="alternate"/>
  </entry>
</feed>

