473,545 Members | 2,688 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Making a method public

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(stri ng s, int Id)

{

}

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

Any one know what I'm I doing wrong?

Thanks,

Tom


Sep 13 '07 #1
4 1247
in C# you need to make an instance first. like this

Form1 formy = new Form1();

formy.publicmet hod("hello world");

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

good luck

"Tom Woods" wrote:
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(stri ng s, int Id)

{

}

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

Any one know what I'm I doing wrong?

Thanks,

Tom


Sep 13 '07 #2
bob
On Thu, 13 Sep 2007 16:48:10 -0500, "Tom Woods" <tw****@gobaker .com>
wrote:
>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(stri ng s, int Id)

{

}

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

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

Sep 13 '07 #3
roger_27 wrote:
in C# you need to make an instance first. like this

Form1 formy = new Form1();

formy.publicmet hod("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
Sep 14 '07 #4
bob wrote:
[...]
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
Sep 14 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2324
by: Mark | last post by:
I'm betting it me. Here is the simple schema I'm using: <?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" jaxb:version="1.0"> <xs:annotation> <xs:appinfo>
7
1347
by: cmelnick | last post by:
I have a custom class that basically consists of two elements, a name (String) and value (object). I am a bit confused on how to clone or make a copy of an instance of my object. If I have: public class MyItem { private string name; private object value; public MyItem() { }
4
1599
by: titancipher | last post by:
I have a container that I wish to allow the user to specify a custom comparison method very similar to std::less. However, I want it to function more like memcmp (returning -1 0 1), and I want to be able to vary the fields that are compared. The example below shows how I'd like it to fit together. struct fields { fields( int f1, int f2,...
5
5319
by: Action | last post by:
does it works like ordinary virtual method?? coz I find that child class can't invoke the event of the parent class. class parent { public virtual event SomeDelegate SomeChanged; } class child : parent {
10
1855
by: Jeff Grills | last post by:
I am an experienced C++ programmer with over 12 years of development, and I think I know C++ quite well. I'm changing jobs at the moment, and I have about a month between leaving my last job and starting my new one. In that time, I have decided to learn C#. I picked up the book, "Programming C# (4th Edition)" recently and have read most of...
3
1176
by: Peteroid | last post by:
Is it possible to make a public parent class method unavailable (i.e., generate an error at compile time) to a particular child class? For example, say a parent class has a public method Add( ). I want to create a child class of this parent class that does not have an Add( ) method (while possibly another child class does). I think I...
1
3580
by: nate axtell | last post by:
In VB .Net I made a custom CheckBox column style (for the Datagrid control) that maps to two DataTable columns , one it uses for the Checked status and the other it uses for the Enabled status. I am having a couple problems so far. 1. with the way the Paint method works: When I scroll to the right, the custom column gets Drawn on top of the...
34
3669
by: Asfand Yar Qazi | last post by:
Hi, I'm creating a library where several classes are intertwined rather tightly. I'm thinking of making them all use pimpls, so that these circular dependancies can be avoided easily, and I'm thinking of making all these pimpl class declarations public. Reasoning is that since only the code within the ..cc file will need to ever access...
7
1791
by: James Crosswell | last post by:
I want to create a class with a static property as follows: class MyClass { private static List<MyHandlerRegisteredHandlers = new List<MyHandler>; } I then want to be able to create descendants of the MyHandler class which will register themselves using a RegisterHandler method, which
4
2123
by: Energizer100 | last post by:
public class fly extends Actor { public fly() { setColor(Color.BLACK); } public fly(Color flyColor) { setColor(flyColor);
0
7502
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7692
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7946
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7457
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6026
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5360
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5078
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3470
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1045
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.