tags:

views:

67

answers:

4

Guys i'm really embaraced to ask for this, but i got stuck and i can't think for solution:

I found an IniParser library that i use for parsing settings from file. However the default approach is this:

 FileIniDataParser parser = new FileIniDataParser();
 //Parse the ini file
 IniData parsedData = parser.LoadFile("TestIniFile.ini");
 parsetData['some']['settings'];

In my project though, i don't want to call everywhere the parser, so i made a class that load up the file at start-up and all i have to do is just access the instance. This is what i want:

namespace my_project
{
    public class settings
    {
        public IniData ini()
        {
            string login = "Settings.ini";
            FileIniDataParser parser = new FileIniDataParser();

            // Parse the ini file with the settings
            IniData settings = parser.LoadFile(login);
            //*strptr = settings;
            return settings;
        }
    }
}

so i could access the settings like this:

settings.ini['some']['settings'];

Bit i get an error:

Error 329 'settings.ini()' is a 'method' but is used like a 'type'

I know its probably too noobish, but im currently trying to learn C# and experiments like this teach more than reading a 500 page book.

+2  A: 

Try making your ini method a property:

namespace my_project
{
    public class settings
    {
        public IniData Ini
        {
            get
            {
                string login = "Settings.ini";
                FileIniDataParser parser = new FileIniDataParser();

                // Parse the ini file with the settings
                IniData settings = parser.LoadFile(login);
                //*strptr = settings;
                return settings;
            }
        }
    }
}
Robert Harvey
...except parsing the file every time is probably not a good plan :)
jvenema
@jvenema: That was my idea... and why I had recommended a static prop.
Reed Copsey
Thanks for this, appreciate it.
Anonymous
+1  A: 

To access your instance as

settings.ini['some']['settings'];

You need to make ini a property:

    public IniData ini
    {
        get
        { 
            string login = "Settings.ini";
            FileIniDataParser parser = new FileIniDataParser();

            // Parse the ini file with the settings
            IniData settings = parser.LoadFile(login);
            return settings;
        }
    }

The way you have it now, you must access it as:

settings.ini()['some']['settings'];
Mark Rushakoff
+3  A: 
var foo = Settings.Data['some']['settings'];

// ..

public static class Settings
{
    static Settings()
    {
        string login = "Settings.ini";
        FileIniDataParser parser = new FileIniDataParser();
        _data = parser.LoadFile(login);
    }

    private static readonly IniData _data;
    public static IniData Data
    {
        get { return _data; }
    }
}
LukeH
This is correct - though I'll point out that making "Settings" static is not required. (It may be very correct to do so, but it may not, if multiple "settings" are possible... )
Reed Copsey
Thanks for this also a nice clean solution
Anonymous
+4  A: 

You defined "ini" as a method. In order to use it, as-is, you would need to type:

settings.ini()['some']['settings'];

You could change this to a property to work around this issue.

However, one thing to be aware of - the way you have this written, every time you call ini(), you're creating (and accessing) a new instance of your IniData class. If this is meant to be a single, shared instance, you may want to use lazy initialization with a static property:

public class Settings
{
    private static IniData ini;
    public static IniData Ini
    {
        get
        {
            if (ini == null)
            {
                string login = "Settings.ini";
                FileIniDataParser parser = new FileIniDataParser();

                ini = parser.LoadFile(login);
            }
            return ini;
        }
    }
}

This way, each call will use the same instance, and it will only create the IniData class one time. You could then write:

Settings.Ini['some']['settings'];
Reed Copsey
Thanks really working well and plus its only one instance. Thanks one more time.
Anonymous