Link to home
Start Free TrialLog in
Avatar of peter_b
peter_b

asked on

Beginner: sharing information between classes

win32, Visual C++6 , beginner (i mean real beginner :)

Sorry for this question but i really dont know. I am getting the time in one class and i am trying to access it in another class. But how do i do that ?

void CW1Dlg::SetData(float* newnndata, float* newppdata)
{

      chart3.SetData(newnndata);
      chart2.SetData(newppdata);
      CTime lastTime;
       lastTime = CTime::GetCurrentTime();



}

now i would like to access lastTime elsewhere. For example in another class

CTimeSpan MinutesX15 = CTimeSpan(0,0,1,0);
      CString display_time;
      CTimeSpan Minutesmin15 = CTimeSpan(0,0,15,0);
      lastTime -= Minutesmin15;

like this.

Can you give me some tips with sharing information in a program. I know its really basic but it would help me understand.


thanks...
Avatar of CJ_S
CJ_S
Flag of Netherlands image

How about having one function in the CW1Dlg which returns you the address of lastTime. Then you send that address to the other class which then can use that address. Only a few changes have to be made (ie, pointers and references), but you can do that on your own.

Avatar of forza
forza

Use the "friend" keyword to let the CW1Dlg class and the other class communicate.
Alternatively you could make lastTime global, it will work, but normally I try to avoid this...
Declare one more member function in CW1Dlg class as
public:
CTimeSpan GetLastTime();
void SetLastTime(CTimeSpan cTime);

then define as given below

CTimeSpan CW1Dlg::GetLastTime()
{
      return lastTime;
}
void CW1Dlg::SetLastTime(CTimeSpan cTime )
{
      lastTime = cTime;
}

To get/read the Last time use

CW1Dlg cW1DlgTemp;
CTimeSpan dtTempTime = cW1DlgTemp.GetLastTime();

To Set/Update the Last time use

CTimeSpan MinutesX15 = CTimeSpan(0,0,1,0);
CTimeSpan Minutesmin15 = CTimeSpan(0,0,15,0);

CTimeSpan dtTempTime = cW1DlgTemp.GetLastTime();

dtTempTime -= Minutesmin15;

cW1DlgTemp.SetLastTime(dtTempTime);

Try this
All the Best
-Vasudev

 
ASKER CERTIFIED SOLUTION
Avatar of abdij
abdij

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of peter_b

ASKER

thanks for the many solutions i like  abdij´s proposal most . How is "friend" working ? Can you give little example ?
Just a thought: Friend is a bad programming practice when trying to program in an object-oriented language, that should be avoided.  Providing a getTime and setTime method is the best way to go about it to maintain code that's easy to use and maintain.

Laminamia :)
Avatar of peter_b

ASKER

Adjusted points from 50 to 70
Avatar of peter_b

ASKER

"Have a static member variable in CW1Dlg say m_stcTime and 2 member functions CW1Dlg::SetTime(..) and CW1Dlg::GetTime(..) "

could you write down an example please. I come from Java. Beginner there too and i really dont understand the syntax of those ::

i am adjusting the points for some extra lesson if you dont mind ?
thanks...
If you have a class with all kinds of functions in it. You have to somehow fill in the functions (what they should do), thus you write out the functions like

void GetTime(void)
{
   ...
}

but how will the compiler know that it belongs to that certain class? It knows by using the double :
First you write the returntype, then the class and then you write the function name.

CJ
Hi Peter,

Could u pl. explain more on the context of usage of your variable 'lastTime'. Because the implementation depends upon the life time of the object and the security of data.
1. If the variable is used to store the Last time of any object of CW1Dlg then you can make use of getTime() and setTime(). But if each object of the class has its own lastdate (usaully)storage then the static usage will be wrong.
2. The usage the friend function almost voilates the O.O.Concept. Before using friend function think twise whether that context suggests that?. (i am worried about the security of lastTime)

Ans1:
if you need only static variable m_stcTime you need not have to have member function to use it. U can
1) Get the Last Date by  
CTimeSpan dtTempTime  = CW1Dlg::m_stcTime and
2) Set the Last Date by  CW1Dlg::m_stcTime = dtTempTime  and

Ans2:
if you need static variable and 2 static member functions then,

Example:
Declare members in CW1Dlg class as
public:
static m_stcTime;
static CTimeSpan GetLastTime();
static void SetLastTime(CTimeSpan cTime);

rest of the  code remains same as commented lasttime.

Try this out
bye
-Vasudev