2014-01-20 31 views
1

我目前正在學習C#,並且正在嘗試爲接下來的幾周準備課程,其中將介紹類和方法。爲此,我試圖建立一個名爲MaxBox的類,它是一個普通的實用類,我可以像'顯示字符串'或'再次播放'一樣存儲一些常規函數。我已經構建了我的主文件(Program)和我的類文件(MaxBox),並且行#23,#28和#59返回了相同的一般錯誤'非靜態字段,方法或屬性需要對象引用' program.MaxBox.DisplayStr(字符串)」。 #57返回一個類似的錯誤'非靜態字段,方法或屬性需要對象引用'program.MaxBox.PlayAgain()'類錯誤 - 方法需要對象引用

我真的是一個全新的,我正在摔跤對象,我已經做了一些研究,讓自己這麼遠,但我不明白這門語言,但仍然能夠理解我讀過的資源是在說我想解決這個錯誤。非常感謝幫助和指導。我還在第一週,真的什麼都不知道。

計劃:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading; // needed for close 
using System.Threading.Tasks; 

namespace a020_Breakcase_MeaningOfNames_C 
{ 
class Program 
{ 
    public void Play() 
    { 
     /* 
     * Conditionals - Use switch/Case statement too: 
     * Evaluate user data (name) 
     * Return meaning of name evaluated 
     * OR 
     * Return 'Name not found' error message 
     * Say goodbye 
     */ 

     MaxBox.DisplayStr("What's in a name? Let's find out!"); 
     Console.Write("\n\n"); 

     do 
     { 
      MaxBox.DisplayStr("Enter Name: "); 

      string uName = Console.ReadLine().ToLower(); 
      switch (uName) 
      { 
       case "doyle": 
        Console.WriteLine("Doyle means descendant of Dubhghalle"); 
        break; 
       case "fiona": 
        Console.WriteLine("Fiona is considered to be a Latinised form of the Gaelic word fionn, meaning \"white\", \"fair\"."); 
        break; 
       case "hunter": 
        Console.WriteLine("Hunter means to search with purpose"); 
        break; 
       case "liam": 
        Console.WriteLine("This name is a short form of the Irish name Uilliam (William) which is now use independently as a given name. As a Hebrew name, Liam means \"my people; I have a nation\"."); 
        break; 
       case "max": 
        Console.WriteLine("Short for of Maximilian, Maxwell, and the various name using it as a first syllable."); 
        break; 
       case "portia": 
        Console.WriteLine("It is of Latin origin. Feminine form of a Roman clan name. Portia was used by Shakespeare as the name of a clever, determined young heroine in \"The Merchant of Venice\" who disguises herself as a lawyer to save her husband's life."); 
        break; 

       default: 
        Console.WriteLine("I'm sorry but I don't know the meaning of the name " + uName + "."); 
        break; 
      } 

     } while (MaxBox.PlayAgain()); 

     MaxBox.DisplayStr("C#eers!"); 
    } 


    static void Main(string[] args) 
    { 
     Program myProgram = new Program(); 
     myProgram.Play(); 

     Console.Read(); 
    } 
} 
} 

我的班級:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace a020_Breakcase_MeaningOfNames_C 
{ 
class MaxBox 
{ 
    /* 
    * MaxBox is my general functions class 
    * Contains 
    * DisplayStr() - Display the string given 
    * PlayAgain() - If True, runs program again 
    */ 

    public String uName; 
    public String command; 

    public void DisplayStr(String StrTxt) 
    { Console.Write(StrTxt); } 

    public Boolean PlayAgain() 
    { 
     Console.Write("\n\nDo you want to play again? (y)es or (n)o: "); 
     String command = Console.ReadLine().ToLower().Trim(); 

     if (command == "y" || command == "yes") return true; 
     return false; 
    } 

} 
} 
+0

使用str1.Equals(STR2)測試字符串相等 – Plue

+1

您應該實例'MaxBox'類,或將其更改爲靜態 –

回答

6

MaxBox的方法不是靜態的,你需要它的一個實例。

MaxBox maxBox = new MaxBox(); 

在您的主類的開始。然後

maxBox.DisplayStr(....) 

還,只是爲了讓你思考,你可以替換:

if (command == "y" || command == "yes") return true; 
    return false; 

return (command == "y" || command == "yes"); 
+0

可能看起來像一個小細節,但MaxBox'是否爲'static'並不重要,只是他試圖訪問的成員不是。 – TypeIA

+0

你是對的我在那裏輸入的太快了 – Jonesopolis

+1

什麼是靜態?爲什麼他需要一個實例,如果它不是靜態的?如果你想知道 - 請參閱http://msdn.microsoft.com/en-us/library/98f28cdx.aspx –

4

的方法PlayAgainDisplayStr是該類型MaxBox實例方法。爲了給它們打電話,你需要一個MaxBox的實例來調用它們。現在你正試圖通過類型名稱只適用於靜態方法

MaxBox.DisplayStr("hello world"); // wrong 
MaxBox mb = new MaxBox(); 
mb.DisplayStr("hello world"); // right 

它可以定義這樣的方法,你可以從類型名稱調用它們給他們打電話。但這樣做需要他們被標記爲static

class MaxBox { 
    public static void DisplayStr2(string str){ ... } 
} 

MaxBox.DisplayStr2("hello world"); // right 
+0

感謝您的幫助 - 非常有幫助的意見,並誠實非常感謝。 – Chezshire

相關問題