473,395 Members | 1,641 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,395 software developers and data experts.

Simple Class/Form Question

Hi,

This question is somewhat basic. Is it possible to call a method in
frmMain, using another class? Here's a snippet:

partial class frmMain : Form
{
private void MessageThis(String text)
{
MessageBox.Show(text);
}
}

public class AnotherClass
{
public AnotherClass(text)
{
frmMain.MessageThis(text); //doesn't work
MessageThis(text); //doesn't work
//and so on...
}
}

I've dabbled with public and static methods, etc, but I can't seem to
find the correct way.

Thanks.
Nov 16 '05 #1
4 1515
MessageThis() is private, which means it can only be called from frmMain.

Making it public will make it callable from AnotherClass, but only if
AnotherClass has an instance of frmMain available to it, for example:

frmMain frm = new frmMain();
frm.MessageThis(text);

The above is a trivial example to get the idea across of needing an instance
variable to call the method from. I realize that you wouldn't likely
actually use it the way I illustrate.

Making MessageThis() public AND static would make it callable as
frmMain.MessageThis() (that is, will make the method available from the
class itself without requiring an instance) but I presume that MessageThis()
is supposed to receive a message on behalf of an instance of frmMain; if so,
then making the method static wouldn't work because the method wouldn't then
have access to instance data of frmMain.

If you could explain what you're actually trying to accomplish perhaps we
could find a way to your solution.

--Bob

"Typpo" <ot********@optonline.net> wrote in message
news:ut*************@TK2MSFTNGP10.phx.gbl...
Hi,

This question is somewhat basic. Is it possible to call a method in
frmMain, using another class? Here's a snippet:

partial class frmMain : Form
{
private void MessageThis(String text)
{
MessageBox.Show(text);
}
}

public class AnotherClass
{
public AnotherClass(text)
{
frmMain.MessageThis(text); //doesn't work
MessageThis(text); //doesn't work
//and so on...
}
}

I've dabbled with public and static methods, etc, but I can't seem to find
the correct way.

Thanks.

Nov 16 '05 #2
Using the following code:

frmMain frm = new frmMain();
frm.MessageThis(text);

Works fine. The problem is, I'm creating a new form in the process and
frm.Show() reveals a duplicate. The method is called correctly,
however, and the intended effect takes place on the duplicate form. The
original form isn't changed at all.

I'm sorry if I was vague. Here's what I am trying to do...

On the main form, the user presses a button that loads a dialog form
that he or she fills out. The dialog form creates a new class that
organizes the information, and I would like to have the class or the
dialog form call a method contained in the main form's class. This
method adds the information to a list on the main form.

Thanks for your help. I hope I'm making sense =/.
Bob Grommes wrote:
MessageThis() is private, which means it can only be called from frmMain.

Making it public will make it callable from AnotherClass, but only if
AnotherClass has an instance of frmMain available to it, for example:

frmMain frm = new frmMain();
frm.MessageThis(text);

The above is a trivial example to get the idea across of needing an instance
variable to call the method from. I realize that you wouldn't likely
actually use it the way I illustrate.

Making MessageThis() public AND static would make it callable as
frmMain.MessageThis() (that is, will make the method available from the
class itself without requiring an instance) but I presume that MessageThis()
is supposed to receive a message on behalf of an instance of frmMain; if so,
then making the method static wouldn't work because the method wouldn't then
have access to instance data of frmMain.

If you could explain what you're actually trying to accomplish perhaps we
could find a way to your solution.

--Bob

"Typpo" <ot********@optonline.net> wrote in message
news:ut*************@TK2MSFTNGP10.phx.gbl...
Hi,

This question is somewhat basic. Is it possible to call a method in
frmMain, using another class? Here's a snippet:

partial class frmMain : Form
{
private void MessageThis(String text)
{
MessageBox.Show(text);
}
}

public class AnotherClass
{
public AnotherClass(text)
{
frmMain.MessageThis(text); //doesn't work
MessageThis(text); //doesn't work
//and so on...
}
}

I've dabbled with public and static methods, etc, but I can't seem to find
the correct way.

Thanks.


Nov 16 '05 #3
Why don't you just pass a reference to your class object to the dialog form
so it can provide the information back to your main form.

Greg
"Typpo" <ot********@optonline.net> wrote in message
news:OF****************@TK2MSFTNGP15.phx.gbl...
Using the following code:

frmMain frm = new frmMain();
frm.MessageThis(text);

Works fine. The problem is, I'm creating a new form in the process and
frm.Show() reveals a duplicate. The method is called correctly,
however, and the intended effect takes place on the duplicate form. The
original form isn't changed at all.

I'm sorry if I was vague. Here's what I am trying to do...

On the main form, the user presses a button that loads a dialog form
that he or she fills out. The dialog form creates a new class that
organizes the information, and I would like to have the class or the
dialog form call a method contained in the main form's class. This
method adds the information to a list on the main form.

Thanks for your help. I hope I'm making sense =/.
Bob Grommes wrote:
MessageThis() is private, which means it can only be called from frmMain.
Making it public will make it callable from AnotherClass, but only if
AnotherClass has an instance of frmMain available to it, for example:

frmMain frm = new frmMain();
frm.MessageThis(text);

The above is a trivial example to get the idea across of needing an instance variable to call the method from. I realize that you wouldn't likely
actually use it the way I illustrate.

Making MessageThis() public AND static would make it callable as
frmMain.MessageThis() (that is, will make the method available from the
class itself without requiring an instance) but I presume that MessageThis() is supposed to receive a message on behalf of an instance of frmMain; if so, then making the method static wouldn't work because the method wouldn't then have access to instance data of frmMain.

If you could explain what you're actually trying to accomplish perhaps we could find a way to your solution.

--Bob

"Typpo" <ot********@optonline.net> wrote in message
news:ut*************@TK2MSFTNGP10.phx.gbl...
Hi,

This question is somewhat basic. Is it possible to call a method in
frmMain, using another class? Here's a snippet:

partial class frmMain : Form
{
private void MessageThis(String text)
{
MessageBox.Show(text);
}
}

public class AnotherClass
{
public AnotherClass(text)
{
frmMain.MessageThis(text); //doesn't work
MessageThis(text); //doesn't work
//and so on...
}
}

I've dabbled with public and static methods, etc, but I can't seem to findthe correct way.

Thanks.


Nov 16 '05 #4
Sorry Greg, I'm not sure what you mean by that. I have an array of
class objects.

How would I pass, say, myClass[2] to the dialog form and have it return
the info filled out back to the main form?

Thanks again.

news.microsoft.com wrote:
Why don't you just pass a reference to your class object to the dialog form
so it can provide the information back to your main form.

Greg
"Typpo" <ot********@optonline.net> wrote in message
news:OF****************@TK2MSFTNGP15.phx.gbl...
Using the following code:

frmMain frm = new frmMain();
frm.MessageThis(text);

Works fine. The problem is, I'm creating a new form in the process and
frm.Show() reveals a duplicate. The method is called correctly,
however, and the intended effect takes place on the duplicate form. The
original form isn't changed at all.

I'm sorry if I was vague. Here's what I am trying to do...

On the main form, the user presses a button that loads a dialog form
that he or she fills out. The dialog form creates a new class that
organizes the information, and I would like to have the class or the
dialog form call a method contained in the main form's class. This
method adds the information to a list on the main form.

Thanks for your help. I hope I'm making sense =/.
Bob Grommes wrote:
MessageThis() is private, which means it can only be called from
frmMain.
Making it public will make it callable from AnotherClass, but only if
AnotherClass has an instance of frmMain available to it, for example:

frmMain frm = new frmMain();
frm.MessageThis(text);

The above is a trivial example to get the idea across of needing an
instance
variable to call the method from. I realize that you wouldn't likely
actually use it the way I illustrate.

Making MessageThis() public AND static would make it callable as
frmMain.MessageThis() (that is, will make the method available from the
class itself without requiring an instance) but I presume that
MessageThis()
is supposed to receive a message on behalf of an instance of frmMain; if
so,
then making the method static wouldn't work because the method wouldn't
then
have access to instance data of frmMain.

If you could explain what you're actually trying to accomplish perhaps
we
could find a way to your solution.

--Bob

"Typpo" <ot********@optonline.net> wrote in message
news:ut*************@TK2MSFTNGP10.phx.gbl...
Hi,

This question is somewhat basic. Is it possible to call a method in
frmMain, using another class? Here's a snippet:

partial class frmMain : Form
{
private void MessageThis(String text)
{
MessageBox.Show(text);
}
}

public class AnotherClass
{
public AnotherClass(text)
{
frmMain.MessageThis(text); //doesn't work
MessageThis(text); //doesn't work
//and so on...
}
}

I've dabbled with public and static methods, etc, but I can't seem to
find
the correct way.

Thanks.


Nov 16 '05 #5

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

Similar topics

13
by: LRW | last post by:
Having a problem getting a onSubmit function to work, to where it popsup a confirmation depending on which radiobutton is selected. Here's what I have: function checkdel() { if...
4
by: JSM | last post by:
Hi, I have a form (FormA) which was loaded using Application.Run when my app loads. This form has a button which loads Form B using "FormB myForm=new FormB()". My question is, how do I access...
1
by: jm | last post by:
Easy probably, please read on. I know some of you have commented already about some of my socket question. I appreciate that. I have a Form1: static void Main() { Application.Run(new...
2
by: Hazzard | last post by:
I just realized that the code I inherited is using all asp.net server controls (ie. webform controls) and when I try to update textboxes on the client side, I lose the new value of the textbox when...
0
by: 42 | last post by:
I implemented a simple class inherited from Page to create a page template. It simply wraps some trivial html around the inherited page, and puts the inherited page into a form. The problem I...
1
by: Joe | last post by:
Hello All, I have a very simple form as you can here. This form has one Textbox and one Button named Download. In ASP after tying in the text box even if I hit enter, the form was getting...
4
by: Ranginald | last post by:
Sorry for the simple question but thanks in advance: My goal is to create reusale code for a web app in C#. I have written the code already as a windows app but here is where I am confused: ...
2
by: njuneardave | last post by:
Hey, this is a simple question. I am new to the whole visual C# scene. I am creating a simple "Find Word" dialog box. I have the menu working: File -> Find -> Find Word....then it pops up my...
3
by: RSH | last post by:
I have a simple question regarding inheritance in a web form. I have a DropDownList in an aspx form. It is called DropDownList1 I have a class that will be overriding the render event so I...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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,...
0
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...
0
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,...

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.