2012-10-10 43 views
2

我想在C#中的索引屬性:非整數索引的索引屬性在C#

public Boolean IsSelected[Guid personGuid] 
{ 
    get { 
     Person person = GetPersonByGuid(personGuid); 
     return person.IsSelected; 
    } 
    set { 
     Person person = GetPersonByGuid(personGuid); 
     person.IsSelected = value; 
    } 
} 
public Boolean IsApproved[Guid personGuid] 
{ 
    get { 
     Person person = GetPersonByGuid(personGuid); 
     return person.IsApproved; 
    } 
    set { 
     Person person = GetPersonByGuid(personGuid); 
     person.IsApproved= value; 
    } 
} 

Visual Studio中的非整數索引語法抱怨:

我知道.NET supports non-Integer indexors


在另一種語言,我會寫:

private 
    function GetIsSelected(ApproverGUID: TGUID): Boolean; 
    procedure SetIsSelected(ApproverGUID: TGUID; Value: Boolean); 
    function GetIsApproved(ApproverGUID: TGUID): Boolean; 
    procedure SetIsApproved(ApproverGUID: TGUID; Value: Boolean); 
public 
    property IsSelected[ApproverGuid: TGUID]:Boolean read GetIsSelected write SetIsSelected; 
    property IsApproved[ApproverGuid: TGUID]:Boolean read GetIsApproved write SetIsApproved; 
end; 
+0

'C#不會將索引類型限制爲整數。「 - 從[這裏](http://msdn.microsoft.com/zh-cn/library/2549tw02.aspx)。不知道爲什麼抱怨。 – Oded

+0

您收到的實際錯誤是什麼? – MNGwinn

+1

你在[] –

回答

7

你的語法不正確:

public Boolean this[Guid personGuid] 
{ 
    get { 
     Person person = GetPersonByGuid(personGuid); 
     return person.IsSelected; 
    } 
    set { 
     Person person = GetPersonByGuid(personGuid); 
     person.IsSelected = value; 
    } 
} 

索引器使用的是this關鍵字聲明 - 你不能用自己的名字。

Using Indexers (C# Programming Guide)

要聲明上的類或結構的索引,使用這個關鍵字


另外,只可能有一個接受類型一個索引 - 這是C#的索引器語法的限制(可能是IL限制,不確定)。

+0

這意味着我不能有兩個(例如'IsSelected [Guid personGuid]'和'IsApproved [Guid personGuid]')索引屬性? –

+0

@IanBoyd - 的確如此。索引者的一個侷限性。儘管你可以有一個'IsApproved [string reallyAGUID]'並解析成一個GUID(醜陋的黑客,但你看到我要去哪裏)。 – Oded

+0

@IanBoyd:正確。如果他們具有不同類型的鍵,則可以擁有多個索引器,但不能讓索引按照您希望的方式工作。用'ToggleSelected(Guid personGuid)'和'ToggleApproval(Guid personGuid)'方法或類似的方法會更好。 –

5

索引器只與this關鍵字工作。見here

該關鍵字用於定義索引器。

+0

@AnonyPegram:我已經被證明有足夠的錯誤時間來小心地做出絕對的聲明。 –

+0

我認爲編譯器有辦法關閉整個事情。 –

1

就像馬特Burland和俄德說,索引僅與此關鍵字的工作,所以你需要有你需要的接口的代理類:

public class PersonSelector 
{ 
    private MyClass owner; 

    public PersonSelector(MyClass owner) 
    { 
     this.owner = owner; 
    } 

    public bool this[Guid personGuid] 
    { 
     get { 
      Person person = owner.GetPersonByGuid(personGuid); 
      return person.IsSelected; 
     } 
     set { 
      Person person = owner.GetPersonByGuid(personGuid); 
      person.IsSelected = value; 
     } 
    } 

} 

public class MyClass 
{ 
    public MyClass() 
    { 
     this.IsSelected = new PersonSelector(this); 
    } 

    public PersonSelector IsSelected { get; private set; } 

    ... 
} 
0

@Jojan回答here

C#3.0規範

「索引器的重載允許一個類,結構或接口到 聲明多個索引器,只要它們的簽名是獨特內 該類,結構或接口。「

,或者如果你的數據集是小,你可以IList的

public IList<Boolean> IsSelected 
{ 
    get { ... } 
} 

public IList<Boolean> IsApproved 
{ 
    get { .... } 
} 

或使用Multiple Indexers technique using Interfaces

0

其實你可以有多個索引類型接受。

但是,您不能有兩個具有相同簽名的索引器。相同的簽名意味着參數號碼和類型 - 所以上面的代碼有兩個簽名相同的索引器。

如果代碼更改爲:

Boolean this[string x, Guid personguid] 

和:

Boolean this[Guid personguid] 

它應該工作。