junaid juni
junaid juni

Reputation: 13

how i can send two or more than two arguments to private methods of class using properties

hi i have class with one private method. i am trying to send two values(two arguments) to its method using property but its not working.some time it gives compiler error and some time wrong answer(logical error).

class sum
{
    private int add (int a, int b) 
    {
        return a+b;
    }

    private int ts;
    public int MyProperty
    {
        get { return ts; }
        set { ts = add(value,value);
    }
}

Code in main Class

private void button5_Click(object sender, EventArgs e)
{
    sum sumv = new sum();
    sumv.MyProperty=2;

    int sumj = sumv.MyProperty;
    MessageBox.Show(sumj.ToString());
}

Upvotes: 1

Views: 299

Answers (4)

Fabjan
Fabjan

Reputation: 13676

There is a rule of some sort about this :

If you have private method that you want to expose to other classes but you don't want to make it public then create a public delegate and pass your method with this delegate

Your property can be of delegate type. In this case the get method will return delegate. This delegates contains your method add, that you can run :

class sum
{
    private int add (int a, int b) 
    {
        return a+b;
    }

    public Func<int, int, int> MyProperty
    {
        get { return add; }
    }
}

And use it like this :

private void button5_Click(object sender, EventArgs e)
{
    sum sumv = new sum();

    int sumj = sumv.MyProperty(5, 10);
    MessageBox.Show(sumj.ToString());
}

Upvotes: 0

DavidG
DavidG

Reputation: 118937

There seems to be a lot of confusion here caused by OOP principles. Here's a couple of ways you could achieve what you want.

  1. Make your method public. There's nothing wrong with doing this as you are not storing anything and there's nothing to encapsulate:

    public class Adder
    {
        public int Add(int value1, int value2)
        {
            return value1 + value2;
        }
    
    }
    
  2. Pass in your parameters in the class constructor:

    public class Adder
    {
        private int _value1;
        private int _value2;
    
        public Adder(int value1, int value1)
        {
            _value1 = value1;
            _value3 = value2; 
        }
    
        public int Add()
        {
            return _value1 + _value2;
        }
    
    }
    

Upvotes: 0

Kilazur
Kilazur

Reputation: 3188

Well the short answer is: you can't send two parameters to a property setter.

Properties are just meant to be synthaxical sugar to avoid having to implement java/c++-like accessors (getProperty(), setProperty()...).

That being said, you can totally do some work on the value you get in the property's setter (like checking if it is in a certain range of values, or even modifying it...). But value is the only value you will ever get by using it.

And public methods aren't inherently bad, else we wouldn't have the mean to use them in the first place. I'd like to know where you've seen that.

Upvotes: 1

Artem Kulikov
Artem Kulikov

Reputation: 2296

If you want to practice in OOP concepts - your way isn't correct.

This class much better suitable for OOP:

public class Adder
{
    public int Result {get; private set;}   // private setter allow you to hide this member.

    public void Sum(int value1, int value2)
    {
        Result = value1 + value2;
    } 
}

Upvotes: 0

Related Questions