Call function

P

PawelR

Hello Group,

In my application I have few class, and I want call function with "master
class". This is as master form (startClass) and option window (ClassA). My
question is how call function from startClass in classA?

public startClass : System.Windows.Forms.Form
{
static void Main()
{...}
public string Function1()
{...}
private callClassA()
{
ClassA classA = new ClassA();
classA.ShowDialog();
}
}

public ClassA : System.WIndows.Forms.Form
{
private void Fuction11()
{
//call Function1();
}

}

Thx PawelR
 
D

Dan Bass

There may be an easier way, but....

This depends on whether Function1() uses any member variables or calls other
methods in startClass. If it doesn't, then Function1() probably should be
moved into a Global class and made static.

If it does call other methods, then you could have a reference of it in your
child ClassA:
in ClassA have the following:

System.Windows.Forms.Form _parent = null;

public System.Windows.Forms.Form Parent
{
set
{
_parent = value;
}
}

// then you can call public methods on parent
public function example ()
{
if ( _parent == null )
return;

_parent.Function1();
}

Finally, in startClass, change to the following:

private callClassA()
{
ClassA classA = new ClassA();
classA.Parent = (System.Windows.Forms.Form)this;
classA.ShowDialog();
}
 
B

Bjorn Abelli

...
In my application I have few class, and I want call function
with "master class". This is as master form (startClass) and
option window (ClassA). My question is how call function from
startClass in classA?

There are in practice two ways of doing it. The first is to make Function1
"static", i.e. a "class-member" instead of an "instance-member"

In that case you can call it from any object with:

startClass.Function1();

However, that's not what I'd recommend, and would need Function1 to deal
with only static members itself, and my guess is that it wouldn't be
appropriate in your case.

The other way and what I'd recommend is to send a reference of the instance
of startClass to the ClassA upon instantiation.

Example:

public StartClass : System.Windows.Forms.Form
{
static void Main()
{...}
public string Function1()
{...}
private callClassA()
{
// Send a reference of this
// upon instantiation of ClassA

ClassA classA = new ClassA(this); // <--
classA.ShowDialog();
}
}

public ClassA : System.WIndows.Forms.Form
{
// We need an instance member of StartClass
// to be able to reference it

StartClass parent = null;

// We modify the constructor to receive
// a reference to the StartClass instance

public ClassA(StartClass parent) : base()
{
this.parent = parent;
}

private void Fuction11()
{
// And then we can call it...
parent.Function1(); // <--
}

}

----------

Notice that I also capitalized the name of startClass to StartClass, as
that's the common practice of naming classes.

// Bjorn A
 

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