Vector Class and Events in .Net

If you came to C# from C++, you're going to notice rather fast that one data type is missing from the System.Collections namespace. Namely a vector class. In C++ there's the std::vector class which is used about any time that you need an array in C++ (unless of course you like recreating an array when you out grow it). In C#, there is no class like that. We have the List class, which is used with about the same frequency and has a similar function but you don't have that great of control over the capacity. I can set the capacity of it but I can't really control how much is added each time it needs to resize (not to mention I would prefer to have some sort of tie in with events, etc.). So I ended up creating my own Vector class.

/*
Copyright (c) 2009 <a href="http://www.gutgames.com">James Craig</a>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.*/

#region Usings
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Utilities.Events.EventArgs;
using Utilities.Events;
#endregion

namespace Utilities.DataTypes
{
    /// <summary>
    /// Vector class
    /// </summary>
    /// <typeparam name="T">The type of item the vector should hold</typeparam>
    public class Vector<T> : IList<T>
    {
        #region Constructor

        /// <summary>
        /// Constructor
        /// </summary>
        public Vector()
        {
            Items = new T[DefaultSize];
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="InitialSize">Initial size of the vector</param>
        public Vector(int InitialSize)
        {
            if (InitialSize < 1) throw new ArgumentOutOfRangeException("InitialSize");

            Items = new T[InitialSize];
            DefaultSize = InitialSize;
        }

        #endregion

        #region IList<T> Members

        public int IndexOf(T item)
        {
            return Array.IndexOf<T>(this.Items, item, 0, this.NumberItems);
        }

        public void Insert(int index, T item)
        {
            if (index > this.NumberItems || index < 0) throw new ArgumentOutOfRangeException("index");

            try
            {
                if (this.NumberItems == this.Items.Length)
                {
                    Array.Resize<T>(ref this.Items, this.Items.Length * 2);
                }
                if (index < this.NumberItems)
                {
                    Array.Copy(this.Items, index, this.Items, index + 1, this.NumberItems - index);
                }
                this.Items[index] = item;
                ++this.NumberItems;
                EventHelper.Raise<ChangedEventArgs>(Changed, this, new ChangedEventArgs());
            }
            catch (Exception a) { throw a; }
        }

        public void RemoveAt(int index)
        {
            if (index > this.NumberItems || index < 0) throw new ArgumentOutOfRangeException("index");

            try
            {
                if (index < this.NumberItems)
                {
                    Array.Copy(this.Items, index + 1, this.Items, index, this.NumberItems - (index + 1));
                }
                this.Items[this.NumberItems - 1] = default(T);
                --this.NumberItems;
                EventHelper.Raise<ChangedEventArgs>(Changed, this, new ChangedEventArgs());
            }
            catch (Exception a) { throw a; }
        }

        public T this[int index]
        {
            get
            {
                if (index > this.NumberItems || index < 0) throw new ArgumentOutOfRangeException("index");
                return this.Items[index];
            }
            set
            {
                if (index > this.NumberItems || index < 0) throw new ArgumentOutOfRangeException("index");
                this.Items[index] = value;
                EventHelper.Raise<ChangedEventArgs>(Changed, this, new ChangedEventArgs());
            }
        }

        #endregion

        #region ICollection<T> Members

        public void Add(T item)
        {
            try
            {
                Insert(this.NumberItems, item);
            }
            catch (Exception a) { throw a; }
        }

        public void Clear()
        {
            try
            {
                Array.Clear(this.Items, 0, this.Items.Length);
                this.NumberItems = 0;
                EventHelper.Raise<ChangedEventArgs>(Changed, this, new ChangedEventArgs());
            }
            catch (Exception a) { throw a; }
        }

        public bool Contains(T item)
        {
            return (this.IndexOf(item) >= 0);
        }

        public void CopyTo(T[] array, int arrayIndex)
        {
            Array.Copy(this.Items, 0, array, arrayIndex, this.NumberItems);
        }

        public int Count
        {
            get { return this.NumberItems; }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        public bool Remove(T item)
        {
            int Index = this.IndexOf(item);
            if (Index > 0)
            {
                this.RemoveAt(Index);
                return true;
            }
            return false;
        }

        #endregion

        #region IEnumerable<T> Members

        public IEnumerator<T> GetEnumerator()
        {
            for (int x = 0; x < this.NumberItems; ++x)
            {
                yield return this.Items[x];
            }
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            for (int x = 0; x < this.NumberItems; ++x)
            {
                yield return this.Items[x];
            }
        }

        #endregion

        #region Protected Variables/Properties

        protected int DefaultSize = 2;
        protected T[] Items = null;
        protected int NumberItems { get; set; }

        #endregion

        #region Events
        public EventHandler<ChangedEventArgs> Changed;
        #endregion
    }
}

Most of the class above is pretty straight forward. We have an array of elements and each time we insert an item and need to increase the size of the array, we double its size. If you wanted you could switch it from multiplying by two to a fixed size (a simple change really). The only portion which may not jump out at you though is the fact that there is an EventHandler class in there. An EventHandler is basically a generic class that holds a method that handles an event. It's not mine, just a .Net class. But it's defined with a ChangedEventArgs class which is mine. The ChangedEventArgs class is a simple EventArgs class that looks like this:

    public class BaseEventArgs : System.EventArgs
    {
        public bool Stop
        {
            get { return _Stop; }
            set { _Stop = value; }
        }
        private bool _Stop = false;

        public object Content
        {
            get { return _Content; }
            set { _Content = value; }
        }
        private object _Content = null;
    }

    public class ChangedEventArgs : BaseEventArgs
    {
    }

Just a slightly modified EventArgs class. The other thing that you may notice about the vector class is it uses an EventHelper class to call the EventHandler. Once again, it's something that I created. Basically I was tired of writing the basic event raising code for every instance so I created a generic way of doing it.

    public static class EventHelper
    {
        /// <summary>
        /// Raises an event
        /// </summary>
        /// <typeparam name="T">The type of the event args</typeparam>
        /// <param name="Delegate">The delegate</param>
        /// <param name="Sender">The sender</param>
        /// <param name="EventArgs">The event args</param>
        public static void Raise<T>(EventHandler<T> Delegate, object Sender, T EventArg) where T : System.EventArgs
        {
            try
            {
                if (Delegate != null)
                {
                    Delegate(Sender, EventArg);
                }
            }
            catch { throw; }
        }
}

It's pretty simple. Just takes in an EventHandler, the sender, and the eventargs. Even if all it does is saves me a couple lines of code in a couple other classes, that's all I really need. Anyway, that's it. With those classes we have our Vector class that can notify us when things are changed, we control how it grows, etc. along with a couple other functions/classes that you might find useful. So try it out, leave feedback, and happy coding.

kick it on DotNetKicks.com   Shout it
Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkListEmail

Posted by: James Craig
Posted on: 8/27/2009 at 8:26 AM
Tags: , , , ,
Categories: C#
Post Information: Permalink | Comments (0) | Post RSSRSS comment feed

Factory Pattern using Generics in C#

I've been on a kick lately of just coding various things that I don't need right now but may need at some point in the future. One of those items is generic class to use in a factory pattern. For those of you who don't know what the factory pattern is, it's a creational pattern that creates objects based on an input without the caller necessarily knowing what type the object will be. Basically I need a class that can create other objects based on the input and sending back the object cast to an interface or base class (usually). It's fairly common in things like messaging systems, test driven development, frameworks, and the like.

Anyway, I was bored and thought a class that could handle any such instance like that would be fairly useful. So I set to work creating the class. And with the help of generics and the Func type, it turns out it's fairly easy:

    public class Factory<Key,T>
    {
        #region Protected Variables

        /// <summary>
        /// List of constructors/initializers
        /// </summary>
        protected Dictionary<Key, Func<T>> Constructors = new Dictionary<Key, Func<T>>();

        #endregion

        #region Public Functions

        /// <summary>
        /// Registers an item
        /// </summary>
        /// <param name="Key">Item to register</param>
        /// <param name="Result">The object to be returned</param>
        public void Register(Key Key, T Result)
        {
            if (Constructors.ContainsKey(Key))
                Constructors[Key] = new Func<T>(() => Result);
            else
                Constructors.Add(Key, new Func<T>(() => Result));
        }

        /// <summary>
        /// Registers an item
        /// </summary>
        /// <param name="Key">Item to register</param>
        /// <param name="Constructor">The function to call when creating the item</param>
        public void Register(Key Key, Func<T> Constructor)
        {
            if (Constructors.ContainsKey(Key))
                Constructors[Key] = Constructor;
            else
                Constructors.Add(Key, Constructor);
        }

        /// <summary>
        /// Creates an instance associated with the key
        /// </summary>
        /// <param name="Key">Registered item</param>
        /// <returns>The type returned by the initializer</returns>
        public T Create(Key Key)
        {
            if (Constructors.ContainsKey(Key))
                return Constructors[Key]();
            return default(T);
        }

        #endregion
    }

Before we talk about the methods, you should note that the class requires two types to be defined. The first is the key that the constructor will be associated with (when you call Create, you'll need to pass this in). The second item is the actual type that you expect from the function that is called. So once you create a Factory object, you can call basically just two methods. The first is the Register method. Calling that allows you to, well, register a constructor with a key. For instance:

TempFactory.Register("Temp1", new Func<ITemp>(() => new Temp1()));

This would register the string "Temp1" with the Temp1 constructor. This of course assumes that the key is a string and the type you're expecting is an ITemp and that Temp1 inherits from ITemp... But assuming all of that, it's fairly simple to grasp. Although if you're curious what a Func is, it's a delegate for a method that has no input but returns an object of a specified type.  Anyway, the second method is the Create method. It takes in a key and spits out an object (assuming the key exists, otherwise it spits out the default value). That's pretty much it. With this, you have yourself a light weight, generic class to handle your factory pattern needs. So try it out, leave feedback, and happy coding.

kick it on DotNetKicks.com   Shout it
Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkListEmail

Posted by: James Craig
Posted on: 8/24/2009 at 12:22 PM
Tags: , , ,
Categories: C#
Post Information: Permalink | Comments (0) | Post RSSRSS comment feed

Extracting an Icon From a File

I had a function a while back that would enumerate images within a DLL and another that would extract one. It was never really all that useful but it did give me a couple hours of fun searching through various dlls. This was back in the days when everyone was trying to learn C++ (and yes, was written in C++). So why does this little story matter? Well I was bored today and wondered how easy it would be to do a similar thing in C#. Which led me to something even more useful. Extracting the icon associated with a file type:

        /// <summary>
        /// Extracts an icon associated with a file
        /// </summary>
        /// <param name="FileName">File to extract the icon from</param>
        /// <returns>Returns the extracted icon</returns>
        public static Bitmap ExtractIcon(string FileName)
        {
            return System.Drawing.Icon.ExtractAssociatedIcon(FileName).ToBitmap();
        }

That's it. One line of code and you can get the icon associated with a file. I remember doing something similar took me a couple hundred lines... And I had to walk up hill to the punch card reader... Both ways... Kids today have it easy. Although no idea why I used punch cards considering I'm in my 20s... Anyway, I hope you can find some use for that bit of code. So try it out, leave feedback, and happy coding.

kick it on DotNetKicks.com   Shout it
Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkListEmail

Posted by: James Craig
Posted on: 8/19/2009 at 3:58 PM
Tags: , ,
Categories: C#
Post Information: Permalink | Comments (0) | Post RSSRSS comment feed