I have a class that stores all of my variables and functions (for our purposes, DataClass), and a few different windows form classes that use the public variables and functions in DataClass.

I need to keep the values of the public variables in DataClass until the program closes.

I set the values of public member variables of DataClass in form1 class:

DataClass data;
       data.m_sComPort = "COM1";
       data.m_sBaudRate = "19200";

Then I close form1 to open form2 (Next button):

OnCancel();
	form2 frm2dlg;
	frm2dlg.DoModal();

form2 opens, and the values of the variables in DataClass that I just set in form1 are gone.

How do I not lose the values of those variables when going from form1 to form2?

This is my first post and thread, but DaniWeb has been a great resource for me for some time. Thanks.

Sorry if you already know this, but it is not the class that stores the data unless the members are static. You can make m_sComPort and m_sBaudRate static and access them as DataClass::m_sComPort and DataClass::m_sBaudRate. Otherwise you must be able to access the object instance 'data' from form2.

If you declare DataClass data inside a method or function it will be allocated on the stack and deallocated when the function returns. You can allocate an instance of DataClass on the heap with the 'new' keyword

DataClass* data = new DataClass();

This instance will remain in memory until you do

delete data;

Or you can declare 'data' as a global variable or within a context that exists during
the entire lifetime of the process (such as a local variable in the main function).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.