Question

Our Class Teacher had given us a Program on Indexers(),When i compiled and Execute the Program in my Computer i got error as

Error:1 Operator '<' cannot be applied to operands of type 'int' and 'method group'

Why i am getting this error??...and Please Explain the Logic of the Program and Why Indexers are used and i am also getting Runtime Error
Index was out of range. Must be non-negative and less than the size of the
collection. Parameter name: index

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Collections;
    namespace ConsoleApplication2
    {
        class Program
        {
            ArrayList array = new ArrayList();
            public object this[int index]
            {
                get
                {
                    if (index < 0 || index >= array.Count)
                    {
                        return (null);
                    }
                    else
                    {
                        return (array[index]);
                    }
                }
                set
                {
                    array[index] = value;
                }
            }
        }
        class Demo
        {
            public static void Main()
            {
                Program p = new Program();
                p[0] = "123";
                p[1] = "abc";
                p[2] = "xyz";
                for (int i = 0; i <p.Count ; i++)
                {
                    Console.WriteLine(p[i]);
                }
            }
        }
    }
Was it helpful?

Solution

It's failing because the compiler could not find a property named Count. Instead, it found a method—either one which isn't shown here, or if Program implements IEnumerable<object>, then it's likely Linq's Count extension method.

Try adding a Count property to Program

class Program
{
    ...

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

That will solve the compiler error. Now, if you want to know why it's using indexers… well I suppose because your teacher wanted to illustrate how they can be used. Indexers are just a bit of syntactic sugar which just make writing code like p.GetItem(i) look cleaner, p[i].

OTHER TIPS

I do not see a Count implementation in you Program. Add the Count implementation and try to re-compile.

    class Program
    {
        ArrayList array = new ArrayList();

        public int Count { get { return array.Count; } }

        public object this[int index]
        {
            get
            {
                if (index < 0 || index >= array.Count)
                {
                    return (null);
                }
                else
                {
                    return (array[index]);
                }
            }
            set
            {
                array[index] = value;
            }
        }
    }

You should add a Count Property and a constructor that initializes your ArrayList to the correct size, like this:

class Program
{
    ArrayList array = null;

    public Program(int size)
    {
        array = new ArrayList(size);
    }

    public object this[int index]
    {
        get
        {
            if (index < 0 || index >= array.Count)
            {
                return (null);
            }
            else
            {
                return (array[index]);
            }
        }
        set
        {
            array[index] = value;
        }
    }

    public int Count
    {
        get
        {
            return array.Count;
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top