473,387 Members | 1,903 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.

pass values between classes

Hi,

I have Class A, B and C.

Class A has an instance of B. Class B has an instance of C. In the
instance of C, it generates some data that need to be passed back to
Class A. But Class C doesnot know anything about Class A.

I'm dealing with legacy code. What is the best way to implement it and
have the code change to existing classes such as A and B small? Any
ideas?

Thanks!

Nov 16 '05 #1
7 2048
This seems trivial but here it is.
class classa
{
private classb b;
private int csvalue;
classa()
{
b=new classb();
this.dosomething();
}
public dosomething()
{
csvalue=b.c.myvalue;
}
}
class classb
{
private classc c;
classb()
{
c=new classc();
}
}
class classc
{
public int myvalue;
classc()
{
myvalue=5;
}
}

Nov 16 '05 #2
Note that circular references are allowed in C#. If an object is
unreachable it
is eligible for garbage collection even if there are circular
references. so if A
references C and C references A but the application no longer holds
references to A or C, A and C can still be garbage collected.

If C is not allowed to call A, or callback A when done, or in some way
notify A
that it is done, then you are left with polling. A.b.isDone B.c.isDone
-->
A.b.GetData() B.c.GetData().

Regards,
Jeff
Class A has an instance of B. Class B has an instance of C. In the
instance of C, it generates some data that need to be passed back to
Class A. But Class C doesnot know anything about Class A.

I'm dealing with legacy code.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #3
This strikes me as a design problem that requires a larger knowledge of
your problem domain.

What are you trying to do? What are A, B, and C? When does the data
need to be passed back and what does that mean in domain terms?

Nov 16 '05 #4
Class A is a user-defined control: It's basically a window that
contains a panel and a paging navigator (with the back and forward
arrow buttons).

Class B is a class that instantiates class C to generate an array of
report objects.

Class C is a class that starts the invokes the printdocument.Print
object, and it takes care of printing each page of the report.

The graphics of each report page is passed back to Class A via the
generic event of OnEndPage of the print controller that takes
PageEventArgs as parameter, so the panel in Class A would show the
report page.

The original design works all fine until now I need to pass something
more than just what the PageEventArgs hold (graphics, bounds,
hasmorepages etc.). What I need to do is to pass an array of
information that contains the page number and the object of the report
back to Class A for future use. Since I can't use the same way of
passing the graphics back (the OnEndPage event), I need to come up with
some event on my own - Each time when a page is generated, my array
needs to be passed back to Class A. Please note that drawing each page
happens on worker thread while Class A is in the main thread.

I've got an idea of how to pass info from worker thread to main thread
- by using
MdiForm.BeginInvoke(new EventHandler(UpdatePageObjectReference),
pList);
in which pList is the array I need to pass over.

I defined the event handler "UpdatePageObjectReference" in Class A, but
at this point, in Class C, I don't hold an instance of Class A so it
won't recognize "UpdatePageObjectReference" event handler.

Please let me know about your thoughts.

Thanks,

Li
Bruce Wood wrote:
This strikes me as a design problem that requires a larger knowledge of your problem domain.

What are you trying to do? What are A, B, and C? When does the data
need to be passed back and what does that mean in domain terms?


Nov 16 '05 #5
If you have control over the source code then you can define your own
custom delegate, your own OnEndPagePrintEventArgs, your own event
dispatcher and pass the custom delegate in the constructor chain. Class
C
fires the custom event after printing each page and Class A subscribes
to the
event. Sample code at:

http://www.geocities.com/jeff_louie/call_console.htm

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #6
Thanks a lot for the ideas and sample code! I tried another approach by
defining an interface that contains one method. Have class A inherit
from the interface and expose this interface part to C. (passed through
B to C). Finally when C is ready to update A, it calls the method. Then
in A, I call Invoke using MarshalEventDelegate to marshall the data
over to the main thread. This has so far worked fine.

Nov 16 '05 #7
Glad to hear you got it to work. Interfaces would be my natural choice
in Java
also. I am coming to understand that delegates and events allow the
framework designer to build in _potential_ support for notification. So
a
WinForms program has build in support for the Closing event, but I do
not
have to subscribe to this event initially. If I decide that I want to
trap the
closing of the window I just do.

this.Closing += new CancelEventHandler(this.Form1_Closing);

private void Form1_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
*** if (isDirtyDocument)
*** {
******* if (MessageBox.Show ("Really? Quit Without Saving?",*
******************************************* "Draw",
******************************************* MessageBoxButtons.YesNo,*
******************************************* MessageBoxIcon.Question)==
DialogResult.No)
******* {
*********** // Cancel the Closing event from closing the form.
*********** e.Cancel = true;
******* }
*** }
}
Regards,
Jeff
I tried another approach by

defining an interface that contains one method.<

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

46
by: J.R. | last post by:
Hi folks, The python can only support passing value in function call (right?), I'm wondering how to effectively pass a large parameter, such as a large list or dictionary? It could achieved...
8
by: Brian F | last post by:
Exactly what the subject says. I have an ASP page that I want my C# windows application to open in Internet Explorer, and if possible have it send along a list of values from a list box. Thank you.
5
by: sparks | last post by:
Ok I have a person class and a files class to output to a database. the question I have is if person has name, and address for example can I pass person object instead of each individual item in...
5
by: adolf garlic | last post by:
Suggestions please for strategy to share values across app. Scenario: I have an asp.net app that uses some com components along with .net classes. Configuration settings for various things...
3
by: Brett | last post by:
I have several classes that create arrays of data and have certain properties. Call them A thru D classes, which means there are four. I can call certain methods in each class and get back an...
6
by: kath | last post by:
hi everyone......... I have a task, I have fragmented the task into subtask. I have planned to create a class to each subclass and one parent class to handle the sub tasks. Each subclass are...
1
by: spooky | last post by:
Hi, Is it possible to pass values two values between two javascript files if possible how?
12
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms....
6
by: =?Utf-8?B?Unlhbg==?= | last post by:
I am trying to pass a value from a texbox in Form1 to a textbox in Form2 using properties in VS2005 but it doesn't work; please help (project is attached). Code for Game Class: using System;...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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.