I have, what I thought, was a trivial question however I am not quite
sure of an answer yet. I'd like a two dimensional array of strings
and integers. The first "column" will be string and the second "column"
will be integer. later i want the first value of array will caompre to table name so that query may run and the second value of array will compare to combox for some purpose how i want like it .
array[0..3] of Record = (
(string0, integer0)
(string1, integer1)
(string2, integer2)
(string3, integer3)
); in delphi

Dani AI

Generated

Both 's record-based idea and 's TStringList hint are reasonable starting points. A clear, type-safe choice is a dynamic array of a small record type (string + integer), assign values with SetLength and field assignment, and compare those fields to your table name or combobox value when needed. Integers do not retain leading zeros — store as string or format/pad when displaying.

A minimal example (different names from earlier posts) showing declaration, assignment and lookup:

type
  TPairRec = record
    Name: string;
    Id: Integer;
  end;

var
  Pairs: array of TPairRec;
  i, FoundId: Integer;
  s: string;
begin
  SetLength(Pairs, 2);
  Pairs[0].Name := 'abc'; Pairs[0].Id := 2;
  Pairs[1].Name := 'def'; Pairs[1].Id := 3;

  FoundId := -1;
  for i := Low(Pairs) to High(Pairs) do
    if AnsiCompareText(Pairs[i].Name, TableName) = 0 then
    begin
      FoundId := Pairs[i].Id;
      Break;
    end;

  if FoundId <> -1 then
    if FoundId = StrToIntDef(ComboBox.Text, -1) then
      { match found }

  // format with leading zeros for display:
  s := IntToStr(FoundId);
  while Length(s) < 3 do s := '0' + s; // yields '002'
end;

For larger/simple lookup needs, a generic TDictionary<string,Integer> (Generics.Collections) is faster and clearer for lookups. For compatibility with older code that relies on TStrings/TStringList, prefer storing a small TObject wrapper with the integer (shown below) instead of casting integers to Pointer — pointer casts break on 64-bit and are unsafe.

type
  TIntHolder = class
    Value: Integer;
    constructor Create(AValue: Integer);
  end;
constructor TIntHolder.Create(AValue: Integer); begin Value := AValue; end;

// usage: SL.AddObject('abc', TIntHolder.Create(2));
// retrieve: Value := TIntHolder(SL.Objects[i]).Value;
// remember to free the holder objects before freeing SL.

Summary: use a dynamic record array for a simple fixed list, TDictionary for fast lookups, and a TObject wrapper (not raw Pointer casts) when placing integers into a TStrings/ComboBox.Objects.

Recommended Answers

All 5 Replies

TRow = record
    Column1: string;
    Column2: Integer;
end;

TArray = array [0..3] of TRow;
commented: Brilliantly eligant +0

sir then how to assign value to TArray value like 'abc' 002, 'def' 003, etc plz give me a complete example.

plz need an example of it help any body

What have you tried?

You can use a StringList...

StringList.AddObject(string0, Pointer(integer0));

The string can be accesssed as StringList.Strings[a_Index];
the integer can be accesssed as integer(StringList.Objects[a_Index])

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.