By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,550 Members | 1,733 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,550 IT Pros & Developers. It's quick & easy.

Call function

P: n/a
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
Nov 16 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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" <pa************@poczta.onet.pl> wrote in message
news:Oa**************@TK2MSFTNGP10.phx.gbl...
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

Nov 16 '05 #2

P: n/a

"PawelR" wrote...
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
Nov 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.