类错误 - 方法需要对象引用

时间:2014-01-20 15:10:09

标签: c#

我目前正在学习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;
    }

}
}

2 个答案:

答案 0 :(得分:6)

MaxBox的方法不是静态的,你需要它的一个实例。

MaxBox maxBox = new MaxBox();

在主课的开头。然后

maxBox.DisplayStr(....)

另外,为了让你思考,你可以替换:

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

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

答案 1 :(得分:4)

方法PlayAgainDisplayStrMaxBox类型的实例方法。要调用它们,您需要一个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