2011-11-28 87 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"); 
    } 
}