How to call a method in class B from class A in C#?

Status
Not open for further replies.

islouis

Junior Member level 1
Joined
Sep 24, 2004
Messages
17
Helped
1
Reputation
2
Reaction score
0
Trophy points
1,281
Activity points
194
Hi:

I would like to ask some question about class in C#, my OO wasnt very good

i have a Class A, and Class B , what if class A require to have instance of class B, and class B need to call a memeber of class A, just 1 member, what should i do?
like the following:

public class A : other class
{
{
private B test = new B();
}

public void Amember()
{
XXX
}

}

and in Class B
i want to call the Amemeber() fm class A.. if i inheritance, i will get exception.

Thanks
 

Re: C# question

i work on Java but nevermind both are the same, so i guess you want to call a method in class B from class A the solution
you might make this method Amember() a static method (method = function)then you can call it by this way
A.Amember();
this will work, it also might work in other way but it depends on what this method do.
if this method job is to get some variable from class A you might use another way.
by passing the variable when creating an instance from B.
 

Re: C# question

Thanks for your reply

however, using static will caused another problem because Amember will call other funciton like listBox1.Items.Add(message), unless i make the listBox1 become static too.

I heard people suggest using delegate, however i have not success on that yet.

Regards,

Louis
 

Re: C# question

you said in your message (and class B need to call a memeber of class A)
what do u mean by member? is it a member varialble or kind of method ?
 

Re: C# question

Hi shebo

Yes, Class B will call have to call a method in Class A, which is Amember()

Class A
{
private B instance = new B();

public Amember(string)
{
listBox1.Items.Add(); //post
}
}

Class B
{
i need to call Amember here
}

thanks
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…