Connecting Tech Pros Worldwide Help | Site Map

Call function

PawelR
Guest
 
Posts: n/a
#1: Nov 16 '05
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


Dan Bass
Guest
 
Posts: n/a
#2: Nov 16 '05

re: Call function


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();
}



"PawelR" <pawelratajczak@poczta.onet.pl> wrote in message
news:Oa0MWlg1EHA.3092@TK2MSFTNGP10.phx.gbl...[color=blue]
> 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
>
>[/color]


Bjorn Abelli
Guest
 
Posts: n/a
#3: Nov 16 '05

re: Call function



"PawelR" wrote...
[color=blue]
> 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?[/color]

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


Closed Thread