开发者

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 xm开发者_StackOverflow社区ldoc 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;
    }

}


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 */
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜