Tag Archives: RTTI

Linking object/TValue to a control, VCL to FireMonkey

In this post, Tag properties and using Rtti.TValue data property as an alternative in FireMonkey / FMX.

Click / Select… now what?

One common challenge we face as developers is that the user clicks on something, we now need to find the object that is required to do the next task based on the item selected.

Sometimes this is from a dataset linked to the control and it may be taken care of for us, other times, we may need to find a specific object that then has the data we need.

Most of us have done it at some time….

Yes… that naughty little trick of using the tag property of a TComponent to store a pointer to an instance of an object that you want a quick way of reaching.

I remember doing this in a POS system where buttons that represented different stock items were linked to dynamically created screens. I also remember often using the TListView and thankfully, the TListViewItem in VCL had a Data property (of type Pointer) that you can set at runtime to an instance of an object. This was a little less naughty when moving to 64bit coding when Integer and Pointer all of a sudden became different sizes, (and if it wasn’t for some insight from the Delphi team to swap tag to NativeInt – lots of code would have broken).

The problem with a pointer property (or using a NativeInt to achieve the same goal) is that it leaves a lot of typecasting in your code, which never sat easy with me.

Continue reading Linking object/TValue to a control, VCL to FireMonkey

Using Generics & RTTI to get enum string name or enum value

Overview

This blog post looks at Enums and how multi-platform generics and RTTI (or what C# guys call reflection) make this really simple using common code on Windows, Mac OS X, iOS and Android using Delphi.

Starting with what an Enum is, the post then explores how you would traditionally have seen it written in code, before looking at the clean X-platform approach RTTI and Generics provide.

If your experienced in working with enums and want to find out how RTTI makes it easier, you may want to skip the first part of this post and get to the section about RTTI further down.

Enumerated Types to and from strings

An Enum, or Enumerated Type is a data type with a set of named values.

Enums are a great way to define a set of constants that are specific for a type and work with them in context, ensuring only one, or an array of allowed values is set.

OK, I’m going to use an example that maybe should have more values listed, but you get the point. It is the points of a compass – North, South, East or West.

type
  TCompass = (North, South, East, West);

var
  D : TCompass; // short version for Direction 
begin
  D := TCompass.North;
  case D of 
    North : ShowMessage('North');
    South : ShowMessage('South');
    East  : ShowMessage('East');
    West  : ShowMessage('West');
    raise Exception.Create('Unknown Direction');
  end;
end;

This provides one way to convert a Enum to a string using a case statement, but every time you make a change to your Enum types, you need to add in extra code… Call me lazy, but thats too much work!

Continue reading Using Generics & RTTI to get enum string name or enum value