Calling a class function from another class

G

Guest

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
 
G

Guest

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(MyForm form)
{
this.form = form;
}

public void DoSomething()
{
int formHeight = this.form.GetFormHeight();
}
}


public static void Main()
{
MyForm f = new MyForm();

StateMachine sm = new StateMachine(f);
sm.DoSomething();
}


Hope that helps
Mark Dawson
http://www.markdawson.org
 
?

=?iso-8859-1?Q?Lasse=20V=e5gs=e6ther=20Karlsen?=

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(StateMachineCompleted);
sm.Execute();
}

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

Example 2, interfaces:

public interface IStateMachineEvents
{
void Completed();
}

public class StateMachine
{
private IStateMachineEvents _Events;

public StateMachine(IStateMachineEvents events)
{
_Events = events;
}

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

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

public class MainForm : IStateMachineEvents
{
public void DoSomething()
{
StateMachine sm = new StateMachine(this);
sm.Execute();
}

void IStateMachineEvents.Completed()
{
...
}
 

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

Top