Home > Blockchain >  Convert C# to delphi
Convert C# to delphi

Time:01-08

please i need help to port this c# code into delphi , I tried but the issue with delphi is that TArray is a genric type look at my delphi code procedure WriteVector(array: TArray);

internal static void WriteValue(object value, Type valueType)
{
    var type = value.GetType();
    switch (Type.GetTypeCode(type))
    {   
        // the following are already implemented into delphi
        case TypeCode.Int32: Write((int)value); break;
        case TypeCode.Int64: Write((long)value); break;
        case TypeCode.UInt32:Write((uint)value); break;
        case TypeCode.UInt64:Write((ulong)value); break;
        case TypeCode.Double:Write((double)value); break;
        case TypeCode.Object:
            if (type.IsArray)   
        // the issue is here            
                WriteVector((Array)value);
            break;
        default:
            break;
    }
}
            
            
internal static void WriteVector(Array array)
{
    if (array == null) { return; }
    int count = array.Length;
    // save count value
    var elementType = array.GetType().GetElementType();
    for (int i = 0; i < count; i  )
        WriteValue(array.GetValue(i), elementType);
}   
// My delphi code  
procedure WriteValue(Value: TValue; valueType: PTypeInfo);
begin
    case Value.Kind of
    tkArray:
      WriteVector(Value.AsType<TArray>);
    end;
end;
    
    
procedure WriteVector(array: TArray);
var
    count: Integer;
    i: Integer;
begin
      if array = nil then
      begin
          Exit;
      end;
      // count := Length(array);
      // save count value
      // how the rest can be correctly written ?
end;

CodePudding user response:

UPD. Sorry, I haven't updated the page before posting. I'll leave it unchaged.

Well... I see some kind of cyclic/recursive stuff, where WriteValue calls WriteVector, WriteVector calls WriteValue and that's all. valueType param is not used, non-array values are ignored. Without additional details it's hard to advise good solution. As an option you may use Variant type.

procedure WriteValue(vValue: Variant);
begin
  if ( VarIsArray(vValue) ) then begin
    WriteVector(vValue);
  end

  else if ( VarIsStr(vValue) ) then
  begin
  end

  else if ( VarIsType(vValue, [varShortInt, varDate]) ) then
  begin
  end

  else begin
    case VarType(vValue) of
      varByte: begin
      end;

      varDouble: begin
      end;
    else
      // default
    end;
  end;
end;

procedure WriteVector(vArray: Variant);
var
  i: Integer;

begin
  if ( not VarIsArray(vArray) ) then Exit;

  if ( VarArrayDimCount(vArray) <> 1 ) then Exit;

  for i := VarArrayLowBound(vArray, 1) to VarArrayHighBound(vArray, 1) do
    WriteValue( VarArrayGet(vArray, [i]) );
end;

procedure Test();
var
  v: variant;

begin
  v := VarArrayOf( ['0', Now(), Byte(1), Double(2), VarArrayOf([3, 4, 5])] );
  WriteValue(v);
  WriteVector(Null);
end;
  • Related