Delphi32.com - Home!
| Home/News | Downloads | Forums | D32 Magazine | Resources | Info and Facts |  
 
 How to use sets in code, such as adding to TDBGrid.Options, at run time


I know how to use the object inspector to change the value of a set, such as TDBGrid.Options, but how do I manipulate sets in code?   

    To use sets, you will have to use square brackets to refer to set elements. Sets can have at most 256 elements in them. Here is an example of using and declaring sets:

    Sample Code
    type // Here are some examples of declaring set types. // A variable of TMyMonth can take one and only one of // one of its members. TMyMonth = (Jan, Feb, Mar, Apr, May); // TMyMonthSet can be an empty set (no months in it), or // contain any combination of the above months. TMyMonthSet = set of TMyMonth; // TMySubMonthSet is a subset of the above set, only able // to take on the first 3 months TMySubMonthSet = set of Jan..Mar;

    // TMyIntSet can be an empty set, or any combination of integers // in the range of to 5. TMyIntSet = set of .. 5; // TMySubIntSet can be an empty set, or any combination of integers // in the range of to 5. TMySubIntSet = set of ..5;

    // Here are some examples of declaring set variables var MyMonth: TMyMonth; // can only be one month (NOT a set) MyMonthSet: TMyMonthSet; // can be more than one month MyIntSet: TMyIntSet; // can be a combination of ints from to 5 only begin // The TMyMonth type can only take on one value at a time MyMonth := May;

    // Sets can take on more than one value, and notice the use of square // brackets MyMonthSet := ;Jan, Mar, May]; // MySubMonths can only take the values Jan..Mar. If you try to set it // to May, you won't get a compile time error, but the set will be empty.

    MyIntSet := ;20]; // 20 is past the range for the set..so this should do nothing! if MyIntSet = ;] then ShowMessage('MyIntSet is empty! (as expected...)');

    // Here are some common operations you can do with sets: // + Union operator MyMonthSet := MyMonthSet + ;Feb, Apr]; // Add 2 months to the set // - Difference operator MyMonthSet := MyMonthSet - ;MyMonth]; // Remove May from the set MyMonthSet := MyMonthSet - ;Jan..Apr]; // Remove Jan through Apr // The set should now be empty (use the equality operator = to test sets) if MyMonthSet = ;] then ShowMessage('MyMonthSet is empty! (as expected...)');

    // * Intersection operator MyMonthSet := ;Jan, Feb] * ;Jan, Apr]; if MyMonthSet = ;Jan] then ShowMessage('MyMonthSet is Jan! (as expected...)');

    // <= Subset operator if ;Jan, Apr] <= ;Jan..Apr] then ShowMessage(' ;Jan, Apr] <= ;Jan..Apr], as expected...'); // <= Subset operator is the opposite of the above

    // To test if an ordinal is in the set, us the "in" operator if Jan in ;Jan..Feb] then ShowMessage('Jan in set ;Jan..Feb]'); end.

     



  << Previous Faq     Complete List     Next Faq >>  



 
 Hits/month  2,500,000+ 
 Downloads
 (Since May 2000)
 7,393,709 
 Total Files  6,023 
 Forum msgs  7,670 
 Articles/FAQs  70+/900+ 
Kylix
Tips n Tricks
FAQs
Knowledge Base
Bug Listings
Articles
Books
Newsgroups
Links
Submissions
Testimonials
Advertising
Contact Us
About Us
Search Amazon:
Top Selling Software at Amazon

| Home/News | Downloads | Forums | Resources | Info and Facts | Testimonials |
  Site Search:
 


Comments/Problems: Webmaster@delphi32.com
Copyright © 1998-2006, Delphi32.com. All rights reserved.
Terms of Use