[SOLVED] Casting integer to TObject lazarus / 21-General ---------------------------------------------------------------------- egsuh · 2024-11-17 06:12 Hello, Casting an integer to TObject is allowed in mode Delphi but not in mode Objfps. Is there any that I can achieve it, in  TStrings.AddObject method in following example. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var     AStrings: TStrings;      i: intger;    s: string; begin      AStrings := TStrings.Create;      AStrings.AddObject (s, TObject(i)); end; ---------------------------------------------------------------------- Packs · 2024-11-17 06:41 Why you are adding as a object. You can add as a string ---------------------------------------------------------------------- Amir61 · 2024-11-17 06:48 Then, they need to pay the cost of converting int to string and string to int. ---------------------------------------------------------------------- egsuh · 2024-11-17 08:14 QuoteWhy you are adding as a object. You can add as a string There is a string already, as var s.  The integer added as TObject will be used as a unique index --- different from itemindex which is the order in the itemlist. ---------------------------------------------------------------------- Jorg3000 · 2024-11-17 08:38 try Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i AStrings.AddObject (s, TObject(Pointer(i)));orAStrings.AddObject (s, TObject(Pointer(PtrInt(i)))); ---------------------------------------------------------------------- paweld · 2024-11-17 08:51 I would use TFPGMap from the fgl unit for thisCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses  fgl; type  TStrIntList = specialize TFPGMap; var  list: TStrIntList;  s: String;  i: Integer;begin  list := TStrIntList.Create;  list.Add(s, i);end; ---------------------------------------------------------------------- cdbc · 2024-11-17 09:29 Hi Like this:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var     AStrings: TStrings;     i: PtrUInt; // <------- HERE!    s: string;begin     AStrings := TStrings.Create;     AStrings.AddObject (s, TObject(i));end; PtrUInt & PtrInt are "Pointer sized integers", but only 'PtrUInt' is /safe/ to exchange for pointer, because it (& pointer) can't be negative and there's the range upwards... Regards Benny ---------------------------------------------------------------------- jamie · 2024-11-17 14:37 Quote from: egsuh on November 17, 2024, 07:12:39 amHello, Casting an integer to TObject is allowed in mode Delphi but not in mode Objfps. Is there any that I can achieve it, in  TStrings.AddObject method in following example. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var     AStrings: TStrings;      i: intger;    s: string; begin      AStrings := TStrings.Create;      AStrings.AddObject (s, TObject(i)); end;    1. Delphi is a mind reader , Most likely the correct answer because it seems good at doing such things 2.Your success with Delphi could have been a 32-bit app where pointers/Objects are also 32 bit, So nothing posted to the complaint department. Jamie ---------------------------------------------------------------------- Thaddy · 2024-11-17 15:27 I am more worried about Tstrings.create. Tstrings is meant to be derived from, but not used on its own:half of it is abstract. As already advised above: use TFpgmap or one of the other generics options. ---------------------------------------------------------------------- egsuh · 2024-11-18 03:29 Thank you for all of your replies. PtrUInt is excellent. This is to add an integer value to list components' items, which are TStrings. For example, TRadioGroups.Items, TCombobox.Items, etc. I want to extract the integer value associated with each item of Items. Some components provide "aliasvalues" or "values", which can be read instead of the item itself, but radio group, listbox, etc. do not provide them. ---------------------------------------------------------------------- PascalDragon · 2024-11-18 20:16 Quote from: cdbc on November 17, 2024, 10:29:08 amHi Like this:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var     AStrings: TStrings;     i: PtrUInt; // <------- HERE!    s: string;begin     AStrings := TStrings.Create;     AStrings.AddObject (s, TObject(i));end; PtrUInt & PtrInt are "Pointer sized integers", but only 'PtrUInt' is /safe/ to exchange for pointer, because it (& pointer) can't be negative and there's the range upwards... As this is simply used for storage of a typecast value it does not matter whether PtrUInt or PtrInt is used.