13.06.2011

Delphi and Generics

In this first developer-themed post, I am going to describe some basic stuff, before I delve into some real-world applications.

Delphi 2009 brought Generics to the Delphi-World. And since then, they are found in all of my Applications. Especially Generics.Collections are heavily used by me.

Before Generics.Collections I had to implement a TList for each Class I would like to store a variable amount of Objects. (Or I could use Contnrs.TObjectList and cast to TAnimal everytime I need one, but I don't find that very desirable)
interface

uses
  Classes;

type
  TAnimal = class
  private
    fName : string;
  public
    property Name : string read fName write fName;
  end;
  
  TAnimalList = class (TList)
  private
  procedure putAnimal(Index : integer; Value : TAnimal);
  function getAnimal(Index : integer) : TAnimal;
  public  
    property Items [Index : integer] : TAnimal read getAnimal write setAnimal;
  end;
  
implementation

procedure TAnimalList.putAnimal(Index : integer; Value : TAnimal);
begin
  inherited put(Index, Value);
end;

function TAnimalList.getAnimal(Index : integer) : TAnimal;
begin
  result := (TAnimal) inherited get(Index);
end;

And this is just for TAnimal, if there are any other classes, I would have to copy&paste the code for the List and replace the data types.

With Generics.Collections the same thing looks like this:
interface

uses
  Generics.Collections;

type
  TAnimal = class
  private
    fName : string;
  public
    property Name : string read fName write fName;
  end;

  TAnimalList = TList<TAnimal>

And I don't even need the alias "TAnimalList", I could use "TList<TAnimal>" instead.

Also, it is not limited to Classes, it can hold every datatype like integer, double, boolean and even records. In my case it is even better to use TObjectList<TAnimal>, which automatically frees its containing objects if it is freed.

Since all the Generics.Collectionshave an Enumerator the following is possible:

var
  fAnimals : TList<TAnimal>

procedure init;
var
  a : TAnimal;
begin
  for a in fAnimals do
  begin
    a.DoSomething;
  end;
end;

Generics.Collections has more then some simple Lists up its sleeve. There are also TQueue, which uses FIFO and TStack which uses LIFO to store its contents.

TDictionary is a little different, because its taking two type parameters. One for the key, one for the Value.
A case that I often stumble upon is to store (and recieve) some object or value for a specific string. And then I always use a TDictionary<String,TValue>

There's also a TObjectDictionary, which takes care of memory of the keys, the values or even both.
procedure init;
var
  ownsKeys, ownsValues, ownsBoth : TObjectDictionary<TKey, TValue>
begin
  ownsKeys := TObjectDictionary<TKey, TValue>.Create([doKeys]);
  ownsValues := TObjectDictionary<TKey, TValue>.Create([doValues]);
  ownsBoth := TObjectDictionary<TKey, TValue>.Create([doKeys, doValues]);
end;

That's it for today.

12.06.2011

Level Up

Time for another Level Up. In this week I became a certified Delphi Developer. There is also the Delphi Master Certification, but since this certification deals a lot with DataSnap, which I have not used very much, I don't think I'll take it anytime soon.

I think this is a good opportunity to blog about some techniques I am using in my every day work. Which may help some fellow Delphi Developers, who can learn a thing or two and also give me feedback to improve my work.

For those interested in these kind of posts, you can grab the feed for the "Developer"-Posts right here

11.11.2010

DOSPad



Nachdem die guten Leute von dospad ihren Quellcode veröffentlicht haben, konnte ich ohne Jailbreak den DOSBox-Port fürs iPad installieren. Und das öffnet natürlich eine Reihe von Möglichkeiten.
Aber ich lass erstmal ein paar Bilder für sich sprechen:


Xenon2


Monkey Island 1


Civilization 1

Desweiteren hatte ich noch Crystal Caves, Ascendancy und Doom ausprobiert. Davon läuft aber nur Crystal Caves einigermaßen flüssig. Doom ist unspielbar und Ascendancy mag er garnicht starten.

Wegen der eingeschränkten Tastatur sind Spiele mit hohem Geschicklichkeitsfaktor leider noch schwieriger als sie normalerweise sind. Desweiteren ist es schade, das es den Rechtsklick nicht im Fullscreenmodus gibt. Soweit ich weiß gibt es auch noch keine Unterstützung für externe Tastaturen, die soll aber noch kommen.

Fazit: Als Spielerei ganz nett, aber echte iPhone-Varianten von Spielen sind deutlich einfacher zu handlen.

PS: Als nächstes könnte man mal Win 3.11 probieren...

08.11.2010

Studienbücher

Gestern überkam es mich und ich habe tatsächlich mein Bücherregal aufgeräumt. Und schweren Herzens habe ich mich entschlossen ein paar alte Studienbücher von mir zu versteigern.

Von den folgenden vier konnte ich mich trennen:

Alle Bücher sind in einem guten bis Top-Zustand.

Hier der Link zu den eBay-Auktionen: http://shop.ebay.de/real_gloegg/m.html

02.06.2010

Not my president

Nur weil 'gefühlt' jedes zweite Kind in Deutschland aus ihrer Lende kam, ist sie noch lange nicht als "Mutter der Nation" qualifiziert.

Wer das Volk belügt, darf nicht Bundespräsidentin werden!
Link 1
Link 2

21.05.2010

Chips mit Geschmack

Ich bin ja durchaus ein Freund von speziellen Chipssorten. Gut das Walkers momentan gleich 5 ausgefallene Sorten im Angebot hat.






Die German Bratwurst schmeckt tatsächlich nicht schlecht und auch der American Cheeseburger ist lecker. Die restlichen Sorten werden dann beim Mittag verköstigt.

21.04.2010

Warum ich Oracle hasse

Grund #4312

CREATE TABLE T (
  id number,
  value float)
INSERT INTO T (id, value) VALUES (1, 3.3)
SELECT * FROM T
ID    Value
 1    3.29999981356

q.e.d.