简体   繁体   English

C# 需要传递一个 object 所以另一个 class 可以调用它的方法来更新它

[英]C# need to pass an object so another class can call it's methods to update it

I have a class that has 3 public List's in it.我有一个 class,其中有 3 个公共列表。 It's basically just a data holding class.它基本上只是一个包含 class 的数据。

I have a file with xml like delimiters(begging and ending tags of some sort, and data value that goes in between)我有一个带有 xml 的文件,例如分隔符(某种类型的乞求和结束标签,以及介于两者之间的数据值)

I have a parse class, that detects and adds certain things to certain lists from the data holding class.我有一个解析 class,它从保存 class 的数据中检测并将某些内容添加到某些列表中。 Basically I'm trying to detect an opening tag, store it in the opening tag List in my data holding class.基本上我正在尝试检测一个开始标签,将它存储在我保存 class 的数据中的开始标签列表中。

The way I'm trying to go (based on the teachers poor excuse for an example) is Main() instantiates the data holding class into an object, likewise for the parse class. The way I'm trying to go (based on the teachers poor excuse for an example) is Main() instantiates the data holding class into an object, likewise for the parse class. Then it calls ParseMain to parse through the file and separate tags, data values, and closing tags to their respective List inside the data holding class.然后它调用 ParseMain 来解析文件并将标记、数据值和结束标记分离到它们各自的列表中,这些列表位于包含 class 的数据中。 Then after the Parse class is finished, back in main, I'm calling up methods inside the data class to display the data.然后在 Parse class 完成后,回到 main,我正在调用数据 class 中的方法来显示数据。

I'm basically being yelled at by the compiler because, even though the xml data holder class has been instantiated in main, it doesn't or can't add to the public List's I get specifically this error我基本上被编译器大喊大叫,因为即使 xml 数据保持器 class 已在 main 中实例化,它没有或不能添加到公共列表中,我特别得到了这个错误

"An object reference is required for the non-static field, method, or property" “非静态字段、方法或属性需要 object 引用”

How can I set the data reading classes List's from my parse class?如何从我的解析 class 中设置数据读取类列表?

My teacher gave a horrible example (he had everything but the classes marked static, and basically just slapped java/c++ style code into a couple classes)我的老师举了一个可怕的例子(除了标记为 static 的类之外,他什么都有,基本上只是将 java/c++ 样式代码拍成了几个类)

This is all extra credit for a basic programming class I'm in.(the normal version is all structural inside one class)这是我参与的基本编程 class 的所有额外功劳。(普通版本都是一个类中的所有结构)

***Edit- adding code snippets ***编辑-添加代码片段

                    XMLDoc XMLData = new XMLDoc();
                    XMLParse parseXML1 = new XMLParse();


                    //Calls the parseXML1 method passing it
                    //the name of the currently opened file.
                    parseXML1.MainParse(fileIn);

then to my main parse然后到我的主要解析

    public void MainParse(FileStream fileIn)
    {

        int byteIn;


        while ((byteIn = fileIn.ReadByte()) != -1)
        {

            if (byteIn == '<')
            {
                ParseElement(ref byteIn,fileIn);
            }

ParseElement looks like ParseElement 看起来像

    public void ParseElement(ref int byteIn,FileStream fileIn)
    {
        token += byteIn;
        //First I need to get the entire tag stored in token
        do
        {        
            byteIn = fileIn.ReadByte();
            token += byteIn;
        } while (byteIn != '>');
        token += '>';

        //now I insert a / into my compare token
        compareToken = token;
        compareToken.Insert(1,"/");

        //It's an ending tag
        if (token == compareToken)
        {
            //figure this out later
        }
        //It's an opening tag,store it into element list
        else
        {
            XMLDoc.elements.Add(token);
            //also tried XMLData/elements/Add(token)
        }

    }

and finally my XMLDoc class looks like....最后我的 XMLDoc class 看起来像....

class XMLDoc
{
    public List<string> elements;
    public List<string> dataValues;
    public List<string> endingElements;

    public XMLDoc()
    {
        elements = new List<string>();
        dataValues = new List<string>();
        endingElements = new List<string>();
    }

    //This method simply displays the contents of the arrays
    public void DisplayCollected()
    {
        Console.WriteLine("Begin unformatted dump");
        for (int ii = 0; ii <= elements.Count;ii++)
        {
            Console.WriteLine("\n"+elements[ii]+dataValues[ii]+
                "\n"+endingElements[ii]);
        }
        Console.WriteLine("End unformatted dump\n");
    }

    //This method will generate an xml style display
    //To add later

}

I'm playing around,learning by doing and such.我在玩,边做边学等等。 I've at this point had to abandon the teachers example, as mentioned above he just made every method in every class static, probably because he slapped his example together from the main lab work(which is structural and all inside the first class)在这一点上我不得不放弃老师的例子,如上所述,他只是在每个 class static 中制作了每个方法,可能是因为他从主要实验室工作中将他的例子放在一起(这是结构性的,都在第一堂课内)

EDIT: Okay, it looks like you just need to pass the reference to the object around, eg编辑:好的,看起来你只需要传递对 object 的引用,例如

XmlDoc xmlData = new XmlDoc();
XmlParser parser = new XmlParser();
parser.MainParse(xmlData, fileIn)

...

public void MainParse(XmlDoc xmlData, FileStream fileIn)
{
    ...
    ParseElement(xmlData, ref byteIn, fileIn);
    ...
}

public void ParseElement(XmlDoc xmlData, ref int byteIn,FileStream fileIn)
{
    ...
}

I've adjusted the names slightly to be more sensible IMO.我已经稍微调整了名称以更明智的 IMO。

I would recommend that you don't use public fields in XmlDoc , by the way.顺便说一句,我建议您不要XmlDoc中使用公共字段。 Public fields violate encapsulation - use properties if you really need to just expose values, but ideally put more behaviour in the object itself.公共字段违反封装 - 如果您确实需要公开值,请使用属性,但理想情况下将更多行为放在 object 本身中。

The error message is pretty good here: "An object reference is required for the non-static field, method, or property"此处的错误消息非常好: “非静态字段、方法或属性需要一个 object 引用”

You are trying to call an instance method in a static fashion, you have two options:您正在尝试以 static 方式调用实例方法,您有两种选择:

  1. Make the method static.制作方法 static。
  2. Instantiate the class and call the method from the instance.实例化 class 并从实例调用方法。

For example:例如:

public class Foo()
{
    public void Frob() {}
}

You cannot do this:你不能做这个:

Foo.Frob();

But you can do this:但是你可以这样做:

var foo = new Foo();
foo.Frob();

or this:或这个:

public class Foo()
{
    public static void Frob() {} // Note static
}

[...]

Foo.Frob();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM