Hardcore Software

How hard it can get?

Using the HashTable in Delphi for hybrid collections February 16, 2007

Filed under: .NET,Delphi — Hemant @ 6:53 am

While working on one of my project I came across a problem of maintaining a collection of item who were of different types. As I was using Delphi.NET, I was able to use HashTable class (in System.Collections namespace) for this purpose.

Here is code snippet:

procedure UsingHashTable;
hybridCollection: Hashtable;
stringValue: string;
dateTimeValue: DateTime;
integerValue: Integer;
hybridCollection := Hashtable.Create;

stringValue := ‘A String…’;
dateTimeValue := System.DateTime.Now;
integerValue := 1000;

//stuff different type of values in the collection
hybridCollection.Add (1, stringValue); //Add a string
hybridCollection.Add (2, dateTimeValue); //Add a datetime
hybridCollection.Add (3, integerValue); //Add an integer

//Get the values back
stringValue := hybridCollection[1] as System.&String;
dateTimeValue := hybridCollection[2] as DateTime;
integerValue := hybridCollection[3] as Integer;

console.WriteLine (stringValue);
console.WriteLine (dateTimeValue);
console.WriteLine (integerValue);

Above code snippet produces following output:
A String…
16-Feb-07 12:15:05 PM


One Response to “Using the HashTable in Delphi for hybrid collections”

  1. Pankaj Jangid Says:

    Nice. Do share such snippets in future also.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s