yesJames.com
ahuh… sure… what ever you say…


Delphi: Declaring and initialising constant arrays

Posted in Delphi, Software, programming by james on February 16th, 2009

In Delphi, arrays are, put simply, a list of values contained within a common variable, that are accessible by their index within that list.

But what if you don’t want your list of values to be able to be altered at run-time? You need them to be contained in a constant rather than a variable.

Example: to declare an array of abbreviated week names:

const
  Days: Array[0..6] of String = (
      'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'
  );

You could then access each name by the index of the day of the week within the Days constant.

i := DayOfWeek(Date); //returns an integer 0-6
ShowMessage('Today is  ' + Days[i]);

Another common use for constant arrays is to describe the values in an ENUM type:

type
  TStatusCodes = [
    scUnknown, scActive, scPending,
    scDisabled, scSuspended
  ];
const
  StatusString = Array[TStatusCodes] of String = (
    'Unknown', 'Active', 'Pending',
    'Disabled', 'Suspended'
  );

Then, the description string for each status code becomes available via the ordinal value of the status code itself:

ShowMessage('Your account is '+ StatusString[scActive]);

Would show a message box with the text “Your account is Active“. Of course in the real world, the status code enumeration value would come from a user object or some such similar method (like “function getAccountStatus(): TStatusCode;” perhaps).

Delphi: Minimising child forms in an SDI application

Posted in Delphi, Software, programming by james on November 19th, 2008

In an SDI application (Single Document Interface), the main form acs as the “container” for the entire application. This means that child forms, are exactly that – children of the application. The long and short of which, means that when you minimise the main form of the application, all forms are hidden. In addition to which, when you minimise a child form of the application, it doesn’t minimise to the task bar, instead it minimises to the application desktop. This also has the side effect that only the main application form itself has a task bar button.

So, How do I change this behaviour?

(more…)