473,385 Members | 1,798 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,385 software developers and data experts.

How to create a function that returns a whole structure in C++ Builder 6?

Hi guys!

I'm using the C++ Builder 6 Enterprise Edition where I create some
tables in Paradox and with the help of a structure i pass my data from
the form (Edit boxes) to the Paradox table with a speedbutton. (I am
also using several data modules where i keep all my tables and
queries).

First of all i need some "mechanism" to read the data from the tables
and also to be able to change these already stored values. I tried with

some DBEdit boxes...but it didn't work. It seems that at run time, when

i press Enter, the value that the user enters pass to the table without

letting me check the validity of user's values.

Is there any way to fix that?

If not, i thought of a way with a structure.
In the data module, in the header sheet ("project name".h) i have
(right down the public:) the struct:

struct PinSynt
{
int SyntAA;
AnsiString SyntKathg;
AnsiString SyntAKK;
float SyntSynt;
AnsiString SyntParat;
};

I'm trying to create a function that it will return whole structure. If

i do that, i'll replace the DBedits with Edits, as i'll pass the values

of my fields in the edits (as they do now with the DBedits).
Just below my struct, I'm writing:

struct PinSynt FunctionRead (int SyntAAKey); // where SyntAAKey =
Primary key

I compile the code, evething's ok. (Build All)

Now in the *.cpp sheet, i'm trying to write:

struct PinSynt TDM_Pinakas_Syntelestvn::FunctionRead (int SyntAAKey);
// where DM_Pinakas_Syntelestvn -> the name of my Data module.
{
struct PinSynt Help; // another struct of the same type for
help.

Query1->Close();
Query1->SQL->Clear();
// * SQL statements*//
//------------------------------//
// some code with if and other statements....//

return Help; // Help = whole structure.
}

Now if i compile the code...an error will occur:
'TDM_Pinakas_Syntelestvn::FunctionRead(int)' is not a member of
'TDM_Pinakas_Syntelestvn'

I'm trying again this way:

struct TDM_Pinakas_Syntelestvn::PinSynt FunctionRead (int SyntAAKey);
// where DM_Pinakas_Syntelestvn -> the name of my Data module.
{
struct PinSynt Help; // another struct for help.

Query1->Close();
Query1->SQL->Clear();
// * SQL statements*//
//------------------------------//
// some code with if and other statements....//

return Help;
}

Now several errors occur:

- Undefined structure 'PinSynt'
- Size of 'StructRead' is unknown or zero
- Undefined symbol 'Query1'
- and every single variable that has to do with my struct PinSynt (i.e.

Help.SyntAA, Help.SyntKathg etc.)

i tried several times but nothing so far. My last try was:

//-------- .h sheet ----------//
PinSynt FunctionRead (int SyntAAKey); // where SyntAAKey = Primary key

//-------- .cpp sheet ---------//
PinSynt TDM_Pinakas_Syntelestvn::FunctionRead (int SyntAAKey);
// or TDM_Pinakas_Syntelestvn::PinSynt FunctionRead (int SyntAAKey);
// or PinSynt FunctionRead (int SyntAAKey);
// it's always the same error!!! ===> Declaration syntax error
{
// the same code
}

I would be grateful if you could write a few lines of code so that i
could understand how i can create a function that returns a whole
structure.

Thanks in advance and please forgive my English..:)

Mar 10 '06 #1
7 5079
"Jimakos Bilakis" <di*********@yahoo.gr> wrote in message
news:11**********************@j52g2000cwj.googlegr oups.com
Hi guys!

I'm using the C++ Builder 6 Enterprise Edition where I create some
tables in Paradox and with the help of a structure i pass my data from
the form (Edit boxes) to the Paradox table with a speedbutton. (I am
also using several data modules where i keep all my tables and
queries).

First of all i need some "mechanism" to read the data from the tables
and also to be able to change these already stored values. I tried
with

some DBEdit boxes...but it didn't work. It seems that at run time,
when

i press Enter, the value that the user enters pass to the table
without

letting me check the validity of user's values.

Is there any way to fix that?

If not, i thought of a way with a structure.
In the data module, in the header sheet ("project name".h) i have
(right down the public:) the struct:

struct PinSynt
{
int SyntAA;
AnsiString SyntKathg;
AnsiString SyntAKK;
float SyntSynt;
AnsiString SyntParat;
};

I'm trying to create a function that it will return whole structure.
If

i do that, i'll replace the DBedits with Edits, as i'll pass the
values

of my fields in the edits (as they do now with the DBedits).
Just below my struct, I'm writing:

struct PinSynt FunctionRead (int SyntAAKey); // where SyntAAKey =
Primary key

I compile the code, evething's ok. (Build All)

Now in the *.cpp sheet, i'm trying to write:

struct PinSynt TDM_Pinakas_Syntelestvn::FunctionRead (int SyntAAKey);
// where DM_Pinakas_Syntelestvn -> the name of my Data module.
{
struct PinSynt Help; // another struct of the same type for
help.

Query1->Close();
Query1->SQL->Clear();
// * SQL statements*//
//------------------------------//
// some code with if and other statements....//

return Help; // Help = whole structure.
}

Now if i compile the code...an error will occur:
'TDM_Pinakas_Syntelestvn::FunctionRead(int)' is not a member of
'TDM_Pinakas_Syntelestvn'
The C++ standard does not define "Data module" and I don't know what you
mean by it.

You should only be using TDM_Pinakas_Syntelestvn::FunctionRead if

1. FunctionRead is a member of a class or struct or namespace called
TDM_Pinakas_Syntelestvn, and
2. you are defining the function outside the scope of the
class/struct/namespace, having previously declared it inside the
class/struct declaration or within the scope of the namespace.

Your declaration of FunctionRead means that it is not a member of a class or
struct and I would hazard a guess that namespaces are not being used either.
Accordingly, your declaration and definition are inconsistent. I could go
on, but I suspect that some time spent studying a C++ textbook is called for
here.
I'm trying again this way:

struct TDM_Pinakas_Syntelestvn::PinSynt FunctionRead (int SyntAAKey);
// where DM_Pinakas_Syntelestvn -> the name of my Data module.
{
struct PinSynt Help; // another struct for help.

Query1->Close();
Query1->SQL->Clear();
// * SQL statements*//
//------------------------------//
// some code with if and other statements....//

return Help;
}
Now several errors occur:

- Undefined structure 'PinSynt'
- Size of 'StructRead' is unknown or zero
- Undefined symbol 'Query1'
- and every single variable that has to do with my struct PinSynt
(i.e.

Help.SyntAA, Help.SyntKathg etc.)

i tried several times but nothing so far. My last try was:

//-------- .h sheet ----------//
PinSynt FunctionRead (int SyntAAKey); // where SyntAAKey = Primary key

//-------- .cpp sheet ---------//
PinSynt TDM_Pinakas_Syntelestvn::FunctionRead (int SyntAAKey);
// or TDM_Pinakas_Syntelestvn::PinSynt FunctionRead (int SyntAAKey);
// or PinSynt FunctionRead (int SyntAAKey);
// it's always the same error!!! ===> Declaration syntax error
{
// the same code
}

I would be grateful if you could write a few lines of code so that i
could understand how i can create a function that returns a whole
structure.

You need to read a good C++ textbook, not engage in random experimentation.
Returning a struct from a function is trivial. You just define it and return
it. That is not your problem. Your problem is you don't understand the
difference between

1. members variables and functions,

and

2. non-member variables and functions.

--
John Carson
Mar 10 '06 #2
On 10/03/2006, Jimakos Bilakis wrote:
First of all i need some "mechanism" to read the data from the tables
and also to be able to change these already stored values. I tried
with
some DBEdit boxes...but it didn't work. It seems that at run time,
when
i press Enter, the value that the user enters pass to the table
without
letting me check the validity of user's values.

Is there any way to fix that?
AFAIK not. I usually use a text box and save my data via ADO. This
gives flexibility in validation, and allows me to connect my app to any
SQL DBMS. The people over at the borland newsgroups may have more
details on this: you'll need to point your newsreader at
newsgroups.borland.com
struct PinSynt
{
int SyntAA;
AnsiString SyntKathg;
AnsiString SyntAKK;
float SyntSynt;
AnsiString SyntParat;
};

struct PinSynt FunctionRead (int SyntAAKey); // where SyntAAKey =
Primary key
This is a standalone function...
Now in the *.cpp sheet, i'm trying to write:

struct PinSynt TDM_Pinakas_Syntelestvn::FunctionRead (int SyntAAKey);
BTW, you should lose the semicolon here, and you don't need the
'struct' keyword.
Now if i compile the code...an error will occur:
'TDM_Pinakas_Syntelestvn::FunctionRead(int)' is not a member of
'TDM_Pinakas_Syntelestvn'


.... but you're now trying to define FunctionRead as a member of the
class TDM_Pinakas_Syntelestvn.

There are several ways of doing what you're attempting. Here's how I
would do it:

In the header file where class TDM_Pinakas_Syntelestvn is defined:

struct PinSynt
{
int SyntAA;
AnsiString SyntKathg;
AnsiString SyntAKK;
float SyntSynt;
AnsiString SyntParat;
};

Find the "class TDM_Pinakas_Syntelestvn" and add this to the public
section:

bool FunctionRead (int SyntAAKey, PinSynt& pinSynt);

In the .cpp file where the methods of class TDM_Pinakas_Syntelestvn are
defined:

bool TDM_Pinakas_Syntelestvn::FunctionRead (int SyntAAKey, PinSynt&
pinSynt)
{
bool result = false;
// perform the SQL query to find SyntAAKey
if (!(Query->Bof && Query->Eof))
{
Query->First();
pinSynt.SyntAA = Query->FieldByName("SyntAA")->AsInteger;
pinSynt.SyntKathg = Query->FieldByName("SyntKathg")->AsString;
// etc for the other fields
result = true;
}
return(result)
}

When you want to call this function to get your data from the database,
you can only call it where you've got an instance of
TDM_Pinakas_Syntelestvn. Let's say this is called myPinakasSyntelestvn
(I think that by default if this is your data module BCB6 creates an
instance for you, probably called DM_Pinakas_Syntelestvn. Once you've
got to grips with this, you probably want to manage the lifecycle of
your data modules yourself. In BCB6 you can only create a data module
on the heap, so myPinakasSyntelestvn must be a pointer.)

PinSynt myPinSynt;
if (myPinakasSyntelestvn->FunctionRead(42, myPinSynt))
{
// You got the data. Do something with it, eg:
mytextbox->Text = pinSynt.SyntKathg;
}
else
{
// do something with the error
}

I haven't compiled any of the above, so there may be the odd typo, but
the basics are there.

--
Simon Elliott http://www.ctsn.co.uk
Mar 10 '06 #3
Jimakos Bilakis wrote:

First of all i need some "mechanism" to read the data from the tables
and also to be able to change these already stored values. I tried with

look at TDataSet::Insert TDataSet::Edit TDataSet::FieldByName,
TDataSet::Fields TDataSet::Post
some DBEdit boxes...but it didn't work. It seems that at run time, when

i press Enter, the value that the user enters pass to the table without

letting me check the validity of user's values.

Is there any way to fix that?


look at TDataSet::BeforePost and other TDataSet events
Mar 10 '06 #4
Firstly John, you're right i need more study! I'm only at the begging
as i don't have more than 4 - 6 months since i started to create my own
projects.

Then again, Simon, you saved me!!.. :)
BTW, you should lose the semicolon here, and you don't need the
'struct' keyword.
Yes my mistake the semicolon! I don't write it in my code. Thanks for
your advise for the keyword struct BTW. I have erased it as well.
I think that by default if this is your data module BCB6 creates an
instance for you, probably called DM_Pinakas_Syntelestvn.
Yes, DM_Pinakas_Syntelestvn is the name.

No more annoying errors... Actually I would like to ask you:
In the header file where class TDM_Pinakas_Syntelestvn is defined:


Where exactly do you mean? Published, private or public? Here is what i
wrote in my code (in the .h sheet):

#ifndef Promier_DM_Pinakas_SyntelestvnH
#define Promier_DM_Pinakas_SyntelestvnH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <DB.hpp>
#include <DBTables.hpp>

class TDM_Pinakas_Syntelestvn : public TDataModule
{
__published: // IDE-managed Components ====> First
TQuery *QueryAA;
TQuery *QueryInsert;
TTable *PinakasSynt;
TQuery *QueryExists;
TQuery *QueryUpdate;
TSmallintField *PinakasSyntSynt_AA;
TStringField *PinakasSyntSynt_Kathg;
TStringField *PinakasSyntSynt_AKK;
TFloatField *PinakasSyntSynt_Synt;
TStringField *PinakasSyntSynt_Parat;
TQuery *QueryRead;
private: // User declarations
public: // User declarations
__fastcall TDM_Pinakas_Syntelestvn(TComponent* Owner);

// --- * --- * --- * --- * --- V A R I A B L E S --- * --- * --- * ---
*--- //
struct PinSynt
{
int SyntAA;
AnsiString SyntKathg;
AnsiString SyntAKK;
float SyntSynt;
AnsiString SyntParat;
};
// --- * --- * --- * --- * --- F U N C T I O N S --- * --- * --- * ---
*--- //
// variable functions of mine //

bool FunctionRead (int SyntAAKey, PinSynt& PinSyntRef);
};
//---------------------------------------------------------------------------
extern PACKAGE TDM_Pinakas_Syntelestvn *DM_Pinakas_Syntelestvn;
//---------------------------------------------------------------------------
#endif

Is that ok? Following your steps i wrote in .cpp file:

bool TDM_Pinakas_Syntelestvn::FunctionRead (int SyntAAKey, PinSynt&
PinSyntRef)
{
bool Result = false;

QueryRead->Close();
QueryRead->SQL->Clear();
QueryRead->SQL->Add(" SELECT * ");
QueryRead->SQL->Add(" FROM Promier_Syntelestes.DB ");
QueryRead->SQL->Add(" WHERE Synt_AA = " + IntToStr(SyntAAKey));
QueryRead->Prepare();
QueryRead->Open();

if (!(QueryRead->Bof && QueryRead->Eof))
{
QueryRead->First();
PinSyntRef.SyntAA =
QueryRead->FieldByName("Synt_AA")->AsInteger;
PinSyntRef.SyntKathg =
QueryRead->FieldByName("Synt_Kathg")->AsString;
PinSyntRef.SyntAKK =
QueryRead->FieldByName("Synt_AKK")->AsString;
PinSyntRef.SyntSynt =
QueryRead->FieldByName("Synt_Synt")->AsFloat;
PinSyntRef.SyntParat =
QueryRead->FieldByName("Synt_Parat")->AsString;

Result = true;
}
else
Result = false;

QueryRead->Close();

return Result;
}

Everything works fine.

Now I'm trying something else.
In the main form, i have a DBGrid and a PageControl with 2 tabs The
first for changing (TabSheet0) and the second for adding (TabSheet1)
values. The whole idea is when the user wants to change some of the
already stored values to do it with a simple click in the first Tab of
PageControl (TabSheet0). There he can click on any DBGrid's cell and
the values of the record to pass through the FunctionRead to the right
Edit Boxes. So far i wrote:

void __fastcall TPinakas_Syntelestvn::DBGrid1CellClick(TColumn *Column)
{
struct TDM_Pinakas_Syntelestvn::PinSynt StructRef;

if (DM_Pinakas_Syntelestvn->FunctionRead(StructRef.SyntAA,
StructRef))
{
Edit11->Text = StructRef.SyntKathg;
Edit12->Text = StructRef.SyntAKK;
Edit13->Text = StructRef.SyntSynt;
Edit14->Text = StructRef.SyntParat;
}
}

I know that it doesn't make sense, i'm still working on it. The problem
is that no matter which cell i press on (at run-time), i can only see
the first's record values!
I want every time i press a cell (and that cell belongs to a different
record), my Edit boxes to display the right values.

Thanks so much again!

Mar 10 '06 #5
On 10/03/2006, Jimakos Bilakis wrote:
In the header file where class TDM_Pinakas_Syntelestvn is defined:

Where exactly do you mean? Published, private or public?


That kind of depends where you want the function to be called from. If
you don't understand public, private or protected, one option is to
read Bruce Eckel's "Thinking in C++", available free at
http://mindview.net/Books/TICPP/ThinkingInCPP2e.html

There's also an excellent C++ FAQ at
http://www.parashift.com/c++-faq-lite/

However, as you've noticed, there's also a __published section in your
class. This is an extension to C++ which is specific to Borland C++
Builder. There are one or two other extensions in BCB, and a lot of
concepts you'll need to get to grips with which are more BCB than C++.
This is why I suggested that you consider posting in the Borland
newsgroups.

BCB is a good way of quickly writing W32 apps, but you will have to get
to grips with C++ and quite a few BCB concepts!
Here is what
i wrote in my code (in the .h sheet):
[snip]
class TDM_Pinakas_Syntelestvn : public TDataModule [snip] public: // User declarations [snip] struct PinSynt
{
int SyntAA;
AnsiString SyntKathg;
AnsiString SyntAKK;
float SyntSynt;
AnsiString SyntParat;
};
You've defined the type PinSynt within the class
TDM_Pinakas_Syntelestvn. This may or may not be what you want,
depending on where you plan to use this type.
QueryRead->SQL->Add(" SELECT * ");
QueryRead->SQL->Add(" FROM Promier_Syntelestes.DB ");
QueryRead->SQL->Add(" WHERE Synt_AA = " +
IntToStr(SyntAAKey)); QueryRead->Prepare();


Not a C++ issue, but consider using query parameters here: it may
improve performance.

[snip DBGrid problem]
This is probably better asked in the Borland groups: then instead of
getting just my opinions, you'll get answers from lots of BCB
developers and TeamB (unpaid Borland semi official support). And we
won't irritate the huge number of C++ developers on comp.lang.c++ who
don't use BCB!

See http://info.borland.com/newsgroups/

--
Simon Elliott http://www.ctsn.co.uk
Mar 10 '06 #6
Thanks again for your opinions and for your valuable help. I'll try to
post my problems in the Borland newsgroups. Sorry if i bothered you and
thank you for your time to apply to my post!

Mar 10 '06 #7
On 10/03/2006, Jimakos Bilakis wrote:
Thanks again for your opinions and for your valuable help. I'll try to
post my problems in the Borland newsgroups. Sorry if i bothered you
and thank you for your time to apply to my post!


Not at all: I just thought it worthwhile to direct you to the
newsgroups where I've received much valuable assistance myself.

--
Simon Elliott http://www.ctsn.co.uk
Mar 10 '06 #8

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

Similar topics

7
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I...
7
by: Felix Kater | last post by:
Hi, when I need to execute a general clean-up procedure (inside of a function) just before the function returns -- how do I do that when there are several returns spread over the whole function?...
14
by: Mike Labosh | last post by:
How do you define whether the return value of a function is ByRef or ByVal? I have a utility class that cleans imported records by doing *really heavy* string manipulation in lots of different...
4
by: Tamer via DotNetMonster.com | last post by:
I'm developing a graphic engine. I got all equations made, I need only to understand how I can create a function that, once accepted three values (x; y; z), it returns the two values (X; Y) coming...
2
by: Jake Barnes | last post by:
Using javascript closures to create singletons to ensure the survival of a reference to an HTML block when removeChild() may remove the last reference to the block and thus destory the block is...
3
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules'...
89
by: Cuthbert | last post by:
After compiling the source code with gcc v.4.1.1, I got a warning message: "/tmp/ccixzSIL.o: In function 'main';ex.c: (.text+0x9a): warning: the 'gets' function is dangerous and should not be...
0
by: MikeCS | last post by:
Hi all I would like some help with this issue. I am new to VB 2005 (OK with VB6) My problem is that I cannot seem to return a structure from a function. Example: I defined a structure in a...
4
by: etuncer | last post by:
Hello All, I have Access 2003, and am trying to build a database for my small company. I want to be able to create a word document based on the data entered through a form. the real question is...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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.