2010-09-05 88 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; 
       } 
      }