views:

43

answers:

3

Probably a stupid question, but I'm quite new to the whole "get-and-set-property"-kind of programming;

I keep getting a compiling-error on this part of my code;

private string _File = "Session.xml";

private XmlDocument XmlDoc
{
    get
    {
        XmlDocument _Doc = new XmlDocument();
        return _Doc.LoadXml(_File);
    }
}

private XmlElement XmlRoot
{
    get
    {
        return XmlDoc.DocumentElement;
    }
}

How come? I can't explain that to myself as I don't even see any implicit conversions...

+2  A: 

_Doc.LoadXml(_File); return void and not XmlDocument. Change your code to:

private XmlDocument XmlDoc
{
    get
    {
        XmlDocument _Doc = new XmlDocument();
        _Doc.LoadXml(_File);
        return _Doc;
    }
}
Grzenio
+3  A: 

The problem is this line:

return _Doc.LoadXml(_File);

You're trying to return a value from a method that has a return type of void.

Try this instead:

private XmlDocument XmlDoc
{
    get
    {
        XmlDocument _Doc = new XmlDocument();
        _Doc.LoadXml(_File);
        return _Doc;
    }
}
Kev
+1  A: 

This code is your problem:

return _Doc.LoadXml(_File);

The LoadXml method has a return type of void, as the method does not return any value, instead populating the XmlDocument instance from the file path specified.

To fix your problem, simply change your property to this:

private XmlDocument XmlDoc
{
    get
    {
        XmlDocument _Doc = new XmlDocument();
        _Doc.LoadXml(_File);
        return _Doc;
    }
}
Programming Hero
Oh boy, that was so obvious... Dammit -.-
ApoY2k