2011-03-09 37 views
9

我越來越想實現在vb.net的接口時,這個錯誤:.NET錯誤:實現屬性必須有匹配的「只讀」或「只寫」符

Public Interface IFoo 
    ReadOnly Property Foo() As String 
End Interface 

Public Class myFoo 
    Implements IFoo 

    Public ReadOnly Property Foo() As String 
    Get 
     return "Foo" 
    End Get 
    End Property 
... 
End Class 

缺少什麼?

+1

只是爲了告知C#可以做一些其他的東西http://stackoverflow.com/questions/6341184/why-cant-interface-readonly-properties-be-overriden-in-vb-net-when-it-is-有效的 – kbvishnu 2013-07-24 11:40:09

回答

19

你將要告訴代碼的myFoo.Foo實現IFoo.Foo(注意添加Implements IFoo.Foo):

Public Interface IFoo 
    ReadOnly Property Foo() As String 
End Interface 

Public Class myFoo 
    Implements IFoo 

    Public ReadOnly Property Foo() As String Implements IFoo.Foo 
     Get 
      Return "Foo" 
     End Get 
    End Property 
End Class 

據我所知,VB.NET不支持以同樣的方式隱式接口的實現像C#一樣。

+1

我一直在堅持這一個,直到偶然發現你的答案。謝謝! – Michael 2011-05-08 14:21:42

+0

所以如果類和接口中的屬性匹配。它不會自動匹配它們呢? – Steve 2012-02-16 14:28:01

+0

@Steve:不,據我所知VB不會爲你做;您需要明確指出類型成員實現接口成員。 – 2012-02-17 14:28:22

相關問題