J.Do
J.Do

Reputation: 11

How to properly create an instance to use methods?

I am currently trying to wrap my head around classes. This is probably a newbie question, but considering I keep googling and am stuck in a circle I guess I need a pointer.

The problem is that im trying to create an instance so that I may use the methods of Info and Liste by button click in winforms. However, it tells me that

there is no argument given that corresponds to the required parameter of form1.

Which makes sense, considering its an constructor and all.

As potential fix its telling me to use the following code in the Methods class:

public Methods()
{
}

Which is another constructor I take it, however I have 0 idea what to do with it. Help is appreciated.

public partial class Main : Form
{
    Methods Methods = new Methods(); // not working

    public Main()
    {
        InitializeComponent();
        Methods Methods = new Methods(this);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Methods.Info();  // not working
        Methods.Liste(); // not working
    }       
}

public class Methods
{
    int count = 0;
    private Main mainForm;

    public Methods(Main form1)
    {
        mainForm = form1;
    }

    public void Info()
    {
        Inventory.array[count] = Convert.ToInt32(mainForm.textBox2.Text) * Convert.ToInt32(mainForm.textBox3.Text);
        count++;
    }

    public void Liste()
    {
        int sum = 0;
        foreach (int i in Inventory.array)
        {
            sum += i;
        }
        mainForm.label5.Text = Convert.ToString(sum);
    }
}

Upvotes: 0

Views: 75

Answers (2)

MakePeaceGreatAgain
MakePeaceGreatAgain

Reputation: 37113

You have a bunch of errors. Let´s focus them step by step:

  1. You have a field of type Methods that references the single instance of your Methods-class (by the way a really bad name for a class). In order to create that instance you need a reference to an existing form. However the this-keyword doesn´t exist outside a member. That´s what the compilercomplains about in your field-declaration.

  2. You have a local variable named Methods in your constructor that hides the field. So in fact you have two instances of the class, one in the field, and one defined in the cosntructor. Just omit the latter:

    readonly Methods Methods;
    
    public Main()
    {
        InitializeComponent();
        this.Methods = new Methods(this);
    }
    
    private void button1_Click(object sender, EventArgs e)
    {
        Methods.Info();  // not working
        Methods.Liste(); // not working
    }
    

    Just an aside: you can omit the this-keyword in the constructor:

    Methods = new Methods(this)
    

    instead of

    this.Methods = new Methods(this)
    

    as it´s clear to which symbol you refer. I only added it to make this more clear.

  3. Most important: Methods is really bad name for a class. Instead chose a name that describes what your class is supposed to do. Best you have a look at naming-conventions.

Upvotes: 1

adjan
adjan

Reputation: 13684

Methods Methods = new Methods(this);

is creating a local variable that shadows the field in the class. Use

this.Methods = new Methods(this);

instead. And you should not use capitalized words for fields or locals.

Upvotes: 1

Related Questions