Making a method public

  • Thread starter Thread starter Tom Woods
  • Start date Start date
T

Tom Woods

I'm new to C# and was wanting to access a method in the main form from
another form

frmMain has a method as such:
public void AddNewForm(string s, int Id)

{

}



However when I'm in a click event in another form, I cannot see AddNewForm
when addressing frmMain.AddNewForm.

Any one know what I'm I doing wrong?

Thanks,

Tom
 
in C# you need to make an instance first. like this

Form1 formy = new Form1();

formy.publicmethod("hello world");

once you press "." for your instance, you will see the method.

good luck
 
I'm new to C# and was wanting to access a method in the main form from
another form

frmMain has a method as such:
public void AddNewForm(string s, int Id)

{

}



However when I'm in a click event in another form, I cannot see AddNewForm
when addressing frmMain.AddNewForm.

Any one know what I'm I doing wrong?

Thanks,

Tom
Hi Tom,
I am assuming that you want to access an instance of frmMain that will
be running at the time and frm2 is instantiated by some action on
frmMain.
So declare a public variable of type frmMain in frm2 and assign it
when you are instantiating frm2
i.e.
in frm2
public frmMain myFrmMain;

In the appropriate point in frmMain
frm2 f = new frm2();
f.myFrmMain = this;
f.show();

hth
Bob
 
roger_27 said:
in C# you need to make an instance first. like this

Form1 formy = new Form1();

formy.publicmethod("hello world");

once you press "." for your instance, you will see the method.

Yes, he will. But it won't be the instance he wants.

Tom, the above reply is half-right anyway: you do need an instance of
your form class to get at the instance method. The solution is either
to somehow get that instance to the event handler, or to make the method
a static method.

If the method does not actually need access to any instance data from
the form instance you have, then the latter is probably the easiest
thing to do.

If you can't make the method static, but you are still having trouble
figuring out how to get the instance to the event handler, please post a
more complete description of how the two forms are related so that more
specific advice can be given.

Pete
 
bob said:
[...]
So declare a public variable of type frmMain in frm2 and assign it
when you are instantiating frm2
i.e.
in frm2
public frmMain myFrmMain;

IMHO it is better to not make public fields. Make a property, so that
the users of the class are not tied to the internal implementation or,
even better, just pass the instance to the constructor when the second
form is created:

class frm2 : Form
{
frmMain _frm;

public frm2(frmMain frm)
{
_frm = frm;
}
}

Then:

frm2 f = new frm2(this);

f.Show();

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top