0

I'm trying to modify a bunch of static value variables as fields in a static class. They need to be initialized in some sort of structure with a string attached to them, but the outside world should be able to just get the variable directly.

Here's a basic code dump of what I'm trying to do (disregard the specifics inside of DoStuff(); just an example of the kind of operations I'm trying to do):

public unsafe static class StaticVariables
{
    public static int foo;
    public static int bar;
    ...
    public static int bazinga;

    static IEnumerable<StaticInteger> intList = new List<StaticInteger>
    {
        new StaticInteger(&foo,"foo"),
        new StaticInteger(&bar,"bar"),
        ...
        new StaticInteger(&bazinga,"bazinga")
    };

    public static void DoStuff()
    {
        foreach(StaticInteger integer in intList)
        {
            if(integer.identifier=="foo") *integer.pValue = 30;
            if (integer.identifier == "bar") *integer.pValue = 23;
        }
        Console.WriteLine("{0} {1}", foo, bar);
    }

}

public unsafe class StaticInteger
{
    public int* pValue;
    public string identifier;

    public StaticInteger(int* pValue, string identifier)
    {
        this.pValue = pValue;
        this.identifier = identifier;
    }
}

I'm not able to grab the address of foo/bar where I want to. They're static/globals, so they shouldn't going anywhere. I can cheat and use fixed inside of DoStuff to initialize the list, but I want to be able to reference my list multiple times after initialization, and I'm not sure that's safe because we'd no longer be in the fixed block. Is there a way to tell the GC "Don't touch where you put this static variable please"?

I'd be super happy if the answer was "don't use pointers, do XYZ instead."

6
  • 4
    What is the ultimate goal you're trying to accomplish? What would be the perfect usecase for what you're trying to do? What would this code accomplish for you once it's written and working the way you want? Commented May 9, 2019 at 16:42
  • The static class will be a class of program-wide constants. I want to be able to query some outside thing with the list of string keys, and update the static variables. Commented May 9, 2019 at 16:48
  • 1
    Do you actually need static fields? If you can have static properties then I think this is a much easier to solve. Commented May 9, 2019 at 16:51
  • 2
    This seems way over-engineered. Why not just use a Dictionary<string, int>? Commented May 9, 2019 at 16:55
  • 2
    If you're using .NET Core, you can set application wide constants in a configuration file and access them using the Configuration object automagically. Commented May 9, 2019 at 17:16

1 Answer 1

1

Using a properties with only a getter rather than fields you can limit users to only reading values & the values can be stored in a Dictionary rather than a list.

public static class StaticVariables
{
    public static int foo { get {return values["foo"];}}
    public static int bar { get {return values["bar"];}}
    public static int bazinga { get {return values["bazinga"];}}

    private static Dictionary<String,int> values = new Dictionary<String,int>();

    static StaticVariables()
    {
        values.Add("foo",0);
        values.Add("bar",0);
        values.Add("bazinga",0);
    }

    public static void DoStuff()
    {
        values["foo"] =30;
        values["bar"] =23;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

That works great! I had to rethink how I iterated over them, but it worked well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.