Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am trying to build a dummy exercise for my own interest. I have one form (form1) with two textboxes (textBox1, textBox2) and a button.

When I click the button, I want to add the numbers passed to the textboxes. Now, I am trying to complicate things by introducing an Interface with the signatures of the appropriate methods to do the addition (ICalculate) and a class (Calculations) which implements the interface.

Then I have another class called Calc which gets initiated from Form1 by passing an ICalculate object and 2 integers (a,b) in its constructor. Furthermore, the Calc class has a method (addition()) for adding the two integers, using the ICalculate object and the two integers instantiated at the constructor of the class and display the result on a messagebox.

The problem is that the compiler throws an error saying that the ICalculate object has not been initialized (Object reference not set to an instance of an object.)

The code is the following:

public interface ICalculate
    {
    int add(int x, int y);
    int sub(int x, int y);
    }

class Calculations : ICalculate
    {

    public int add(int a, int b)
        {
        return (a + b);
        }
    public int sub(int z, int k)
        {
        return (z - k);
        }
    }

class Calc
    {

    private ICalculate _nc;
    private int _x, _y;

    public Calc(ICalculate nc, int a, int b)
        {
        var _nc = nc;
        _x = a;
        _y = b;

        }
    public void addition()
        {
        MessageBox.Show(_nc.add(_x, _y).ToString());
        }
    }
}

The Form1 Code is the following:

        public Form1()
        {
        InitializeComponent();
        }

    private void button1_Click(object sender, EventArgs e)
        {
        var a = Int32.Parse(textBox1.Text);
        var b = Int32.Parse(textBox2.Text);

        var c = new Calc(new Calculations(), a, b);
        c.addition();
        }
    }

Any help appreciated !

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
225 views
Welcome To Ask or Share your Answers For Others

1 Answer

The problem is in the constructor of the Calc class.
Specifically, this row:

var _nc = nc;

You should remove the var:

_nc = nc;

What happens is that once you have the var keyword you are actually creating a local variable called _nc in the constructor, and assign the value of nc to it, so the class member called _nc is never initialized.

The compiler (tested on VS 2013) should issue a warning for this:

Field '<your namespace here>.Calc._nc' is never assigned to, and will always have its default value null


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...