Solved

Table['Field'] vs Table.FieldByName('Field')

Posted on 1997-10-15
2
623 Views
Last Modified: 2010-04-03
What is the difference between

Table['Field']
AND
Table.FieldByName('Field')

I was under the impression that the two methods both returned the same and that the first was quick-to-type
version of the second.

However in my program

 str := folders.fieldByName('name').AsString;

Works fine but
     
str := folders.['name'].AsString;

Genrates an exception.

Does anybody know why and how to use Table['field'] safely?
I cannot find it in the on-line help.
0
Comment
Question by:essbb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 5

Accepted Solution

by:
JimBob091197 earned 50 total points
ID: 1347614
Normally the 2 methods are interchangable.  However, if the field being returned is empty (i.e. null) then you cannot assign it to a string, as in "s := MyTable['MyField']".  You get an exception because you are trying to assign a null variant to a string type.

I.e. using the "MyTable['MyField']" method returns a variant, which can be null.  But, using the "MyTable.FieldByName('MyField').AsString" function will always return a string.  If the field is null, then the AsString function will return an empty string.

You could do the following, but you will end up typing more code anyway:
if (VarIsNull(MyTable['MyField'])) then
    s := ''
else
    s := MyTable['MyField'];

Normally, I only use the "MyTable['MyField']" method when assigning a value to the field.  When I read a value FROM the field, I use the AsString or AsInteger functions if there's a possibility of the return value being null.

Hope this is what you were looking for.
JB
0
 

Author Comment

by:essbb
ID: 1347615
JimBob,

Yes That seems to be the problem! I shouldn't be getting any Null
fields with my DB setup but it appears that I am. At least I know what bug(in my program) I am looking for now!

Thanks for your help
-Brett
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

695 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