| View previous topic :: View next topic |
| Author |
Message |
Tom Byars Guest
|
Posted: Mon Apr 10, 2006 5:03 pm Post subject: TDataSet::operator[] |
|
|
I only just realised this existed and thought it was a more compact way of
typing Tbl->Values["fieldname"] however, if I type Tbl["fieldname"] I get an
error stating Cannot convert 'char*' to 'int'. What am I missing? |
|
| Back to top |
|
 |
Vladimir Stefanovic Guest
|
Posted: Mon Apr 10, 2006 5:03 pm Post subject: Re: TDataSet::operator[] |
|
|
If it's a question of T(ADO)Table, then you can do
something like this:
// if field_name is string
ADOTable1->FieldByName("field_name")->AsString
or:
// type will be determined internaly
ADOTable1->FieldValues["field_name"]
or:
// 0 is the first field, and it's string
ADOTable1->Fields->Fields[0]->AsString
Probably there are some other notations, and ways, but
these three came to my mind.
--
Best Regards,
Vladimir Stefanovic
"Tom Byars" <tam118118 (AT) hotmail (DOT) com> wrote in message
news:443a8581 (AT) newsgroups (DOT) borland.com...
| Quote: | I only just realised this existed and thought it was a more compact way of
typing Tbl->Values["fieldname"] however, if I type Tbl["fieldname"] I get
an error stating Cannot convert 'char*' to 'int'. What am I missing?
|
|
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Mon Apr 10, 2006 7:03 pm Post subject: Re: TDataSet::operator[] |
|
|
"Tom Byars" <tam118118 (AT) hotmail (DOT) com> wrote in message
news:443a8581 (AT) newsgroups (DOT) borland.com...
| Quote: | I only just realised this existed and thought it was a more
compact way of typing Tbl->Values["fieldname"]
|
Which version of BCB are you using? The subscript '[]' operator is only
implemented for VCL properties in BCB 6 and BDS 2006 that are marked as
'default' in the Delphi source code.
Assuming 'Tbl' is a TTable, then there is no Values[] property in TTable.
What exactly is 'Tbl' declared as?
| Quote: | however, if I type Tbl["fieldname"] I get an error stating Cannot
convert 'char*' to 'int'. What am I missing?
|
Operators have to be called on an object *reference*, not an object
*pointer*. Since all VCL objects are dynamically allocated on the heap, you
have to deference the pointer in order to reach the actual object in order
to call operators on it. This is documented in the help file:
Subscript operators
One Object Pascal feature that has not been available to C++Builder
users is the concept of a default property (an indexed property that is
implied if you use a subscript operator on the class). VCL and CLX classes
that have default properties in Object Pascal now have a subscript operator
in C++ to provide much of the same convenience. It does differ in that you
must dereference the object pointer to use it. For example, now instead of
writing
Strings1->Strings[i]
you can write
(*Strings1)[i]
So, assuming that 'Tbl' has a 'default' property Values[], then you can
write:
(*Tbl)["fieldname"]
Gambit |
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Mon Apr 10, 2006 8:03 pm Post subject: Re: TDataSet::operator[] |
|
|
"Remy Lebeau (TeamB)" <no.spam (AT) no (DOT) spam.com> wrote in message
news:443aa838$1 (AT) newsgroups (DOT) borland.com...
| Quote: | Assuming 'Tbl' is a TTable, then there is no Values[] property
in TTable. What exactly is 'Tbl' declared as?
|
Nevermind. I see that you were referring to TDataSet. TDataSet does not
have a Values[] property. It does, however, have a FieldValues[] property
instead.
Gambit |
|
| Back to top |
|
 |
Tom Byars Guest
|
Posted: Tue Apr 11, 2006 1:03 pm Post subject: Re: TDataSet::operator[] |
|
|
Sorry Remy,
I should of course have written Tbl->FieldValues["fieldname"] instead of
Tbl->Values["fieldname"] (Tbl was meant to denote a TTable).
(*Tbl)["fieldname"] does return Tbl->FieldValues["fieldname"] although the
(*) kinda retracts from brevity.
Thanks to yourself and Vladimir. |
|
| Back to top |
|
 |
Tom Byars Guest
|
Posted: Tue Apr 11, 2006 1:03 pm Post subject: Re: TDataSet::operator[] |
|
|
Any idea why this doesn't work
Tbl->Edit();
(*Tbl)["fieldname"]=5;
Tbl->Post();
compiles and executes OK but, the field value isn't changed to 5.
Also, trying to inspect (*Tbl)["fieldname"] gives the error "E2094:
'operator +' not implemented in type 'Dbtables::TTable' for arguments of
type 'char *'.
"Tom Byars" <tam118118 (AT) hotmail (DOT) com> wrote in message
news:443b9642 (AT) newsgroups (DOT) borland.com...
| Quote: | Sorry Remy,
I should of course have written Tbl->FieldValues["fieldname"] instead of
Tbl->Values["fieldname"] (Tbl was meant to denote a TTable).
(*Tbl)["fieldname"] does return Tbl->FieldValues["fieldname"] although the
(*) kinda retracts from brevity.
Thanks to yourself and Vladimir.
|
|
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Tue Apr 11, 2006 7:03 pm Post subject: Re: TDataSet::operator[] |
|
|
"Tom Byars" <tam118118 (AT) hotmail (DOT) com> wrote in message
news:443b9fbb (AT) newsgroups (DOT) borland.com...
| Quote: | Any idea why this doesn't work
|
Does this work?
Tbl->Edit();
Tbl->FieldValues["fieldname"] = 5;
Tbl->Post();
If that works, then there is no reason for (*Tbl)["fieldname"] to not work.
Gambit |
|
| Back to top |
|
 |
Tom Byars Guest
|
Posted: Tue Apr 11, 2006 10:03 pm Post subject: Re: TDataSet::operator[] |
|
|
Remy
Tbl->Edit();
Tbl->FieldValues["fieldname"] = 99;
(*Tbl)["fieldname"] = 5;
Tbl->Post();
changes the field value to 99 ????
"Remy Lebeau (TeamB)" <no.spam (AT) no (DOT) spam.com> wrote in message
news:443bf6b5$2 (AT) newsgroups (DOT) borland.com...
| Quote: |
"Tom Byars" <tam118118 (AT) hotmail (DOT) com> wrote in message
news:443b9fbb (AT) newsgroups (DOT) borland.com...
Any idea why this doesn't work
Does this work?
Tbl->Edit();
Tbl->FieldValues["fieldname"] = 5;
Tbl->Post();
If that works, then there is no reason for (*Tbl)["fieldname"] to not
work.
Gambit
|
|
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Tue Apr 11, 2006 11:03 pm Post subject: Re: TDataSet::operator[] |
|
|
"Tom Byars" <tam118118 (AT) hotmail (DOT) com> wrote in message
news:443c1ae4 (AT) newsgroups (DOT) borland.com...
| Quote: | Tbl->Edit();
Tbl->FieldValues["fieldname"] = 99;
(*Tbl)["fieldname"] = 5;
Tbl->Post();
changes the field value to 99 ????
|
Are you asking me or telling me? ;-)
If you look in db.hpp, you can exactly what the '[]' operator is really
doing:
public:
Variant operator[](AnsiString FieldName) { return
FieldValues[FieldName]; }
Because the Variant is being returned by value instead of by reference, and
the FieldValues property getter method is always being invoked, you will not
be able to use the (*)[] syntax to assign values, only read them.
Gambit |
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Wed Apr 12, 2006 12:03 am Post subject: Re: TDataSet::operator[] |
|
|
"Remy Lebeau (TeamB)" <no.spam (AT) no (DOT) spam.com> wrote in message
news:443c2ea4$1 (AT) newsgroups (DOT) borland.com...
| Quote: | Because the Variant is being returned by value instead of by reference,
and the FieldValues property getter method is always being invoked,
you will not be able to use the (*)[] syntax to assign values, only read
them. |
I have reported the issue to QC #27241, as the issue still exists in BDS and
effects every class that has a default property.
Gambit |
|
| Back to top |
|
 |
Tom Byars Guest
|
Posted: Wed Apr 12, 2006 9:03 am Post subject: Re: TDataSet::operator[] |
|
|
OK Remy, thanks.
"Remy Lebeau (TeamB)" <no.spam (AT) no (DOT) spam.com> wrote in message
news:443c2ea4$1 (AT) newsgroups (DOT) borland.com...
| Quote: |
"Tom Byars" <tam118118 (AT) hotmail (DOT) com> wrote in message
news:443c1ae4 (AT) newsgroups (DOT) borland.com...
Tbl->Edit();
Tbl->FieldValues["fieldname"] = 99;
(*Tbl)["fieldname"] = 5;
Tbl->Post();
changes the field value to 99 ????
Are you asking me or telling me? ;-)
If you look in db.hpp, you can exactly what the '[]' operator is really
doing:
public:
Variant operator[](AnsiString FieldName) { return
FieldValues[FieldName]; }
Because the Variant is being returned by value instead of by reference,
and
the FieldValues property getter method is always being invoked, you will
not
be able to use the (*)[] syntax to assign values, only read them.
Gambit
|
|
|
| Back to top |
|
 |
|