Reputation: 2276
I have a public method in a class. I would like to make this private and use a property to expose the method. The problem is that my method accepts an integer parameter and returns a boolean. In my experience a property has to accept and return the same type. What is the best practice to encapsulate this method and expose it using a property?
Upvotes: 3
Views: 7434
Reputation: 575
public bool MyProp
{
get;
private set;
}
public int MyProp_AsInt
{
set
{
MyProp = (value > 0) ? true : false;
}
}
Your function's body can replace the above 'set'. I just put some sample code there.
full code
public class MyClass
{
public bool MyProp
{
get;
private set;
}
public int MyProp_AsInt
{
set
{
MyProp = (value > 0) ? true : false;
}
}
}
public class Program
{
static void Main(string[] args)
{
MyClass o = new MyClass();
o.MyProp_AsInt = 1;
System.Console.WriteLine("{0}", o.MyProp);
o.MyProp_AsInt = 0;
System.Console.WriteLine("{0}", o.MyProp);
string line = System.Console.ReadLine();
}
}
Upvotes: 1
Reputation: 45771
If the value passed in isn't the same as the value returned, then it's not a property. If you go down this route you'll be creating confusion for anyone who needs to call your code, or maintain it in the future.
The only solution I can think of, and a bad one at that, is to declare the property to be of type object
.
What is the best practice to encapsulate this method and expose it using a property?
Does it have to be the same property? Could you have something similar to:
private Type2 _valueThatIsStoredAsAResultOfCallMethod;
private Type2 CallMethod(Type1 value)
{
// Whatever logic is required here to take a value of Type1 and
// get a value of Type2 from it
return value.ToType2();
}
public Type1
{
set
{
// value is of type Type1
_valueThatIsStoredAsAResultOfCallMethod = CallMethod(value);
}
}
public Type2
{
get
{
return _valueThatIsStoredAsAResultOfCallMethod;
}
}
Upvotes: 2
Reputation: 700422
A method that takes a parameter and returns a value doesn't translate well into a property, regardless of the types it uses.
The method accepts the parameter, then returns the result, while a parameter either accepts a value or returns a value, not both in the same operation. If you would have to use the property by first setting the value, then reading the value, that would be very counter intuitive. It's simply not at all how you normally use a property.
Upvotes: 0
Reputation: 8645
Well, generally speaking, it's bad practice to call a method via a property (or do anything complicated in a property) as it's not what the user of the class would expect to happen. The expected behaviour being getting a value.
I would just expose the method.
Upvotes: 0
Reputation: 17119
There is no best practice for doing this because it's a bad practice.
If you had two covariant types as your accept/return, you could refer to them by their lowest common ancestor, but I'd still be wary.
Upvotes: -1
Reputation: 61599
You shouldn't be using properties this way. Properties are used to wrap get_
and set_
calls to an appropriate backing field and expose them as a single member. The set_
method that is generated internally is void
and accepts an instance of the property type as its only argument.
If what you are trying to achieve requires a method, then expose a method. The only solution you could possibly use otherwise would be to use object
.
Upvotes: 8