473,387 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 1265
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Daniel Kay | last post by:
Hello! I have written two template classes which implement the observerpattern in C++. I hope I manage to desribe the problem I have. template<class T> class Observer { /* ... */ }; ...
7
by: Tim ffitch | last post by:
Hi I have created a VB dll file that contains common functions I use across various projects in VB, Access and Excel. Rather than have to code the functions in each I decided to use the dll...
2
by: rawCoder | last post by:
Hi I am having this InvalidOperationException with message Cannot call Invoke or InvokeAsync on a control until the window handle has been created This is raised when i try to invoke a method...
5
by: TS | last post by:
From my presentation layer, I call a validation method in my business layer that i pass a custom class to that holds all parameters. I am currently also passing an error message by reference so...
5
by: Stephen Barrett | last post by:
I have read many threads related to async fire and forget type calls, but none have addressed my particular problem. I have a webpage that instantiates a BL object and makes a method call. The...
9
by: Steve Richter | last post by:
in a generic class, can I code the class so that I can call a static method of the generic class T? In the ConvertFrom method of the generic TypeConvert class I want to write, I have a call to...
8
by: hoofbeats95 | last post by:
I don't think this should be this complicated, but I can't figure it out. I've worked with C# for several years now, but in a web environment, not with windows form. I have a form with a query...
1
by: noony | last post by:
Here is the error I get after calling a method (described below) : Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is usually a result of calling...
44
by: Steven D'Aprano | last post by:
I have a class which is not intended to be instantiated. Instead of using the class to creating an instance and then operate on it, I use the class directly, with classmethods. Essentially, the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.