开发者

Working with property lists

开发者 https://www.devze.com 2023-02-22 04:45 出处:网络
i want开发者_Go百科 to fill some properties. I get the data from a XML File which i read by using LINQ to XML.

i want开发者_Go百科 to fill some properties. I get the data from a XML File which i read by using LINQ to XML.

That looks like that:

var CharNames = from CharName in doc.Descendants("character").Attributes("name")
                select CharName;

my properties looks like that:

public List<int> CharClassId { get; set; }
public List<String> CharRace { get; set; }
public List<int> CharRaceId { get; set; }
public List<int> CharGenderId { get; set; }

then i want to fill this properties.. actually i use a simple foreach

foreach (String s in CharNames)
{
    CharName.Add(s);
}

and of course i get a NullReferenceException, because the List isn't initialized.

But i can't initialize it by using

CharName = new List<String>();

what would be a good solution?

maybe i am working in a absolutely wrong way... if it is so please tell me

thx

EDIT:

Ok i got the solution. I simply used the .NET 2.0 property style and made it like:

    private List<String> _CharName;
    public List<string> CharName 
    {
        get
        {
            if (_CharName == null)
                _CharName = new List<string>();
            return _CharName;
        }
        set
        {
            _CharName = value;
        }
    }

Now my list is getting filled by all names from the xml.

Thanks anyways =)


Simply Initialize it in the constructor.


List<String> CharNames = new List<String>();

CharNames = (from CharName in doc.Descendants("character").Attributes("name")
             select CharName).ToList();
0

精彩评论

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