Case (keyword)
From RemObjects Software
The "case of" statement
Case statements provide a convenient way to process different values contained in an ordinal value:
case status of 0: DoNothing; 1: DoMinor 2: DoDefault 3..7: DoMajor end;
This code is equivalent to:
if status=0 then DoNothing else if status=1 then DoMinor else if status=2 then DoMinor else if 3 <= status <= 7 then DoMajor end;
The else statement is optional and it is not necessary to test every possible value. Whenever possible the compiler will create a jump table.
Support for Strings
Oxygene also allows testing of string values:
method TestApp.ProcessStatus(status: String); begin case status of 'urgent': DoUrgent; 'high': DoHigh; 'low': DoLow; else DoNormal; end; end;
The "case type of" statement
As an expanded version of standard "case of" statements, "case type of" allows you to execute different code paths based on the type of a variable.
Example
case lMember type of CodeMemberField: GenerateField(...); CodeTypeConstructor: GenerateTypeConstructor(...); CodeConstructor: GenerateConstructor(...); CodeMemberMethod: GenerateMethod(...); CodeMemberEvent: GenerateEvent(...); CodeMemberProperty: GenerateProperty(...); CodeSnippetTypeMember: GenerateSnippetMember(...); else Output.WriteLine(lMember.Name+' '+lMember.GetType().Name); end;
The type checking is satisfied by the type or a descendant, so you need to be careful about the order of the statements.
Consider the following, where Employee is a descendant of Person:
case somebody type of Person: msg := msg_Person; Employee: msg := msg_Employee; end;
The above will never recognize an employee. The fix is simple, of course, just switch the order of the two statements.
Notes
While in general most applications of "case type of" are better handled by a proper polymorphic design of the class library at hand, there are cases where the design of the accessed library is outside of the developer's hands and the "case type of" construct then comes in handy.
See Also
Product: RemObjects Oxygene (formerly known as Chrome)
Current version: 3.0
Previous Versions: 'Joyride' (2.0), 'Floorshow' (1.5), 'Adrenochrome' (1.0)
Glossary — Keywords — Language Features — Platform Features — Samples — Articles — How To — Issues
Categories: Text | Oxygene | Language | Keyword
