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


Delphi: Declaring and initialising constant arrays

Posted in Delphi,programming,Software 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).

Firefox compatible xPath functions in JavaScript

Posted in Ajax,JavaScript,programming by james on November 19th, 2008

There are a lot of quirks between different browsers, in particular though are the differences in the way each browser handles the DOM in JavaScript.

Internet Explorer, for the most part, implements MSXml 4.0 or higher. However that’s (obviously) a Microsoft technology, and the standard implementation doesn’t support XPath in the DOM.

In particular the selectNodes() and selectSingleNode() functions are of particular use when manipulating XML or the DOM. Using these functions you can parse an XPath expression and get a nodelist of elements in your DOM that match the expression.

(more…)

howto: Format numbers in JavaScript

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

Formatting Numbers

Numeric formatting in JavaScript can be very tedious. From handling non-numeric characters, to multiple-currency symbols, to alternate negative display methods. The functions below should be valuable in assisting to format numbers and currencies of all types.

(more…)

Delphi: Minimising child forms in an SDI application

Posted in Delphi,programming,Software 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…)

JavaScript Trim() function

Posted in JavaScript,programming by james on March 20th, 2008

Trim(), is one function that we all use almost every day in most languages, whether it’s a core function of the language you’re using or a member (or prototype in JavaScript) function of the String object itself (as in C#), it’s invaluable for developers.

Unfortunately JavaScript doesn’t have this sort of thing built in, so here’s how to implement your own trim() functions (as well as ltrim() and rtrim()) in various ways depending on the level of backward compatibility you require.

(more…)

howto: GET a URL parameter in JavaScript

Posted in JavaScript,programming by james on February 11th, 2008

A useful little piece of JavaScript for retrieving a URL parameter (Query String) from your page. If a requested parameter doesn’t exist it will return an empty string rather than a NULL object. It will also handle the inclusion of anchor tags in the URL and exclude them from possible results.

e.g. http://www.yesjames.com/test.php?myparam=justatest
calling “gerURLParam(‘myparam’) will return “justatest”

function getURLParam(name) {
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var pattern = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp(pattern);
  var aResult = regex.exec(window.location.href);
  if ( aResult == null ) {
    return "";
  } else {
    return aResult[1];
  }
}