开发者

c# program of Indexers

开发者 https://www.devze.com 2023-01-14 06:21 出处:网络
there is an error in this program.can anyone fix that? Error is :TempRecord already defines a member called 开发者_Python百科\'this\' with the same parameters value

there is an error in this program.can anyone fix that?

Error is :TempRecord already defines a member called 开发者_Python百科'this' with the same parameters value

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication6
{
    class TempRecord
    {
        // Array of temperature values
        private float[] temps = new float[10] { 56.2F, 56.7F, 56.5F, 56.9F, 58.8F, 
                                            61.3F, 65.9F, 62.1F, 59.2F, 57.5F };
        private int[] d= new int[10]{4,5,5,4,4,43,2,2,5,3};
        // To enable client code to validate input 
        // when accessing your indexer.
        //public int Length
        //{
        //    get { return temps.Length; }
        //}
        // Indexer declaration.
        // If index is out of range, the temps array will throw the exception.
        public float this[int index]
        {
            get
            {
                return temps[index];
            }

            set
            {
                temps[index] = value;
            }
        }
        public int this[int index]//error:TempRecord already defines a member called 'this' with the same parameters value
        {
            get
            {
                return d[index];
            }

            set
            {
                d[index] = value;
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            TempRecord tempRecord = new TempRecord();
            // Use the indexer's set accessor
            tempRecord[3] = 58.3F;
            tempRecord[5] = 60.1F;



            // Use the indexer's get accessor
            for (int i = 0; i < 10; i++)
            {
                System.Console.WriteLine("Element #{0} = {1}", i, tempRecord[i]);
            }
            Console.WriteLine(tempRecord[2]);
            // Keep the console window open in debug mode.
            System.Console.WriteLine("Press any key to exit.");
            System.Console.ReadKey();

        }
    }
}


You have two members named this, that take the same parameters. That's not allowed in C# (or other .Net languages, as far as I'm aware).

You'd think you'd be able to do this if both members return different types, as yours do. But that would present the compiler with ambiguity. Which member would be called if you had code like this?

object x = tempRecord[3];

Make one or both indexers a method.


What you're trying to do is have 2 indexers with the same parameters and different return types. This is not legal in C#. You'll need to move at least one of them into a function

public int GetD(int index) {
  return d[index];
}

public void SetD(int index, int value) {
  d[index] = value;
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication6
{
    class TempRecord
    {
        // Array of temperature values 
        private float[] temps = new float[10] { 56.2F, 56.7F, 56.5F, 56.9F, 58.8F, 61.3F, 65.9F, 62.1F, 59.2F, 57.5F }; private int[] d = new int[10] { 4, 5, 5, 4, 4, 43, 2, 2, 5, 3 };

        public int Length //
        {
            get { return temps.Length; }
        }
        public float this[int index]
        {
            get { return temps[index]; }

            set { temps[index] = value; }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            TempRecord tempRecord = new TempRecord();
            tempRecord[3] = 58.3F;
            tempRecord[5] = 60.1F;


            for (int i = 0; i < 10; i++)
            {
                System.Console.WriteLine("Element #{0} = {1}", i, tempRecord[i]);
            }
            Console.WriteLine(tempRecord[2]);
            System.Console.WriteLine("Press any key to exit.");
            System.Console.ReadKey();

        }
    }
}

If you are trying some concept similar to overloading of functions, I'd like to say it never works with just a change in return type. Similar is the case of data members, where you have tried to use this with the same arguments but different return types.

The best method would be however (even for readability sake) making separate functions for the exclusive events that are being performed above.

I deleted the second data member above, replace it with the something like the foll. I think you better use temprecord.d[index Value] to access & use the member d from main.

public int d[int index]
            {
                get
                {
                    return d[index];
                }

                set
                {
                    d[index] = value;
                }
            }
0

精彩评论

暂无评论...
验证码 换一张
取 消