473,769 Members | 7,375 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Calling a class function from another class

Hi,

I have a windows form project. The form class has a number of methods in it
that i want to call from another class I have just created.

This class is a finite state machine and is created and kicked off in same
method where the applications' Run method is called. i.e main method in
Program.cs.

I'd appreciate suggesstions for calling methods in the form class from this
finite state maching class.

Regards
Macca
Mar 13 '06 #1
2 27263
Hi Macca,
if you want to call methods in the form class from your other class you
just need to have a reference to the Form class. For example:

class MyForm : Form
{
// some method we want to call
public int GetFormHeight()
{
return 200;
}
}
class StateMachine
{
private MyForm form;

public StateMachine(My Form form)
{
this.form = form;
}

public void DoSomething()
{
int formHeight = this.form.GetFo rmHeight();
}
}
public static void Main()
{
MyForm f = new MyForm();

StateMachine sm = new StateMachine(f) ;
sm.DoSomething( );
}
Hope that helps
Mark Dawson
http://www.markdawson.org
"Macca" wrote:
Hi,

I have a windows form project. The form class has a number of methods in it
that i want to call from another class I have just created.

This class is a finite state machine and is created and kicked off in same
method where the applications' Run method is called. i.e main method in
Program.cs.

I'd appreciate suggesstions for calling methods in the form class from this
finite state maching class.

Regards
Macca

Mar 13 '06 #2
> Hi,

I have a windows form project. The form class has a number of methods
in it that i want to call from another class I have just created.

This class is a finite state machine and is created and kicked off in
same method where the applications' Run method is called. i.e main
method in Program.cs.

I'd appreciate suggesstions for calling methods in the form class
from this finite state maching class.

Regards
Macca


I'd implement this either using events or interfaces. Either of those two
solutions would allow you to implement the code you need and make the state
machine call into your form, without the state machine being tied to that
particular form. Examples of both follow.

Example 1, events:

public class StateMachine
{
public event EventHandler Completed;

public void OnCompleted()
{
if (Completed != null)
Completed(this, EventArgs.Empty );
}

public void Execute()
{
OnCompleted();
}
}

public class MainForm
{
public void DoSomething()
{
StateMachine sm = new StateMachine();
sm.Completed += new EventHandler(St ateMachineCompl eted);
sm.Execute();
}

public void StateMachineCom pleted(Object sender, EventArgs e)
{
...
}
}

Example 2, interfaces:

public interface IStateMachineEv ents
{
void Completed();
}

public class StateMachine
{
private IStateMachineEv ents _Events;

public StateMachine(IS tateMachineEven ts events)
{
_Events = events;
}

public void OnCompleted()
{
if (_Events != null)
_Events.Complet ed();
}

public void Execute()
{
OnCompleted();
}
}

public class MainForm : IStateMachineEv ents
{
public void DoSomething()
{
StateMachine sm = new StateMachine(th is);
sm.Execute();
}

void IStateMachineEv ents.Completed( )
{
...
}

--
Lasse Vågsæther Karlsen
http://usinglvkblog.blogspot.com/
mailto:la***@vk arlsen.no
PGP KeyID: 0x2A42A1C2
Mar 13 '06 #3

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

Similar topics

4
2433
by: jarmopy | last post by:
Hi, I have made a service with C# and calling that service class from another C# program with remoting. (Referendes from the calling program) That service class is configured so that garpage collection is not used in this class. (singleton class + override InitializeLifetimeService ) The service class uses C++ unmanaged function from dll (using DLLimport).
10
1901
by: Shayne Wissler | last post by:
I've overloaded the global new operator, so that I can, detect when I've run out of memory: extern void* operator new(size_t s) { void *r = malloc(s); if (!r && s) { fprintf(stderr, "Error: No more memory."); exit(1); } return r;
3
2393
by: Ken | last post by:
hello, I would to know if it is possible to call an object in a function within a class. Meaning , In a class, A function X calling onto a function Y, and function Y we want one of the two calculation ( eg seconds , out of seconds and minutes) thanks , Ken
2
1919
by: William Payne | last post by:
Hello, consider these following two classes. A base class, class MDIChildWindow, and a class inherting from that base class, class Document. In the static base member function callback() I obtain a pointer to the child class and call the function on_mdiactivate() using this pointer. For some reason, the program executes MDIChildClass::on_mdiactivate() and not Document::on_mdiactivate(). Why? on_mdiactivate() is a virtual function in...
8
1626
by: Andreas Lagemann | last post by:
Hi, after browsing FAQ and archive for a while I decided that the following is a legal question. Consider this: Class Base { public: Base() {}
7
6606
by: Klaus Friese | last post by:
Hi, i'm currently working on a plugin for Adobe InDesign and i have some problems with that. I'm not really a c++ guru, maybe somebody here has an idea how to solve this. The plugin is written in C++ and it's calling a java application. This application displays a window and pushing a button is calling back the c++-plugin again.
15
11788
by: Bryan | last post by:
I have a multi-threaded C# console application that uses WMI (System.Management namespace) to make RPC calls to several servers (600+ ) and returns ScheduledJobs. The section of my code that performs the query is contained in a delegate function that I execute via a second thread. On 1 or 2 of the 600+ servers the query hangs. I've tried to use Thread.Join() coupled with a Thread.Abort() but this does not kill the thread. Based on...
5
3436
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS 5), but fails on IIS 6 running on a Win2003 server. The web uses Pages derived from a custom class I wrote (which itself derives from Page) to provide some common functionality. The Page_Load handler the failing webpage starts out like this: ...
4
2255
by: Bugs | last post by:
Hi, I wonder if anyone can help me out. I'm building a vb.net application that has a form with a panel that contains several other sub forms (as a collection of controls). What I'm wanting to do is call a generically named public sub in the top-most sub form in the panel. I have the name of the top-most form as a string (eg. g_TopForm = "frmCustomers") and I can reference the form with:
3
8634
by: drummond.ian | last post by:
Hello Everyone, This problem's been causing me a lot of trouble and I'm hoping somebody can help me out!! I have a dialog-based MFC application in visual studio 2003. I want to call a function called "OnPrintMsg" from my dialog class called "MyAppDlg.h". The function needs to be called from another class called "Client.cpp". The Client class (derived from the CSockets class) exists as described below.
0
9589
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10216
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10049
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9865
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8873
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5309
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3965
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3565
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.