1

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
  • Not related to the question at hand, but I would change the name Ilist. I[Something] implies that it is an interface, which it is not. It also looks far too similar to the existing interface IList. Commented Feb 1, 2009 at 13:51
  • msdn.microsoft.com/en-us/library/ms229043.aspx should help with understanding some of the naming conventions in the language. Commented Feb 1, 2009 at 14:03

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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);

Comments

0

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();

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.