Milanisius Milanisius - 1 month ago 11
C# Question

How can I call a non-static method out of another class in C#?

The question itself sounds a bit nooby. To call a non static method from another class you have to use the current instance of the class. I tried to get the instance of the class with the method i want to call to the second class. I think I succeeded but it is still not working yet.

This one way how I tried to bring the instance to the second class:

arbeitsbearbeitung arbeitenbearbeitung = new arbeitsbearbeitung(arbeit);
arbeitenbearbeitung.Parent = this;
arbeitenbearbeitung.Show();


(out of Class 1)

And this is where I used it in class 2:

Form frm = (Form)this.Parent;
frm.updateGrid();


I also tried it with different ways like passing it as parameter; same result...

This is
updateGrid()
in class 1:

public void updateGrid()
{
klassenarbeitenTableAdapter.Fill(this.database1DataSet.Klassenarbeiten);
}


It tells me that Form does not contain a definition for
updateGrid
.

I must be dumb but I can't find my mistake.

Thinking and googling for 2 hours and still no clue.

I feel dumb now. Thank you for your help!

Answer

Form does not have an updateGrid method but your derived class does. You need to cast Parent to your class: MyForm frm = (MyForm)this.Parent;