Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting Pascal 'type' to C#

I'm trying to convert a Pascal type to C#. I have had a look around on Google, but I have not managed to find an answer, possibly because I am not googling correctly, so sorry if this is a duplicate.

I have these two Pascal types:

type
  TVector3i = array [0..2] of longint;

  Tcolface = packed record
    A, B, C: word;
    SurfaceA, SurfaceB: word;
  end;

I know

Tcolface = packed record
  A, B, C: word;
  SurfaceA, SurfaceB: word;
end;

converts to:

struct Tcolface {
  ushort A, B, C;
  ushort SurfaceA, SurfaceB;
}

but how does TVector3i = array [0..2] of longint; convert?

I'm trying to avoid using/writing a class, as when I convert the rest of the Pascal code it will be expecting the type as an array, and I am trying to avoid converting that to .x .y and .z.

I did consider doing float[] variablename = new float[3];, but as soon as I get List<float[]> variblename it gets a bit more complex.

The full code is:

TVector3i = array [0..2] of Longint;
TVector3f = array [0..2] of Single;
TVector3d = array [0..2] of Double;

TVector4i = array [0..3] of Longint;
TVector4f = array [0..3] of Single;
TVector4d = array [0..3] of Double;

TMatrix3i = array [0..2] of TVector3i;
TMatrix3f = array [0..2] of TVector3f;
TMatrix3d = array [0..2] of TVector3d;

TMatrix4i = array [0..3] of TVector4i;
TMatrix4f = array [0..3] of TVector4f;
TMatrix4d = array [0..3] of TVector4d;

Hence why I am trying to avoid classes :D

like image 578
D-Bennett Avatar asked Dec 08 '22 02:12

D-Bennett


1 Answers

how does TVector3i = array [0..2] of longint; convert?

There is no direct equivalent. TVector3i is an alias for a static array. C# does not have similar aliasing for arrays. The best you can do is declare a struct that contains an int[] array inside of it, and provides an [] indexer for closer syntax compatibility with the Pascal code:

struct TVector3i
{
    private int[] arr = new int[3];

    public int this[int i]
    {
        get
        {
            return arr[i];
        }
        set
        {
            arr[i] = value;
        }
    }
}

Update: based on your examples, try something like this:

struct TVector3<T>
{
    private T[] arr = new T[3];

    public T this[int i]
    {
        get
        {
            return arr[i];
        }
        set
        {
            arr[i] = value;
        }
    }
}

struct TVector4<T>
{
    private T[] arr = new T[4];

    public T this[int i]
    {
        get
        {
            return arr[i];
        }
        set
        {
            arr[i] = value;
        }
    }
}

using TVector3i = TVector3<int>;
using TVector3f = TVector3<float>;
using TVector3d = TVector3<double>;

using TVector4i = TVector4<int>;
using TVector4f = TVector4<float>;
using TVector4d = TVector4<double>;

using TMatrix3i = TVector3<TVector3i>;
using TMatrix3f = TVector3<TVector3f>;
using TMatrix3d = TVector3<TVector3d>;

using TMatrix4i = TVector4<TVector4i>;
using TMatrix4f = TVector4<TVector4f>;
using TMatrix4d = TVector4<TVector4d>;
like image 170
Remy Lebeau Avatar answered Dec 10 '22 15:12

Remy Lebeau