Delphi Basics - Case Command
Delphi Basics - Case Command
DelphiBasics
Case A mechanism for acting upon different values of an Ordinal
Keyword
The Tag provides identification of the case element. // Calculations can also be used in the case statement
number := 17;
Case number mod 2 of
Related commands 0 : ShowMessage(IntToStr(Number)+' mod 2 = 0');
1 : ShowMessage(IntToStr(Number)+' mod 2 = 1');
Else Starts false section of if, case and try statements else ShowMessage(IntToStr(Number)+' mod 2 is unknown');
End Keyword that terminates statement blocks end;
If Starts a conditional expression to determine what to end;
do next
Packed Compacts complex data types into minimal storage
Record A structured data type - holding fields of data // Procedure to show the colour of a passed
Then Part of an if statement - starts the true clause procedure TForm1.ShowColour(colour : TPrimary);
begin
// Use a case statement to see the colour of the passed var
Author links // Note how important the else clause is, even though we have
// apparently covered all TPrimary values!
Case colour of
Buy Website Traffic at Red : ShowMessage('The colour is Red');
Buywebsitetrafficexperts.com
Green : ShowMessage('The colour is Green');
Buy Proxies at Blue : ShowMessage('The colour is Blue');
Buyproxies.io Yellow : ShowMessage('The colour is Yellow');
else ShowMessage('The colour is Unknown!');
end;
end;
Download this web site as a Windows program.
Show full unit code
type
// Declare a fruit record using case to choose the
// diameter of a round fruit, or length and height ohterwise.
TFruit = record
name : string[20];
Case isRound : Boolean of // Choose how to map the next section
True :
(diameter : Single); // Maps to same storage as length
False :
(length : Single; // Maps to same storage as diameter
width : Single);
end;
var
apple, banana, fruit : TFruit;
begin
// Set up the apple as round, with appropriate dimensions
apple.name := 'Apple';
apple.isRound := True;
apple.diameter := 3.2;
Delphi Programming Neil Moffatt 2002 - 2017. All rights reserved. | Home Page
http://www.delphibasics.co.uk/RTL.asp?Name=Case 2/2