Solved

Question about the "FieldByName"

Posted on 2002-03-07
9
493 Views
Last Modified: 2011-09-20
Hi all,
Why this always return a Null value?
-------------------------->
FieldByName('EventMessage').Value := FieldByName('EventMessage').Value + 'test';
--------------------------<

Change to the following can get the correct result.
-------------------------->
FieldByName('EventMessage').Value := FieldByName('EventMessage').AsString + 'test';
--------------------------<

I also use "FieldByName('XXXXX').Value" in other place, Do I must change them to AsFloat/AsInteger/As..... ?

(Using D3)

Thanks, supwang
0
Comment
Question by:SupWang
9 Comments
 
LVL 2

Author Comment

by:SupWang
ID: 6847396
The DataSet is a TQuery.
0
 
LVL 2

Accepted Solution

by:
Drareg earned 20 total points
ID: 6847875

FieldByName('XXXXX').Value return a TVariant, so it maybe not possible to "work" with the result.
You can do FieldByName('X').Value := FieldByName('Y').Value but if you want to do some manipulation you have to use AsString, AsInteger , etc.
0
 
LVL 44

Expert Comment

by:CrazyOne
ID: 6848851
SupWang I would suggest not even using Value becuase as Drareg mentioned Value is a TVariant and using variants can slow down the proccess because it takes proccessor time to convert the data to the type that it is. Try getting in the habit of using AsString, AsInteger, AsDateTime etc.


The Crazy One
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 2

Author Comment

by:SupWang
ID: 6849194
Hi CrazyOne,
If it is a persistent field, Do I also need to change ".Value" to AsString, AsInteger, AsDateTime etc?

Regards, supwang
0
 
LVL 44

Expert Comment

by:CrazyOne
ID: 6849507
Yes, If you know the data type then it is best to use .AsString, .AsInteger, .AsDateTime. Experiment with this to get the overall feel of it. Example:

FieldByName('EventMessage').AsString := FieldByName('EventMessage').AsString + 'test';

One thing I do is to use a variable to capture the field's value. Mainly because I might need to use this value over and over again. Using the variable will cut down on the number of calls I need to make to the field which has the possible benefit of speeding up the process and shorting the length of the code making it easier to read. Again this something one needs to experiment with to see how it works for them.

var
  s: string;

begin

  s := FieldByName('EventMessage').AsString;
  FieldByName('EventMessage').AsString := s + 'test';

In this case the there is not much benefit in using the variable approach. But lets say we call on this field many times in our code then using the variable should improve performance. Example:

if s = 'a' then
  DoSomething
else if s = 'b' then
  DoSomething
else if s:= 'c' then
  DoSomething
else if s = 'd' then
  DoSomething
else if s = 'e' then
  DoSomething
else if s = 'f' then
  DoSomething
else if s = 'g' then
  DoSomething
else if s = 'h' then
  DoSomething;

Now a neat thing here is lets say we have label and we want to set the caption of this label to that of a field that is an integer type. We can do this rather easily by using the .AsString.

Example:

Label1.Caption := FieldByName('AnIntegerField').AsString;

This will convert the Integer field to a string for us.


The main point I am trying to make is no matter whether your code is dealing with values from a database or your code is manipulating or tracking values you create in the code try and stay away from using variants unless you absolutely need to. Variants definitely slow down the processing of the code.
0
 
LVL 9

Expert Comment

by:ITugay
ID: 6854338
Hi SupWang,

yes, field property Value is variant type. Empty field Value =  'NULL'.
Concatenation of string and 'NULL' always = 'NULL'.

You can notice the same behavior after executing SQL statement which has some concatenation.

e.g.

select FirstName || LastName from TheTable

the result will be empty if one of fields is NULL.

btw assignin empty string to empty field convert result to empty string.

FieldByName('EventMessage').Value := '';

FieldByName('EventMessage').Value := FieldByName('EventMessage').Value + 'test';

should return valid result.

------
Igor.
0
 
LVL 2

Author Comment

by:SupWang
ID: 6855878
Hi all,
How about the TTime field? Use "ThisTime.AsDateTime" instead of "ThisTime.Value"?

Regards, supwang
0
 
LVL 44

Expert Comment

by:CrazyOne
ID: 6855892
AsDateTime, you can always convert it to the format you need when you actually need to use it.
0
 
LVL 2

Author Comment

by:SupWang
ID: 6863912
Hi all,
Thank you very much. I had already changed many many lines in my program.

Hi CrazyOne,
Have some points for you, Please go here: http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=delphi&qid=20276807

Hi ITugay,
Have some points for you, Please go here:
http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=delphi&qid=20276808

Best Regards, supwang


0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Best Firemonkey component pack 1 100
tidtcpserver connection lost handle 2 83
Tviruailstringtree sort multi columns on header click 1 61
Delphi Yen format 3 31
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now