Articles   Members Online: 3
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
Abbreviating Array Properties Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
12-Sep-03
Category
Algorithm
Language
Delphi 2.x
Views
158
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Jeff Guidotti

Wouldn't it be much easier to enter AStringList[Index] instead of 
AStringList.Strings[Index]? Well you can, and you can add this functionality to 
your own classes.

Answer:

This may be obvious to some, but it's also something that I haven't come across 
until now, so I figured it might be useful to someone out there. 

For many array properties found in classes in delphi, you can simplify access by 
abbreviating the call.  This means that you can use the syntax ArrayProperty[Index] 
as opposed to ArrayProperty.Items[Index]. 

Either way is appropriate, and there is no right or wrong, however here is what I 
think.  The advantage to using the abbreviated version make repeated calls easier 
to look at, especially if there are in one parameter list.  The only disadvantage I 
can think of is that someone else reading the code might not be able to determine 
what you are referencing without some thought, or knowledge of the class in 
question. 

Here's an example using a TStringList: 
1   
2   //Unabbreviated: 
3   
4   for I := 0 to MyStringList.Count - 1 do
5     ShowMessage(MyStringList.Strings[I]);
6   
7   //Abbreviated: 
8   
9   for I := 0 to MyStringList.Count - 1 do
10    ShowMessage(MyStringList[I]);


Again, in that example it really doesn't save much, but in my code I have some 
classes I created and have to repeatedly call different sometimes.  Here's an 
example of something I might do, and how the abbreviation cleans things up: 
11  
12  for I := 0 to UserList.Count - 1 do
13    if AUser.read(UserList.Items[I].Company, UserList.Items[I].User) then
14    begin
15      ACompanyStr := UserList.Items[I].Company;
16      AUserStr := UserList.Items[I].User;
17    end;


Now the code in this example is pretty useless, and some would say why not use a 
'with UserList.Items[I] do' statement to clean things up.  However often I might 
have these nested within other 'with' statments, so the 'with' solution is not 
available. 

Now look at the abbreviated code: 

18  for I := 0 to UserList.Count - 1 do
19    if AUser.read(UserList[I].Company, UserList[I].User) then
20    begin
21      ACompanyStr := UserList[I].Company;
22      AUserStr := UserList[I].User;
23    end;


It definetly reduces the amount of code, and I believe it does clean up the 
appearance.   

What makes this all possible is the use of the 'default' directive with the 
corresponding array property.  

This can be used for many different items in delphi already including TStringList, 
TListItem, etc. but there are some items that do not have default array properties 
and therefore you cannot access in the abbreviated fashion. 

If you attempt to access something that does not have the default array property, such as the TListView component (I.E. ListView[I].Caption instead of ListView.Items[I].Caption), you will recieve an error such as "Class does not have default property". 

			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC