Support Forums
[C#]StringList class - Printable Version

+- Support Forums (https://www.supportforums.net)
+-- Forum: Categories (https://www.supportforums.net/forumdisplay.php?fid=87)
+--- Forum: Coding Support Forums (https://www.supportforums.net/forumdisplay.php?fid=18)
+---- Forum: Visual Basic and the .NET Framework (https://www.supportforums.net/forumdisplay.php?fid=19)
+---- Thread: [C#]StringList class (/showthread.php?tid=17994)



[C#]StringList class - BaussHacker - 04-14-2011

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];
                        }
                    }
                }
            }
        }
    }



RE: [C#]StringList class - cfillion - 04-14-2011

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.


RE: [C#]StringList class - BaussHacker - 04-15-2011

(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


RE: [C#]StringList class - KoBE - 04-15-2011

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.