Tyler W
Tyler W

Reputation: 472

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. It's basically just a data holding class.

I have a file with xml like delimiters(begging and ending tags of some sort, and data value that goes in between)

I have a parse class, that detects and adds certain things to certain lists from the data holding class. Basically I'm trying to detect an opening tag, store it in the opening tag List in my data holding 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. Then after the Parse class is finished, back in main, I'm calling up methods inside the data class to display the data.

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

"An object reference is required for the non-static field, method, or property"

How can I set the data reading classes List's from my parse 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)

This is all extra credit for a basic programming class I'm in.(the normal version is all structural inside one 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

    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....

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)

Upvotes: 0

Views: 1476

Answers (2)

Jon Skeet
Jon Skeet

Reputation: 1500515

EDIT: Okay, it looks like you just need to pass the reference to the object around, e.g.

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.

I would recommend that you don't use public fields in XmlDoc, by the way. Public fields violate encapsulation - use properties if you really need to just expose values, but ideally put more behaviour in the object itself.

Upvotes: 2

verdesmarald
verdesmarald

Reputation: 11866

The error message is pretty good here: "An object reference is required for the non-static field, method, or property"

You are trying to call an instance method in a static fashion, you have two options:

  1. Make the method static.
  2. Instantiate the class and call the method from the instance.

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();

Upvotes: 1

Related Questions