Java enum method for Delphi

public enum HTTPHeaderKey { CACHE_CONTROL("Cache-Control"), CONNECTION("Connection"), TRANSFER_ENCODING("Transfer-Encoding"), HOST("Host"), USER_AGENT("User-Agent"), CONTENT_LENGTH("Content-Length"), CONTENT_TYPE("Content-Type"); private final String str; private HTTPHeaderKey(final String _str) { str = _str; } /** Over ridden toString returns the HTTP/1.1 compatible header */ public String toString() { return str; } }; 

I am trying to convert this listing to Delphi. I know how to define enumeration variables, but I have no idea how I can insert a method into an enumeration?

Or can someone suggest another way to do this?

+7
java delphi
source share
2 answers

You can get the part there using the write helper, which is available for value types from XE3 onwards. For example:

 {$APPTYPE CONSOLE} uses System.SysUtils, System.TypInfo; type TMyEnum = (enumValue1, enumValue2); TMyEnumHelper = record helper for TMyEnum public function ToString: string; end; function TMyEnumHelper.ToString: string; begin Result := GetEnumName(TypeInfo(TMyEnum), ord(Self)); end; begin Writeln(enumValue1.ToString); Writeln(enumValue2.ToString); end. 

This program displays the following:

  enumValue1
 enumValue2

Of course you can do it like this:

 {$APPTYPE CONSOLE} uses System.SysUtils, System.TypInfo; type TMyEnum = (enumValue1, enumValue2); TMyEnumHelper = record helper for TMyEnum private const EnumNames: array [TMyEnum] of string = ('Friendly name 1', 'Friendly name 2'); public function ToString: string; end; function TMyEnumHelper.ToString: string; begin Result := EnumNames[Self]; end; begin Writeln(enumValue1.ToString); Writeln(enumValue2.ToString); end. 

The output is here:

  Friendly name 1
 Friendly name 2

This, apparently, will allow you to solve the fact that the listed Delphi types do not support the text naming available in Java.

Another method, the HTTPHeaderKey constructor, HTTPHeaderKey not be supported by the listed type. The reason is that this requires a state, and the only state for the Delphi enumerated type is the value of the enumerated type itself. You cannot transplant an additional instance variable, as is done in Java code.

All things considered, I don’t think that trying on a literal translation with an enumerated type will work. I suggest you translate using either a record or a class and create equivalent functionality using the available Delphi language constructors.

+8
source share

Based on Davids' answer, this modified version should be able to print HTTP header names:

 type THTTPHeaderKey = (hkCACHE_CONTROL, hkCONNECTION, hkTRANSFER_ENCODING); TTHTTPHeaderKeyHelper = record helper for THTTPHeaderKey private const EnumNames: array [THTTPHeaderKey] of string = ('Cache-Control', 'Connection', 'Transfer-Encoding'); public function ToString: string; end; // ToString returns the HTTP/1.1 compatible header function TTHTTPHeaderKeyHelper.ToString: string; begin Result := EnumNames[Self]; end; ... begin Writeln(hkCACHE_CONTROL.ToString); Writeln(hkTRANSFER_ENCODING.ToString); end. 

should output

 Cache-Control Transfer-Encoding 
+1
source share

All Articles