fieldingmellish
fieldingmellish

Reputation: 1483

Thought I understood static classes

Trying to construct a helper class that will return an arraylist, but I'm getting the following error, having to do with the xml document I need to create:

Util.oDocument': cannot declare instance members in a static class

I think I understand why you wouldn't want to create a new xmldoc object each time this method gets called, but I need that doc in there for the functionality. How should I be approaching this?

using System;
using System.Collections;
using System.Xml;

public static class Util
{

    public static ArrayList multipleArtistList(string artistName)
    {
        XmlDocument oDocument = new XmlDocument();

        string uri = "http://api.leoslyrics.com/api_search.php?auth=duane&artist=" + artistName;
        oDocument.Load(uri);

        XmlNodeList results = oDocument.GetElementsByTagName("name");
        ArrayList artistList = new ArrayList();

        for (int i = 0; i < results.Count; i++)
        {
            if (!artistList.Contains(results[i].InnerText))
            {
                artistList.Add(results[i].InnerText);

            }

        }

        return artistList;
    }

}

Upvotes: 1

Views: 199

Answers (1)

user47322
user47322

Reputation:

This error here:

Util.oDocument: cannot declare instance members in a static class

means that you've declared oDocument outside of the method.

There's nothing wrong with the code you posted, in fact the error and the code contradict each other.

Make sure that oDocument is declared inside the method. If you want to declare it as a field, make sure to give it the static modifier, like so:

public static class Util
{
    static XmlDocument oDocument;

    /* code */
}

Upvotes: 4

Related Questions