Remove Quotes From String

A Delphi function that will remove enclosing and double quotes from quoted text, useful for reading CSV data.

Using RemoveQuotes:

Parameters:

S: A string value containing the quoted text.

QuoteChar: The character that is to be treated as the quoted text.

Return Value:

This function will return a string value containing the input text with double and enclosing quote characters removed.

Example:

 

Move A Directory To The Recycle Bin

A Delphi function that will move a directory to the recycle bin.

Using RecycleDir:

Parameters:

Dir: A string value specifying the name of the directory to be moved to the recycle bin.

Return Value:

The function will return the Boolean value True if successful.

If the function fails, it will return the Boolean value False.

Example:

 

Move A File To The Recycle Bin

A Delphi function that will move the specified file to the recycle bin.

Using RecycleFile

Parameters:

FileName: A String value specifying the name of the file to be moved to the recycle bin.

Return Value:

The function will return the Boolean value True if successful.

If the function fails, it will return the Boolean value False.

Example:

Passing Parameters

Passing Parameters By Value

By default, when you pass a parameter to a Delphi function Delphi will create a copy of the value in a new variable, and pass that new variable to the function – this means you can change the value of the parameter inside the function, and the value of the variable passed will remain untouched. This is called passing by value.

Passing Parameters By Reference

Passing by value is very useful, but as the computer needs to copy the value before passing it to the method, is slightly less efficient.

const parameters

If you do not need to change the value of a parameter in a delphi method, you can specify the const keyword before the parameter:

The const keyword tells Delphi that you do not want to change the value of the parameter inside the procedure, and passes a reference to the original variable, and as it does not need to create a new variable when calling the procedure, your program will run a little faster.

If you try to change the value of a const parameter in a function, your program will not compile, as Delphi will treat this as an error in your program.

var parameters

If you want to change the value of a variable passed as a parameter, you can use the var declaration with your parameter. This tells Delphi that the method is allowed to change the value of the passed variable:

out parameters

Out parameters are very similar to var parameters (they are still passed by reference), but out tells Delphi that passed variable does not need to have been set before calling the function.

Returning More Than One Value

If you need to return more than one value from a function, you can do this by using out parameters:

In the example above, the procedure accepts two variables as parameters, and the value of each variable is changed by the procedure, resulting in two values being returned:

The message displayed above would be ‘Joe Bloggs is 50′. This is because the values of both varables were set by the function GetName.

Free All Objects In A TList

When programming in Delphi, I often find myself creating objects and adding them to a TList.  To safely deallocate these objects I would need to loop throgh the list, destroying each object each time I cleared that list.  I found myself doing this so often I wrote the following function to take care of this for me:

Using FreeList

The FreeList function does not destroy the list itself, but will loop through the list destroying each object held within the list.  It will automatically skip any nil pointers held in the list.

Parameters:

List: A TList object containing references to the objects to be destroyed.

Return Value:

This procedure has no return value.

Example:

 

Is Key Pressed

A Delphi function to determine if a key on the keyboard is being held down by the user.

Using IsKeyDown:

Parameters:

VK: The virtual key code of the key you want to check.

Return Value:

Returns True if the key specified is pressed, False if the key in not pressed.

Example: