Chris Whisenhunt
Chris Whisenhunt

Reputation: 351

Set multiple properties with one method

So I have a c# class with three properties on it.

public class ClassA
{
    public bool IsBool1;
    public bool IsBool2;
    public bool IsBool3;
}

I have a method that does the following.

public void MethodA()
{
    ClassA c = GetCurrentClassA();
    c.IsBool1 = !c.IsBool1;
    ClassADataAccess.Update(c);
    this.BindClassADetails(c);
}

Now to keep from writing MethodA over for the other two properties is there a way to write a method that can handle all three?

Something maybe like this?

public void UpdateAndBind(bool value)
{
    ClassA c = GetCurrentClassA();

    ///what to do here to set the property?

    ClassADataAccess.Update(c);
    this.BindClassADetails();
}

Upvotes: 1

Views: 2174

Answers (3)

gajama
gajama

Reputation: 79

It can be done using Func delegates as input parameters.

First, you need to use properties, not fields:

public class ClassA
{
    public bool IsBool1 { get; set; }
    public bool IsBool2 { get; set; }
    public bool IsBool3 { get; set; }
}

Then you need two Func delegates as input parameters:

public ClassB
{
     public void UpdateAndBind(Func<ClassA, bool> getProp, Func<ClassA, bool, ClassA> setProp)
    {
        ClassA c = GetCurrentClassA();

        // What to do here to set the property?
        setProp(c, getProp(c));

        ClassADataAccess.Update(c);
        this.BindClassADetails(c);
    }
    ...
}

Finally, usage would look like:

static void Main(string[] args)
{
    ClassB classB = new ClassB();
    classB.UpdateAndBind(classA => classA.IsBool1, (classA, value) => { classA.IsBool1 = !value; return classA; });
    classB.UpdateAndBind(classA => classA.IsBool2, (classA, value) => { classA.IsBool2 = !value; return classA; });
    classB.UpdateAndBind(classA => classA.IsBool3, (classA, value) => { classA.IsBool3 = !value; return classA; });
}

Upvotes: 1

dylansweb
dylansweb

Reputation: 684

In response to your comment:

That could work perfectly fine, I was just curious as to whether or not there is a way to tell a method which property on a passed in object to set. – Chris Whisenhunt

There is a way using reflection:

    // using a generic method, you can specify the name of the property you want to set, 
    // the instance you want it set on, and the value to set it to...
    private T SetProperty<T>(T instance, string propertyName, object value)
    {
        Type t = typeof(T);
        PropertyInfo prop = t.GetProperty(propertyName);
        prop.SetValue(instance, value, null);
        return instance;
    }

Now...you may want to use try/catch, also perhaps inspect what's being passed in before using it in various ways to ensure it will not jut blow up...but generally, and most simplistically, that is the way you'd do it. Good Luck!

Upvotes: 1

Ignacio Soler Garcia
Ignacio Soler Garcia

Reputation: 21855

What about adding parameters to the GetCurrentClassA method so you can specify the values you want there?

public void MethodA()
{
    ClassA c = GetCurrentClassA(true, false, true);
    ClassADataAccess.Update(c);
    this.BindClassADetails(c);
}

Upvotes: 0

Related Questions