2013-02-14 49 views
22

我有一個名爲Pin的類。C#不能將方法轉換爲非委託類型

public class Pin 
{ 
    private string title; 

    public Pin() { } 

    public setTitle(string title) { 
     this.title = title; 
    } 
    public String getTitle() 
    { 
     return title; 
    } 
} 

從另一個類我添加在List<Pin>銷銷對象,並從另一個我想遍歷列表銷和獲得的元素。所以我有這個代碼。

foreach (Pin obj in ClassListPin.pins) 
{ 
    string t = obj.getTitle; 
} 

使用此代碼我無法檢索標題。爲什麼?

(注:ClassListPin僅僅是一個靜態類,其中包含一些元件和這些中的一個,是List<Pin>引腳)

+0

因爲您需要調用它,例如'obj.getTitle()' – leppie 2013-02-14 14:46:38

+0

您是否嘗試過搜索Google?這是一個相當普遍的初學者錯誤。 – antonijn 2013-02-14 14:49:22

+0

稍微偏離主題,但引入了屬性來反對寫作創作者和獲得者。欲瞭解更多信息:http://msdn.microsoft.com/en-us/library/x9fsa0sw%28v=vs.80%29.aspx – Matthew 2013-02-14 15:01:29

回答

43

你需要一個方法調用後添加括號,否則編譯器會認爲你是在談論方法本身(委託類型),而實際上你正在談論該方法的返回值。

string t = obj.getTitle(); 

額外的非基本信息

而且,看看屬性。這樣,你可以使用標題,就好像它是一個變量,而在內部,它就像一個函數。這樣,你就不必寫的功能getTitle()setTitle(string value),但你可以做這樣的:

public string Title // Note: public fields, methods and properties use PascalCasing 
{ 
    get // This replaces your getTitle method 
    { 
     return _title; // Where _title is a field somewhere 
    } 
    set // And this replaces your setTitle method 
    { 
     _title = value; // value behaves like a method parameter 
    } 
} 

或者你可以使用自動實現的屬性,這會在默認情況下使用:

public string Title { get; set; } 

而且你不必創建自己的後臺字段(_title),編譯器會自己創建它。

此外,您還可以更改屬性訪問器(getter和setter)的訪問級別:

public string Title { get; private set; } 

您使用性能,如果他們領域,即:

this.Title = "Example"; 
string local = this.Title; 
+3

噢,我的上帝......這是一個幼稚的錯誤! – gts13 2013-02-14 14:51:31

3

因爲getTitle不是string,它返回一個參考或delegate的方法(如果像),如果你沒有明確請撥打的方法。

打電話給你的方法是這樣的:

string t= obj.getTitle() ; //obj.getTitle() says return the title string object 

然而,這會工作:

Func<string> method = obj.getTitle; // this compiles to a delegate and points to the method 

string s = method();//call the delegate or using this syntax `method.Invoke();` 
2

要執行的方法你需要添加圓括號,即使該方法不帶參數。

所以它應該是:

string t = obj.getTitle(); 
6

getTitle是一個函數,所以你需要把()後。

string t = obj.getTitle(); 
5

如前所述,你需要使用obj.getTile()

但是,在這種情況下,我認爲你正在尋找使用Property

public class Pin 
{ 
    private string title; 

    public Pin() { } 

    public setTitle(string title) { 
     this.title = title; 
    } 

    public String Title 
    { 
     get { return title; } 
    } 
} 

這將允許您使用

foreach (Pin obj in ClassListPin.pins) 
{ 
    string t = obj.Title; 
} 
+0

它應該是'Title',而不是'getTitle'。 – antonijn 2013-02-14 14:52:34

+0

我的不好,修好了。 :) – eandersson 2013-02-14 15:10:07

6

由於@Antonijn說,你需要執行的getTitle方法,通過添加括號:

string t = obj.getTitle(); 

但我想補充,你正在用C#進行Java編程。還有就是properties(雙get和set方法),這應該在這樣的情況下使用的概念:

public class Pin 
{ 
    private string _title; 

    // you don't need to define empty constructor 
    // public Pin() { } 

    public string Title 
    { 
     get { return _title; } 
     set { _title = value; } 
    } 
} 

而且還要多,在這種情況下,你可以要求編譯器不僅爲get和set方法產生,但也爲後面儲存一代,通過auto-impelemented property用法:

public class Pin 
{ 
    public string Title { get; set; } 
} 

現在你不需要執行方法,因爲性質一樣使用領域:

foreach (Pin obj in ClassListPin.pins) 
{ 
    string t = obj.Title; 
} 
4

您可以將您的類代碼簡化爲下面的代碼,它將按原樣運行,但如果您想讓示例工作,請在末尾添加括號:string x = getTitle();

public class Pin 
{ 
    public string Title { get; set;} 
} 
相關問題