2014-07-22 54 views
0

我已經繼承了一些代碼,並且當我嘗試運行代碼時遇到了上述錯誤消息。以下是代碼:C#索引器必須至少有一個參數

using System.Runtime.CompilerServices; 
using System.Runtime.InteropServices; 
namespace Scripting 
{ 
     [CompilerGenerated] 
     [Guid("C7C3F5A0-88A3-11D0-ABCB-00A0C90FFFC0")]  
     [TypeIdentifier]  
     [ComImport]  
     public interface IDrive  
     { 
      [DispId(0)]  
      [IndexerName("Path")]  
      string this[] { [DispId(0)] get; } //The error is here//  

      [DispId(10009)] 
      int SerialNumber { [DispId(10009)] get; } 

      [DispId(10007)] 
      string VolumeName { [DispId(10007)] get; [DispId(10007)] set; } 

      [SpecialName] 
      [MethodImpl(MethodCodeType = MethodCodeType.Runtime)] 
      void _VtblGap1_7(); 

      [SpecialName] 
      [MethodImpl(MethodCodeType = MethodCodeType.Runtime)] 
      void _VtblGap2_1(); 
     } 
} 

我是C#的新手,想知道缺少哪個參數。

我無法問原始編碼器。任何幫助將不勝感激。

+0

你能指出這個錯誤發生的行號嗎 – sunbabaphu

+0

@sunbabaphu他做到了。 –

+0

對,嗯.... – sunbabaphu

回答

3

就像錯誤說的那樣,「索引器必須至少有一個參數」。

因此,您需要向索引器添加一個參數,例如,

string this[int index] { [DispId(0)] get; } 

如果你想想看,當你使用索引你提供一個整數作爲參數。

例如

string path = myIDrive[0]; // Use the integer parameter to access the element 

var wut = myIDrive[?]; // without any parameter, how would you get the Path data? 
+0

謝謝你做的伎倆。 – Swagman9203

2
string this[] { [DispId(0)] get; } 

的錯誤說你缺少的參數。

string this[object myIndexerParameter] 
{ 
    get 
    { 
     // return some value based on the parameter passed. 
    } 
} 

然後調用它像這樣:var something = myIDriveInstance[myIndexValue];

http://msdn.microsoft.com/en-us/library/6x16t2tx.aspx

這基本上是讓List<T>允許的項目中通過索引同樣的事情;因此名稱爲indexer

+2

+1 rtfm並鏈接到tfm –

相關問題