473,385 Members | 1,320 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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(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


Sep 13 '07 #1
4 1245
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

"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(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


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(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

Sep 13 '07 #3
roger_27 wrote:
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
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
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"...
7
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: ...
4
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...
5
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...
10
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...
3
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( )....
1
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...
34
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...
7
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...
4
by: Energizer100 | last post by:
public class fly extends Actor { public fly() { setColor(Color.BLACK); } public fly(Color flyColor) { setColor(flyColor);
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.