Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Variable Getters / Setters

Tags:

c#

I'm trying to create a simple way for external programs to get/set variables within my class. What I have is a class with several variable such as this:

class myClass
{
    public int one
    {
        get{ /* get code here */ }
        set{ /* set code here */ }
    }
}

Rather than just variable 'one' I have close to 100 variables. All are set up the same way with get and set code. What I would like to have is a simple way to get and set the variables. Example: Instead of having to do this:

myClass c = new myClass();
c.one = 5;

I would like to find a way to do something similar to this:

myClass c = new myClass();
c.setVariable("variableName", value);

It would be ideal to have the "variableName" text come from an enum list so that they could be referenced like:

c.setVariable(enumName.varName, value);

I am unsure how to go about this or if it is even possible. As I said i have close to 100 variables which need their own get/set code, but I'd prefer to have only one public get function and one public set function for various reasons.

As I don't think reflection is very efficient, I'd like to avoid it if at all possible.

I've seen other code in C# where something like this is used:

this["variableName"] = value;

However I can't seem to find a way to make this work either...

like image 929
m0n1ker Avatar asked May 08 '26 02:05

m0n1ker


2 Answers

this["variableName"] = value;

The syntax you describe is only valid for dictionaries. And if you don't want to use reflection and only want a single getter and setter, you should maybe use a dictionary. Edit: Do you know about lambda expressions? They are inline anonymous methods, and by putting those in another dictionary you can do validation! Something like this:

public class MyClass
{
    private readonly Dictionary<String, Object> dictionary;
    private readonly Dictionary<String, Func<Object, bool>> validators;

    public MyClass()
    {
        this.dictionary = new Dictionary<String, Object>();
        this.validators = new Dictionary<String, Func<Object, bool>>();

        // Put the default values in the dictionary.
        this.dictionary["One"] = 10;
        this.dictionary["Another"] = "ABC";

        // The validation functions:
        this.validators["One"] = obj => ((int)obj) >= 0;
        this.validators["Another"] = obj => obj != null;
    }

    public Object this[string name]
    {
        get { return this.dictionary[name]; }
        set
        {
            // This assumes the dictionary contains a default for _all_ names.
            if (!this.dictionary.Contains(name))
                throw new ArgumentException("Name is invalid.");

            // Get a validator function. If there is one, it must return true.
            Func<Object, bool> validator;
            if (validators.TryGetValue(name, out validator) &&
                !validator(value))
                throw new ArgumentException("Value is invalid.");

            // Now set the value.
            this.dictionary[name] = value;
        }
    }
}

Some things to note about this code:

  • The dictionary is a generic object: Dictionary Of String and Object, where String is the type of the key and Object is the type of the value. If you have only one kind of value (for example integers) then you should change Object everywhere to, for example, int.
  • Given an instance MyClass myClass, you can do myClass["One"] = 20 to set the value of One.
  • If you (inadvertently) try to get the value with a name that does not exist (var value = myClass["DoesNotExist"]), you'll get an exception. You can change this to return a default value or do something else entirely.
  • To use an enum, create the enum and specify its type instead of String in the code.
like image 101
Daniel A.A. Pelsmaeker Avatar answered May 10 '26 14:05

Daniel A.A. Pelsmaeker


Another solution could be use reflection in this way:

public object this[string name] {
    get { return this.GetType().GetProperty(name).GetValue(this, null); }
    set { this.GetType().GetProperty(name).SetValue(this, value, null); }
}

or:

public void SetVariable(string name, object value){
    this.GetType().GetProperty(name).SetValue(this, value, null); 
}
like image 30
Omar Avatar answered May 10 '26 16:05

Omar