Board index » cppbuilder » Access to Forms and structures within functions

Access to Forms and structures within functions


2005-06-28 02:43:41 AM
cppbuilder53
Being too old (at 75) to pick up C++ quickly, I need some help.
I've had to split a working program into functions, each of which tries to use the Form belonging to the caller and the structure representing a file record belonging to the caller. Can someone please correct this code to allow access to both Form and the I/O record structure?
Thank you.
Don
In the .h file is:
class TForm2 : public TForm
{
__published: // IDE-managed Components
TLabel *Label1;
TLabel *Label2;
TEdit *Edit1;
TLabel *Label3;
TEdit *Edit2;
TButton *Button1;
TButton *Button2;
void __fastcall Button2Click(TObject *Sender);
void __fastcall Button1Click(TObject *Sender);
void __fastcall FormKeyPress(TObject *Sender, char &Key);
private: // User declarations
public: // User declarations
// My declarations:
int primary(TObject* Form3, bool newrec, void* client, long location);
int secondary(TObject* Form9, bool newrec, void* clientcase, long location);
__fastcall TForm2(TComponent* Owner);
}
.
.
.
In the code responsible for processing Forms 3 and 9:
int TForm2::primary(TObject* Form3, bool newrec, void* client, long location)
{
.
.
.
Form3->Edit1->Text = lastname;
Compiler error message for the line above:
[C++ Error]Unit2.cpp(326):E2316 'Edit1' is not a member of 'TObject'
Form3->Edit1->Text = (String)client.last_name;
Compiler error message for the line above:
[C++Error]Unit2.cpp(347) E2294 Structure required on left side of. or.* :
What's the simplist solution to the above error messages?
Thanks again.
Don
 
 

Re:Access to Forms and structures within functions

Re-post of earlier message, corrected for line wraps:
Being too old (at 75) to pick up C++ quickly, I need some help.
I've had to split a working program into functions, each of
which tries to use the Form belonging to the caller and the
structure representing a file record belonging to the caller.
Can someone please correct this code to allow access to both Form and the I/O record structure?
Thank you.
Don
In the .h file is:
class TForm2 : public TForm
{
__published: // IDE-managed Components
TLabel *Label1;
TLabel *Label2;
TEdit *Edit1;
TLabel *Label3;
TEdit *Edit2;
TButton *Button1;
TButton *Button2;
void __fastcall Button2Click(TObject *Sender);
void __fastcall Button1Click(TObject *Sender);
void __fastcall FormKeyPress(TObject *Sender, char &Key);
private: // User declarations
public: // User declarations
// My declarations:
int primary(TObject* Form3, bool newrec, void* client, long location);
int secondary(TObject* Form9, bool newrec, void* clientcase, long location);
__fastcall TForm2(TComponent* Owner);
}
.
.
.
In the code responsible for processing Forms 3 and 9:
int TForm2::primary(TObject* Form3, bool newrec, void* client, long location)
{
.
.
.
Form3->Edit1->Text = lastname;
Compiler error message for the line above:
[C++ Error]Unit2.cpp(326):E2316 'Edit1' is not a member of 'TObject'
Form3->Edit1->Text = (String)client.last_name;
Compiler error message for the line above:
[C++Error]Unit2.cpp(347) E2294 Structure required on left side of. or.* :
What's the simplist solution to the above error messages?
Thanks again.
Don
 

Re:Access to Forms and structures within functions

Don P. wrote:
Quote
In the .h file is:

class TForm2 : public TForm
Is TForm2 the only class definition derived from TForm ?
Or did you define also somewhere TForm1, TForm3 and so ?
Quote
int primary(TObject* Form3, bool newrec, void* client, long location);
Think that you meant:
int primary(TForm2* Form3, bool newrec, void* client, long location);
But it could likewise be:
int primary(TForm3* Form3, bool newrec, void* client, long location);
You have to give more info as how you create an instance of TForm2
and how you call primary() on which forms.
So there is an instance of TForm2. Let say it is Form2.
Then you can do something like:
Form2->primary ( Form2, true, NULL, 0L );
or
Form2->primary ( Form2A, true, NULL, 0L );
Hans.
 

{smallsort}

Re:Access to Forms and structures within functions

Hans Galema < XXXX@XXXXX.COM >wrote:
Quote
Is TForm2 the only class definition derived from TForm ?
Or did you define also somewhere TForm1, TForm3 and so ?
TForm2 is the only class definition. It was supplied
automatically when I wrote the original code.
I know my basic problem is, I am trying to create and use
an ordinary 'C' function, and don't know how to translate the
requirement into C++.
Based on your previous reply, here is what I did:
class TForm2 : public TForm
{
__published: // IDE-managed Components
TLabel *Label1;
TLabel *Label2;
TEdit *Edit1;
TLabel *Label3;
TEdit *Edit2;
TButton *Button1;
TButton *Button2;
void __fastcall Button2Click(TObject *Sender);
void __fastcall Button1Click(TObject *Sender);
void __fastcall FormKeyPress(TObject *Sender, char &Key);
private: // User declarations
public: // User declarations
int primary(TForm2* Form3, bool newrec, void* client, long location);
int secondary(TForm2* Form9, bool newrec, void* clientcase, long location);
__fastcall TForm2(TComponent* Owner);
};
The above produces no error. Nor do I get an error on the
function definition in the snippit below; however I do get
an error on the last line of this snippet:
int TForm2::primary(TForm2* Form3, bool newrec, void* client,
long location)
{
if (newrec) // If constructing a new client record
{
Form3->Edit1->Text = lastname;
Form3->Edit1->Enabled = false; // New record must freeze last name
Form3->Edit2->Text = ""; // Blank out most of the other edit controls
Form3->Edit3->Text = "";
The line above produces the compiler error:
[C++ Error]Unit2.cpp(329) E2316 'Edit3' is not a member of 'TForm2'
Plus, in calling the function, which looks like:
funcret = primary(Form3,true,&Client,loc);
I get these two errors:
[C++ Error]Unit2.cpp(264): E2034 Cannot convert 'TForm3*' to 'TForm2*'
and:
[C++ Error]Unit2.cpp(264): E2342 Type mismatch in parameter 'Form3'(wanted 'TForm2*',got 'TForm3*')
Thanks for the help!
 

Re:Access to Forms and structures within functions

"Don P." < XXXX@XXXXX.COM >wrote:
Quote

The errors are correct. This line:
Form3->Edit3->Text = "";
is in error because the functions parameter list has Form3
defined as a pointer of type TForm2:
primary(TForm2* Form3 .... )
and TForm2 does not have a control named Edit3 thus the error:
E2316 'Edit3' is not a member of 'TForm2'
Quote
Plus, in calling the function, which looks like:

funcret = primary(Form3,true,&Client,loc);

I get these two errors:

[C++ Error]Unit2.cpp(264): E2034 Cannot convert 'TForm3*' to 'TForm2*'

and:

[C++ Error]Unit2.cpp(264): E2342 Type mismatch in parameter 'Form3'(wanted 'TForm2*',got 'TForm3*')
These error are also correct because you are passing in a
pointer of type TForm3 when you have defined that parameter to
be of type TForm2.
~ JD
 

Re:Access to Forms and structures within functions

"Don P." < XXXX@XXXXX.COM >wrote:
Quote

[...] I need some help.
Click File | New | Unit to add a new unit to the project, save
it as Common.cpp and make the unit look like this:
//-------------------------------------------------------------
#ifndef CommonH
#define CommonH
//-------------------------------------------------------------
// add any needed includes like maybe Unit3.h and Unit9.h
//-------------------------------------------------------------
int __fastcall GetPrimary( TForm3 *pForm, bool newrec, void* client, long location );
int __fastcall GetSecondary( TForm9 *pForm, bool newrec, void* clientcase, long location);
//-------------------------------------------------------------
#endif
//-------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Common.h"
//-------------------------------------------------------------
#pragma package(smart_init)
int __fastcall GetPrimary( TForm3 *pForm, bool newrec, void* client, long location )
{
// use pForm as if it were Form3
}
//-------------------------------------------------------------
int __fastcall GetSecondary( TForm9 *pForm, bool newrec, void* clientcase, long location)
{
// use pForm as if it were Form9
}
//-------------------------------------------------------------
Then in what ever Form or Unit that you need to use the above
functions, all you have to do is include the header Common.h.
I suspect that there is a better solution but you didn't show
enough code. I also suspect that both functions could be
combined and all you have to do is check what type of form was
passed in.
~ JD
 

Re:Access to Forms and structures within functions

Don P. wrote:
Quote
TForm2 is the only class definition. It was supplied
automatically when I wrote the original code.
So it is the classdefinition for your mainform. Now how many other
instances of TForm2 are there going to be ? It seems at least one
or two. But if there is only one form then get rid of the
form parameters.
Quote
int primary(TForm2* Form3, bool newrec, void* client, long location);
int secondary(TForm2* Form9, bool newrec, void* clientcase, long location);
change to:
int primary( bool newrec, void* client, long location);
int secondary( bool newrec, void* clientcase, long location);
Please explain why one form should know about the other forms
(in case there are more instances of TForm2).
Hans.
 

Re:Access to Forms and structures within functions

Hans Galema < XXXX@XXXXX.COM >wrote:
Quote
So it is the classdefinition for your mainform. Now how many other
instances of TForm2 are there going to be ? It seems at least one
or two. But if there is only one form then get rid of the
form parameters.
Sorry to confuse things. My original program had a Form2 with
corresponding TForm2 which Borland automatically generated. My
original program also had Form3, with its corresponding TForm3.
Within the code for TForm2 I plugged into Form3 items from a
file record then issued a ShowModal() for Form3. This worked
beautifully, and my user has used it bug free for a long time.
Now I must split the program such that I'll choose between
Form3 and a new Form9 at the user's whim.
I chose to split these requirements into functions, such that
from the Form2 code I could call for handling of Form3 or
Form9.
That's where my troubles started. Not understanding C++ well,
I tried to create ordinary C-type functions. Now the code in
these functions cannot reach their own forms, Forms 3 and 9,
respectively.
So, as far as I can tell there are only single instances of
TForm2 and TForm3. But how do I reach the forms from within
these functions, as I used to outside the functions?
 

Re:Access to Forms and structures within functions

"JD" < XXXX@XXXXX.COM >wrote:
Quote

"Don P." < XXXX@XXXXX.COM >wrote:
>
>[...] I need some help.

Click File | New | Unit to add a new unit to the project ...
Pretty sophisticated stuff for a guy who has trouble understanding C++. I'll try it, though, and thank you for taking all the trouble.
Don
 

Re:Access to Forms and structures within functions

"Don P." < XXXX@XXXXX.COM >wrote:
Quote

[...] But how do I reach the forms from within these
functions, as I used to outside the functions?
All you need to do is to make sure that the code inside the
functions can access a valid pointer to the desired form. This
can be the application instance (Form3) or another pointer of
the same type (TForm3) but with a different name - as long as
it's been correctly assigned. The code that I posted relies on
you passing the pointer to the desired form.
At the moment, the reason that you can't use Form3->Something
is because the functions have no concept of what Form3 is and
you'll have the same issue with the code that I posted until
you add the proper #include(s) to bring the definition for
TForm3 into the same scope as the functions. The same holds
true for TForm9.
~ JD
 

Re:Access to Forms and structures within functions

Turns out that 9/10 of the solution was to put some include
statements in the functions (#include "Unit2.h", etc.), and to
quit trying to pass the addresses of the forms. Doing that
gets me past that kind of compiler error.
It was your help, and that of Hans Galena, that clued me in.
I thank you both!
Don