Jpsh
Jpsh

Reputation: 1726

Error using index property c#

So my main issue was I had a static class in a VB.NET project that I used for accessing AppSettings. Original Code looks like this

Module AppSettings
    Public Property Value(ByVal NodeName As String) As String
        Get
            ConfigurationManager.RefreshSection("appSettings")
            Return ConfigurationManager.AppSettings(NodeName)
        End Get
        Set(ByVal value As String)
            Dim Config As Configuration = _
                ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location)
            Config.AppSettings.Settings.Item(NodeName).Value = value
            Config.AppSettings.SectionInformation.ForceSave = True
            Config.Save(ConfigurationSaveMode.Modified)
            ConfigurationManager.RefreshSection("appSettings")
        End Set
    End Property
End Module

so after a lot of searching I decided that that using an indexer was the way to go I came up with the following c# code and causes no *compiling errors unless I try to implement it. I don't know if the issue is with my implementation or syntax when I'm trying to use it.

public static class AppSettings
{
    public static class Value
    {
        public static string this[string nodeName]
        {
            get
            {
                ConfigurationManager.RefreshSection("appSettings");
                return ConfigurationManager.AppSettings[nodeName];
            }
            set
            {
                Configuration Config = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);
                Config.AppSettings.Settings[nodeName].Value = value;
                Config.AppSettings.SectionInformation.ForceSave = true;
                Config.Save(ConfigurationSaveMode.Modified);
                ConfigurationManager.RefreshSection("appSettings");
            }
        }
    }
}

this is the code that shows the error when I try to use these new classes

string mySetting = AppSettings.Value["mySettingName"];

this gives me the following error

'...AppSettings.Value' is a 'type', which is not valid in the given context

any help would be greatly appreciated.

Edit after some helpful comments I realized the code does not compile, for some reason was not showing me an error. So my is how would be the most similar way to the working VB code to implement it in C#.

Upvotes: 0

Views: 123

Answers (4)

user1967487
user1967487

Reputation: 83

to make something that works very similarly to the vb Code you'd have to do something like the following. notice how the _value class is not static and the static AppSettings class has to initialize the instance of the _value class.

public static class AppSettings{
    public static _value Value { get; set; }

    //static setter
    static AppSettings(){
        Value = new _value();
    }
}

public class _value{
    public string this[string nodeName]{
        get{
            //get code here
        }
        set{
            //set code here
        }
    }
}

to use this code you would do something like the following.

//get a value
string mySetting = AppSettings.Value["mySetting"];
//set a value
AppSettings.Value["mySetting"] = "myNewSettingValue";

hope this helps.

Upvotes: 1

Dave Doknjas
Dave Doknjas

Reputation: 6542

The original VB code is not implementing an indexer, but a "parameterized property", which isn't available in C#. The closest C# equivalent is:

internal static class AppSettings
{
    public static string get_Value(string NodeName)
    {
        ConfigurationManager.RefreshSection("appSettings");
        return ConfigurationManager.AppSettings[NodeName];
    }
    public static void set_Value(string NodeName, string value)
    {
        Configuration Config = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);
        Config.AppSettings.Settings[NodeName].Value = value;
        Config.AppSettings.SectionInformation.ForceSave = true;
        Config.Save(ConfigurationSaveMode.Modified);
        ConfigurationManager.RefreshSection("appSettings");
    }
}

Upvotes: 1

Bart De Boeck
Bart De Boeck

Reputation: 806

I'm not sure I fully understand your question. Does this code help?

class Program
{
    static void Main(string[] args)
    {
        Value v = new Value();
        string output = v["hello"];
        Console.WriteLine(output);
        Console.ReadLine();
    }
}

public class Value
{
    public string this[string nodeName]
    {
        get
        {
            ConfigurationManager.RefreshSection("appSettings");
            return ConfigurationManager.AppSettings[nodeName];
        }
        set
        {
            Configuration Config = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);
            Config.AppSettings.Settings[nodeName].Value = value;
            Config.AppSettings.SectionInformation.ForceSave = true;
            Config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");
        }
    }
}

Upvotes: 1

Abion47
Abion47

Reputation: 24726

Static indexers aren't supported in C# as indexers require a reference to this which doesn't exist in a static context. The reason you are getting that error is because AppSettings.Value is a type, but using an indexer treats it like an array, and that isn't going to fly.

You're going to instead want to use a getter and setter function, t.e.:

AppSettings.Value.GetSetting("settingName");
AppSettings.Value.SetSetting("settingName", "settingValue");

Alternatively, you can use an object reference to handle the indexer. (See the solution on this page for more info.)

See this page on MSDN for more info on indexers in general.

Upvotes: 0

Related Questions