Michael Tilelli
Michael Tilelli

Reputation: 11

How, if possible, can you pass in a C# Property to be used like a method?

I know Func<> is used to pass a method that has a return value to be used inside another method. I know Action<> is used to pass a method that does not have a return value to be used inside another method. Is there a way to pass in a property so it's get/set can be used inside another method?

For example, here is a method that uses Func<>:

public bool RangeCheck (int minVal, int maxVal, Func<< int, int >> someMethod)  
{  
    bool retval = true;  
    try  
    {  
        for (int count = min; count <= max; count++)  
        {  
            int hello = someMethod(count);  
        }  
    }  
    catch  
    {  
        retval = false;  
    }  
    return retval;  
}  

What I am looking for is something like this:

public bool RangeCheck(int min, int max, Prop<< int >> someProperty)  
{  
    bool retval = true;  
    try  
    {  
        for (int count = min; count <= max; count++)  
        {  
            someProperty = count;  
        }  
    }  
    catch  
    {  
        retval = false;  
    }  
    return retval;  
}  

Is there anything out there like this? I can't find anything. This would be very useful. Thanks.

Upvotes: 1

Views: 134

Answers (4)

Silvermind
Silvermind

Reputation: 5944

You could use a Func like this Func<int, T>

void Main()
{
    var sc = new SimpleClass();
    var result = RangeCheck(0, 10, x => sc.Value = x );
    System.Console.WriteLine(result);
    System.Console.WriteLine(sc.Value);
}

public class SimpleClass
{
    public int Value { get; set; }
}

public bool RangeCheck<T>(int minVal, int maxVal, Func<int, T> someMethod)   
{   
    bool retval = true;   
    try   
    {   
        for (int count = minVal; count <= maxVal; count++)   
        {
            //someMethod(count); //is not a range check,
            //Did you mean
            someMethod(count - minValue);
        }   
    }   
    catch   
    {   
        retval = false;   
    }   
    return retval;   
}

Upvotes: 0

muratgu
muratgu

Reputation: 7311

Why not simply make it a ref argument?

public bool RangeCheck(int min, int max, ref int someProperty)

You can now set the value of someProperty inside the method.

And call it like so:

RangeCheck(min, max, ref myProperty);

Upvotes: 1

Cole Cameron
Cole Cameron

Reputation: 2233

Not that I know of. You could try using reflection and pass the object along with the corresponding PropertyInfo object of the property you want to get the value of. You then call PropertyInfo's SetValue function to assign a value to it (assuming it's read/write, of course).

    public void SetMyIntValue()
    {
        SetPropertyValue(this, this.GetType().GetProperty("MyInt"));
    }

    public int MyInt { get; set; }

    public void SetPropertyValue(object obj, PropertyInfo pInfo)
    {
        pInfo.SetValue(obj, 5);
    }

Upvotes: 3

Matthew
Matthew

Reputation: 25783

Could you use a lambda as a wrapper?

MyClass myClass = new MyClass();

bool val = RangeCheck(0, 10, () => myClass.MyProperty);

If you're looking to do both, you would make two lambdas, one for set, and one for get.

bool val = RangeCheck(0, 10, () => myClass.MyProperty, (y) => myClass.MyProperty = y);

My syntax is probably off, but I think this gives the idea.

Upvotes: 6

Related Questions