469,568 Members | 1,401 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,568 developers. It's quick & easy.

How to call a method in the calling class?

Hi NG,

my problem is: I have a class A with a map on it. There is a public
methode "Refresh" for this map. Within A i call a class B (new B). In B
I modify some things, that would need to do a refresh in A.

My question: HOW can I call A.refresh() from B???

What is here a "best practice"?

Thank you very much

Rudi

Nov 17 '05 #1
3 1136
> My question: HOW can I call A.refresh() from B???

What is here a "best practice"?


Depends on how tightly coupled you want the A and B classes to be. You could
add a parameter of type A, pass in the this reference and then just call
Refresh on that from B. Or you could define and implement an interface (such
as IRefreshable) and use that as the parameter type. Or you could pass in a
delegate that represents just the Refresh method.
Mattias

Nov 17 '05 #2
Hi Lakesider,
you can pass the reference of class A to B in its constructor, then B will
be able to call A.Refresh(), like:

class A
{
public void DoSomething()
{
B bInst = new B(this);
bInst.SomeMethod();
}

public void Refresh()
{
}
}

class B
{
private A _aInst;

public B(A aInst)
{
_aInst = aInst;
}

public void SomeMethod()
{
//do work

_aInst.Refresh();
}
}

Or you could pass in a Reference to A in the method you call on B, if you
want to limit the scope of the A reference inside of B.

You could have A call Refresh on itself once it has finished calling the
method in B which updates the map values, but I wouldn't recommend this
baceause then class A needs to know what B is doing internally which is not a
good thing, it should not be A who is aware of this but rather class B.

Hope that helps
Mark R Dawson
http://www.markdawson.org

"Lakesider" wrote:
Hi NG,

my problem is: I have a class A with a map on it. There is a public
methode "Refresh" for this map. Within A i call a class B (new B). In B
I modify some things, that would need to do a refresh in A.

My question: HOW can I call A.refresh() from B???

What is here a "best practice"?

Thank you very much

Rudi

Nov 17 '05 #3
A simple solution like the following

public class A {

void DoSomthing() {
B b = new B();
b.DataChanged += new EventHandler(this.DataChangedHandler);
}

public void DataChangedHandler(object sender, EventArgs e) {
//refresh here
}

}

public class B {
public event EventHandler DataChanged;

public void ChangData(A a) {
//supposed to change some data
OnDataChanged(EventArgs.Empty);
}

protected void OnDataChanged(EventArgs e) {
DataChanged(this,e);
}
}

Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Daniel Kay | last post: by
5 posts views Thread by Stephen Barrett | last post: by
44 posts views Thread by Steven D'Aprano | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.