2010-09-05 91 views
3

这个程序有错误,任何人都可以解决这个问题吗?索引器的c#程序

错误是:TempRecord已经定义了一个名为成员“本”使用相同的参数看重

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

     } 
    } 
} 

回答

0

什么你想要做的是有2个索引使用相同的参数和不同返回类型。这在C#中是不合法的。你需要它们中的至少一个移动到一个功能

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

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

您有一个名为this两个成员,即采取同样的参数。这在C#(或其他.Net语言,就我所知而言)中是不允许的。

如果两个成员返回不同的类型,你会认为你可以做到这一点,就像你的做法一样。但是这会给编译器带来模棱两可的印象。如果你有这样的代码,会调用哪个成员?

object x = tempRecord[3]; 

使一个或两个索引器成为一种方法。

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

     } 
    } 
} 

如果你正在尝试类似的功能超载一些概念,我想说它永远不会只用在返回类型的变化工作。数据成员的情况类似,您尝试使用this具有相同的参数但返回类型不同。

然而,最好的方法是为上面正在执行的排他性事件制作单独的功能(即使为了可读性)。

我删除了上面的第二个数据成员,用类似foll的东西替换它。我认为你最好使用temprecord.d[index Value]来访问&使用main从成员d。

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

       set 
       { 
        d[index] = value; 
       } 
      }