Question

In Java we have something like VarName.toString to indetify the name of the var.

I have a code in Delphi and I need to achieve the same thing, because I have a record type that has many sub-strings that I need to identify:

type
THierarchyAccess = String; // receive the user permissions from the MySQL DB
THierarchy = record
      MOD_HIERARQUIA   : THierarchyAccess; // 'BROWSE_ONLY', 'MANAGE', 'NONE'...
      MOD_OPERADORES   : THierarchyAccess;
      MOD_ESTATISTICAS : THierarchyAccess;
      MOD_AUDITORIA    : THierarchyAccess;
      MOD_HOMEPAGE     : THierarchyAccess;
      MOD_HOTSITES     : THierarchyAccess;
      MOD_MATRIZ       : THierarchyAccess;
      MOD_NOTICIAS     : THierarchyAccess;
      MOD_VISITANTES   : THierarchyAccess;
          ...
          end;
...

function  TAccess.IsAccessPermited(apNeed, apHave: String): Boolean;
begin
// HERE I need to know not only the content of "apHave", 
// but if it came from THierarchy.MOD_HOTSITES or THierarchy.MOD_MATRIZ etc.
end;

...

if IsAccessPermited('BROWSE_ONLY', MyHierarchy.MOD_HOTSITES) then Form2.Open;

Is there an elegant way to identify what was the variable name passed to the function?

Was it helpful?

Solution

Delphi is not Java!

You will need to use completely different, strongly typed techniques. For example your code can be something like:

type
  THierarchyAccess = set of (haBrowseOnly, haManage, haNone {...});
  THierarchyMod = (hmHierarquia, hmOperadores, hmEstatisticas {...});
  THierarchy = array [THierarchyMod] of THierarchyAccess;
//...

function  TAccess.IsAccessPermited(apNeed: THierarchyAccess; apHave: THierarchy; hMod: THierarchyMod): Boolean;
begin
  case hMod of
    hmHierarquia:
      if apNeed <= apHave[hmHierarquia] then
        Beep;
    hmOperadores:
      if (apNeed + [haBrowseOnly]) <= apHave[hmOperadores] then
        Beep;
  end
end;

if IsAccessPermited([haBrowseOnly], MyHierarchy, hmOperadores) then Form2.Open;

OTHER TIPS

In general, symbol names such as record field names or parameter variable names are not included in the executable image by native code compilers such as Delphi because native machine instructions only care about offsets and addresses, not names. This is one of the reasons why Delphi produced exes can be hundreds of times smaller than their equivalent .NET or Java apps.

However, type metadata such as field names can be useful for "self-sufficient" object streaming. This type metadata is usually called Run Time Type Information (RTTI) by native code compilers.

You can try compiling your types above with {$M+} enabled around the type declaration. Be sure to set {$M-} immediately afterwards to avoid bloating your exe with unneeded type names.

You will then need to use Delphi RTTI methods to access the names of the fields of the record structure.

However, I don't think this will work with your sample code, since you are passing the record field as a parameter to another function. To get the name of the nth field of a record, you need the record type.

Your IsAccessPermitted() function only receives the value of the contents of the record field. It has no idea what field was used to pass the value in, or even if a record was used at all

As dthorpe (a former architect of Delphi!) says, that is impossible. But ISTM that your record could just as well be an array of THierarchyAccess.

I would do things this way:

type
  THierarchyMode = (mHierarchia, mOperadores, mEstatisticas, ... );

  THierarchy = array[THierarchyMode] of THierarchyAccess;

...

  procedure TAccess.IsAccessPermitted(const apNeed: THierarchyAccess; apHave: THierarchyMode; const Hierarchy: THierarchy);
  begin
    // access
    if apNeed = Hierarchy[apHave] then ...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top