Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[C#]StringList class
#1
The class is a string array class.
You can add and remove strings in the array called Items.
It can be very useful, when you're working with string arrays, however I would suggest using dicts.

Code:
public class StringList
    {
        public StringList(int Length)
        {
            Items = new string[Length];
        }
        public string[] Items;
        public void Add(string Item)
        {
            for (int i = 0; i < Items.Length; i++)
            {
                if (Items[i] == null)
                {
                    if (!Items.Contains(Item))
                    {
                        Items[i] = Item;
                        i = Items.Length + 1;
                    }
                }
            }
        }
        public void Add(byte[] BytesToDecode, params char[] separator)
        {
            string[] items = Encoding.ASCII.GetString(BytesToDecode).Split(separator);
            for (int y = 0; y < items.Length; y++)
            {
                string Item = items[y];
                for (int i = 0; i < Items.Length; i++)
                {
                    if (Items[i] == null)
                    {
                        if (!Items.Contains(Item))
                        {
                            Items[i] = Item;
                            i = Items.Length + 1;
                        }
                    }
                }
            }
        }
        public void Add(string[] ItemsToAdd)
        {
            for (int y = 0; y < ItemsToAdd.Length; y++)
            {
                for (int i = 0; i < Items.Length; i++)
                {
                    if (Items[i] == null)
                    {
                        if (!Items.Contains(ItemsToAdd[y]))
                        {
                            Items[i] = ItemsToAdd[y];
                            i = Items.Length + 1;
                        }
                    }
                }
            }
        }
        public void Remove(string Item)
        {
            string[] items = new string[Items.Length];
            int y = 0;
            for (int i = 0; i < Items.Length; i++)
            {
                if (Items[i] == Item)
                {
                    Items[i] = null;
                }
                else if (Items[i] != null)
                {
                    if (Items[i].Length > 0)
                    {
                        items[y] = Items[i];
                        y++;
                    }
                }
            }
            Items = new string[Items.Length];
            for (int u = 0; u < Items.Length; u++)
            {
                if (Items[u] == null)
                {
                    if (items[u] != null)
                    {
                        if (items[u].Length > 0)
                        {
                            Items[u] = items[u];
                        }
                    }
                }
            }
        }
    }
Reply
#2
I think you dont know the List<string> class which is simpler to use.
But good work. Thumbsup

P.S. Please throw an exception if the added entry exceeds the list's limits.
Reply
#3
(04-14-2011, 04:52 PM)cfillion Wrote: I think you dont know the List<string> class which is simpler to use.
But good work. Thumbsup

P.S. Please throw an exception if the added entry exceeds the list's limits.

I do know abut List<string>, but this is my own class for working with stringarrays. Victoire
Reply
#4
I think it would look A LOT better if you commented your end brackets.. but the code looks pretty good. I didn't look at it entirely but thanks for the share.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)