views:

541

answers:

4

These Codes really boring. And Tostring() give me error !!! Can you rearrange these codes ?


  class Program
    {
        static void Main(string[] args)
        {
            string[] arraystr = { "yusuf", "mehmet" };
            Ilist myitems = new Ilist(arraystr);

            SelectedItemsList slist = new SelectedItemsList();
            slist.Items.Add(myitems);
            Console.Write(slist.Items[0].ToString());
            Console.ReadKey();
        }
    }
    public class Ilist
    {
        private string[] Ad;

        public Ilist(string[] ad)
        {
            Ad = ad;
        }
        public override string[] ToString()
        {
            return this.Ad;
        }
    }

    public class SelectedItemsList
    {
        public List<Ilist> Items;

        public SelectedItemsList()
        {
            Items = new List<Ilist>();

        }
    }

error : 'Generics_List_with_Class.Ilist.ToString()': return type must be 'string' to match overridden member 'object.ToString()'

+2  A: 

Rename Ilist.ToString() to ToStringArray(). All objects have a ToString() method but you're overriding it with a function with a different return type, causing your error.

IList isn't a good name for a class, because convention dicates that names starting with "I" should be interfaces. I recommend Ilist should look more like this:

public class StringList
{
    private string[] Ad;

    public StringList(string[] ad)
    {
     Ad = ad;
    }

    public string[] ToStringArray()
    {
     return this.Ad;
    }

    public override ToString()
    {
     return string.Join(",", Ad);
    }
}

To be honest though, I recommend you ditch this whole approach and look into using List instead.

Neil Barnwell
Thanks but can you clarify your advice please with sample? PLease ...
Phsika
A: 

If you're using C# 3.0 you can use the extension methods in the System.Linq namespace to make it easy:

IList<string> list = new List<string>();
list.Add("item1");
list.Add("item2");
string[] array = list.ToArray();
Erik van Brakel
A: 

Your ToString function returns a string array, whereas the method it overrides from object should return a single string.

Also, naming your class IList doesn't follow conventional design patterns of naming classes with "Proper" names (like StringList), and prefixing interfaces with an I.

It is also probably worth having a List rather than a string array as Ad.

ck
+2  A: 

The method you are overriding has a return type of string. If you override a method, it's method signature (it's return type, name and arguments) should remain the same (well, there are cases where it can be different, but for now assume they should be the same). So your ToString() method must look like this:

public override string ToString()
{
    ...
}

It's up to you to decide what the best string representation is, but if you want to use ToString() it must return a string.

As Neil Barnwell suggests if you actually just want to return an array you could rename your current method to something like:

public string[] GetItems()
{
    return Ad;
}

or if you do want a string you could make your ToString method something like this:

public override string ToString()
{
    StringBuilder stringBuilder = new StringBuilder();
    stringBuilder.Append("{ ");
    stringBuilder.Append(string.Join(", ", Ad));
    stringBuilder.Append(" }");
    return stringBuilder.ToString();
}

Depending on whether you are doing this to learn C# or whether it's actual code, I would look at:

List<string> list = new List<string>()
{
    "yusef",
    "mehmet",
};

if you are using C# 3.0, or if not:

List<string> myitems = new List<string>();
myitems.AddRange(arraystr);
ICR
Thanks Alot !!!! it is really help me!
Phsika