Discussion:
Return's on Forms
(too old to reply)
w***@remov.yahoo.com
2007-06-06 16:31:21 UTC
Permalink
OK

After being shown the light on global variable being a bad thing. I'm
looking at how to pass information around.

Is it possible for a C++Builder Form to have a return value? And were in the
Unit ( Or how ) would the return be placed? But as I understand it, "return"
exits the function right then, so would any of the cleanup ( destructors ect
) occur?


A simple thought

struct Data
{
int a, b;
}

Data D1;


D1 = Form2->ShowModal();


And somewhere inside Form2--------------

Data D2;

D2.a = 1;
D2.b = 2;


return D2;

OR
Maybe the way to do it is pass Form2 the address of D1 ( &D1 ) and just let
the form manipulate the variable that way. But how ( or should ) I change
the header information

OK, Back to the C++ Builder Books, and see if I can figure it out.... Just
when I thought I had pointers figured out...LOL

Thanks in advance

Michael
Clayton Arends
2007-06-06 20:18:31 UTC
Permalink
I understand you are learning C++. I'll try to be as gentle as I can ;-)

Your concept of what a "return" does is off. "return" exits the current
function, procedure, or method. It is not a C++ specific operation nor does
Post by w***@remov.yahoo.com
But as I understand it, "return"
exits the function right then, so would any of the cleanup ( destructors
ect ) occur?
Cleanup of stack variables happens. Pointers are not cleaned up unless you
have used a smart pointer to clean them up for you. Smart pointers are
feature of C++ that you will learn down the road.
Post by w***@remov.yahoo.com
D1 = Form2->ShowModal();
The return type of ShowModal() is TModalResult. What is really getting
returned is the value of Form2->ModalResult. You cannot change this
behavior unless you overload ShowModal() thus making a new ShowModal()
method.
Post by w***@remov.yahoo.com
Maybe the way to do it is pass Form2 the address of D1 ( &D1 ) and just
let the form manipulate the variable that way. But how ( or should ) I
change the header information
TForm2 must know what "Data" is. This is accomplished by declaring your
struct "Data" in some common header file and then adding a #include in
TForm2's header. Or, if this type exists solely for TForm2 then you can
declare it in TForm2's header above the declaration of TForm2.

Then, in TForm2 you will add the following to the "public" section. This
isn't the best approach but since you are still learning which way is up
most people will let this slide:

public:
Data* D2;

__fastcall TForm2(TComponent* AOwner);

//...etc

Finally, when you create the form you will most likely use it like this:

Data d1;
TForm2* form = new TForm2(this);
form->D2 = &d1;
form->ShowModal();
form->Release(); // or "delete form;" though I prefer Release()
// do something with 'd1'

HTH,
- Clayton
w***@remov.yahoo.com
2007-06-08 13:11:40 UTC
Permalink
<Snipped>
Post by Clayton Arends
TForm2 must know what "Data" is. This is accomplished by declaring your
struct "Data" in some common header file and then adding a #include in
TForm2's header. Or, if this type exists solely for TForm2 then you can
declare it in TForm2's header above the declaration of TForm2.
Then, in TForm2 you will add the following to the "public" section. This
isn't the best approach but since you are still learning which way is up
Data* D2;
__fastcall TForm2(TComponent* AOwner);
//...etc
Data d1;
TForm2* form = new TForm2(this);
form->D2 = &d1;
form->ShowModal();
form->Release(); // or "delete form;" though I prefer Release()
// do something with 'd1'
HTH,
- Clayton
Clayton

Thanks for the push in the right direction. But.......

I did what you said, here is Unit2 ( Form2 that collects the information ),
included Unit3 which defines the struct "data", but the compiler throws me
an error....... "Type name Expected" and "Declaration Missing ;". I know
I'm missing something extremely basic, but it seems that I followed the
right steps.

Create the common header file with the struct definitions.
Include the file in the Unit's header.
Add the line of code to the classes Public section.
So, were did I screw up here.

Thanks

Michael


//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <vcl\Classes.hpp>
#include <vcl\Controls.hpp>
#include <vcl\StdCtrls.hpp>
#include <vcl\Forms.hpp>
#include "Unit3.h"
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TButton *Button1;
TButton *Button2;
TLabel *Label1;
void __fastcall Button2Click(TObject *Sender);


void __fastcall FormPaint(TObject *Sender);
private: // User declarations
public: // User declarations

data D3; // This is the only line I have added to the file

__fastcall TForm1(TComponent* Owner);

};
//---------------------------------------------------------------------------
extern TForm1 *Form1;

//---------------------------------------------------------------------------
#endif
w***@remov.yahoo.com
2007-06-08 13:28:36 UTC
Permalink
On 8-Jun-2007, ***@remov.yahoo.com wrote:
<snipped>
Post by w***@remov.yahoo.com
I did what you said, here is Unit2 ( Form2 that collects the information ),
included Unit3 which defines the struct "data", but the compiler throws me
an error....... "Type name Expected" and "Declaration Missing ;". I know
I'm missing something extremely basic, but it seems that I followed the
right steps.
Create the common header file with the struct definitions.
Include the file in the Unit's header.
Add the line of code to the classes Public section.
So, were did I screw up here.
Thanks
Michael
//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <vcl\Classes.hpp>
#include <vcl\Controls.hpp>
#include <vcl\StdCtrls.hpp>
#include <vcl\Forms.hpp>
#include "Unit3.h"
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TButton *Button1;
TButton *Button2;
TLabel *Label1;
void __fastcall Button2Click(TObject *Sender);
void __fastcall FormPaint(TObject *Sender);
private: // User declarations
public: // User declarations
data D3; // This is the only line I have added to the file
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
After some more testing ( OK shooting in the dark )

It gets better, If in the C++ IDE, I click the run key several times without
changing anything, after 4 tries, it compiles and runs. I get no complaints
if I declare a "data" variable within the program code, only if it is in the
class definition.

Thanks again

Michael
Duane Hebert
2007-06-08 13:39:19 UTC
Permalink
<***@remov.yahoo.com> wrote in message news:8Qcai.12$***@trnddc07...

<snip>
Post by w***@remov.yahoo.com
After some more testing ( OK shooting in the dark )
It gets better, If in the C++ IDE, I click the run key several times without
changing anything, after 4 tries, it compiles and runs. I get no complaints
if I declare a "data" variable within the program code, only if it is in the
class definition.
Can you show the defintion of data? From your first post
you show:

struct Data
{
int a, b;
}


There are two problems if this is still your declaration.
The case (you try to init data, not Data) and
there is a ; missing at the end of the struct declaration.
w***@remov.yahoo.com
2007-06-08 17:02:22 UTC
Permalink
Post by Duane Hebert
<snip>
Post by w***@remov.yahoo.com
After some more testing ( OK shooting in the dark )
It gets better, If in the C++ IDE, I click the run key several times without
changing anything, after 4 tries, it compiles and runs. I get no complaints
if I declare a "data" variable within the program code, only if it is in
the
class definition.
Can you show the defintion of data? From your first post
struct Data
{
int a, b;
}
There are two problems if this is still your declaration.
The case (you try to init data, not Data) and
there is a ; missing at the end of the struct declaration.
I should have included that in my post, What I have is for Unit3 ( the
common header file ). I think I have it right, it just seems really strange
to me that I if I compile/run the program several times the first 3-4 show
errors, then the next time it will compile and run, with the variables
seeming to work correctly.

//---------------------------------------------------------------------------
#ifndef Unit3H
#define Unit3H
#include "Unit1.h"
#include "Unit2.h"

struct data
{
int a, b;
};
//---------------------------------------------------------------------------
#endif

Loading...