简体   繁体   中英

Calling a class's method from Windows form application

I am trying to call a method with the same name of its class (AHPModel) but accepts an integer argument, from a Windows form by creating an instance of the class, but it gives me an error of "Object reference not set to an instance of an object" , please could you help!

Thank you.

In the Form:

private void button6_Click(object sender, EventArgs e)
{
    try
    {
        AHPModel model = new AHPModel(3, 3);
        model.AddCriteria(criteria);
        model.AddCriterionRatedChoices(0, night);
        model.AddCriterionRatedChoices(1, act);
        model.AddCriterionRatedChoices(2, cost);

        model.CalculateModel();

        GeneralMatrix calcCriteria = model.CalculatedCriteria;
        GeneralMatrix results = model.ModelResult;
        GeneralMatrix choices = model.CalculatedChoices;
    }

    catch (System.Exception excep)
    {
        MessageBox.Show(excep.Message);
    }
}

My Class

public class AHPModel
{
    public AHPModel(int n)
    {

    }
    public void CalculateModel()
    {
        CalculatePriorities();
        CalculateChoices();
        CalculateFinalResult();
    }
}

That is happening because you have not created the parmeterized constructor for

AHPModel model = new AHPModel(3, 3);

You can do the AHPModel model = new AHPModel(3, 3); when you have class with constructor like below

public class AHPModel
 {
    public AHPModel(int n)
    {

    }
    public AHPModel(int n,int n)
    {

    }
    public void CalculateModel()
    {
        CalculatePriorities();
        CalculateChoices();
        CalculateFinalResult();
    }
}

I just realised that I was passing null values of arrays that are locally declared to the method. But now I am able to access the targeted method from my windows form.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM