2011-11-28 67 views
1

我是C#的新手,並且在獲取有關歧義的錯誤時遇到了問題。請讓我知道需要更正的內容。c#模糊錯誤

public class JessiahP3 
{ 
    boolean isPlaying = false; 
    int strings = 1; 
    boolean isTuned = false; 
    public String instrumentName; 

    //is tuned 
    public void isTuned() 
    { 
     isTuned = true; 
     System.out.println("Currently tuning " + getInstrumentName()); 
    } 

    //not tuned 
    public void isNotTuned() 
    { 
     isTuned = false; 
     System.out.println(getInstrumentName() + " is not tuned"); 
    } 
} 
+2

什麼是精確的錯誤信息? –

+0

如果你說出'ambiguity'錯誤發生在哪裏,好友會有所幫助。 – Strelok

+3

'System.out.println'來自Java。這不是C#代碼。 –

回答

6

您有一個名爲isTuned的變量和函數。

+0

錯誤說JessiahP3.isTuned和JessiahP3.isTuned()之間的ambuguity。 @DBM我改變了println語句,那裏沒有錯誤。只有含糊不清。 –

+0

扔我們一塊骨頭,接受一些答案。 :) – BNL

1

你有一個領域和具有相同簽名的方法。見isTuned

+0

問題解決了。感謝大家的意見。 –

4

可能我建議以下更習慣C#。

  1. 使用屬性而不是公共字段。
  2. 適當時,首選自動獲取/設置屬性。
  3. 屬性名稱應以大寫
  4. 開始明確指定知名度

-

public class JessiahP3 
{ 
    private int strings = 1; 
    public string InstrumentName { get; set; } 
    public boolean IsPlaying { get; set; } 
    public boolean IsTuned { get; set; } 
} 
1

我在這裏看到三個明顯的錯誤。

  1. 你必須同時用作變量,並且在同一類型中的一個方法名稱isTuned
  2. System.out.println將需要爲Console.WriteLine
  3. boolean應該是bool(或Boolean

話雖這麼說,在C#中,這往往會被(不斷變化getInstrumentName()InstrumentName財產一起)完成作爲一個單一的屬性:

bool isTuned = false; 

bool IsTuned 
{ 
    get { return isTuned; } 
    set 
    { 
     this.isTuned = value; 
     Console.WriteLine(isTuned ? "Currently tuning " + this.InstrumentName : this.InstrumentName + " is not tuned"); 
    } 
}