Class hierachy and methods

T

Tom Machado

Hello,

I am working on a project and have become a little confused on how to
call methods from another class. Before I explain, here is an example:


class 0
{
main()
{
class 1 = new class1();
}
}

class 1
{
class1()
{
/*
now I want to call a method from class 0 (i.e.
class0.getVariable()) do I need to make a new instance or what since
this class already being called by class 0?
*/
}

}


Any idea? Also, class0 is a windows form and class1 is not, so calling
it again doesnt really make sense to me...

Thank you!

Tom
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Tom said:
I am working on a project and have become a little confused on how to
call methods from another class. Before I explain, here is an example:


class 0
{
main()
{
class 1 = new class1();
}
}

class 1
{
class1()
{
/*
now I want to call a method from class 0 (i.e.
class0.getVariable()) do I need to make a new instance or what since
this class already being called by class 0?
*/
}

}

Any idea? Also, class0 is a windows form and class1 is not, so calling
it again doesnt really make sense to me...

The second class need a reference to the first class.

If the first class A do:

B o = new B(this);

and the second class B looks like:

public class B
{
private A a;
public B(A a)
{
this.a = a;
}
...
}

then other metods in B can call:

a.somemethod();

Arne
 
T

tomb

You can pass a reference to class0 to the constructor of class1, then
call class0 functions through the reference object.

class 1 = new class1(this);

T
 

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