2

I 've problem with array in C#. I'm quite new in C# I'm used to do programs in java. I'm trying to transfer this code from C++ to C#. This is code in C++

typedef struct point_3d {           // Structure for a 3-dimensional point (NEW)
    double x, y, z;
} POINT_3D;

typedef struct bpatch {             // Structure for a 3rd degree bezier patch (NEW)
    POINT_3D    anchors[4][4];          // 4x4 grid of anchor points
    GLuint      dlBPatch;               // Display List for Bezier Patch
    GLuint      texture;                // Texture for the patch
} BEZIER_PATCH;

I have struct Vector3 in C# which is float x,y,z (I don't need double ...) Now I'm trying to make structure bpatch and I have problems with the declaration of array

[StructLayout(LayoutKind.Sequential)]
struct BPatch
{
  Vector3[][] anchors = new Vector3[4][4]; //there is the problem
  uint dblPatch; // I'll probably have to change this two lines but it doesn't matter now
  uint texture; 

}

what do I do wrong?? I need aray 4x4 in structure, its type should be structure Vector3 which is declared as float x, float y, float z. Thanks

2
  • What makes you think that the C++ POINT_3D translates to the .NET Vector3? Commented Dec 31, 2011 at 15:58
  • Why not? I work according this tutorial from NEHE nehe.gamedev.net/tutorial/bezier_patches__fullscreen_fix/18003 and point3D has coordinates x,y,z and Vector3 I mentioned is ready to use in library which made gave to us our teacher at university, also with operations add and others.. so I used it. Commented Dec 31, 2011 at 16:45

2 Answers 2

3

You can use:

Vector3[,] anchors = new Vector3[4,4];
Sign up to request clarification or add additional context in comments.

1 Comment

Exactly! that's a matrix in c#
1

In C#, Vector3[][] is not a matrix but an array of arrays. So, you will need to do this:

anchors = new Vector3[4][];
for(var i=0;i<anchors.Length;i++)
    anchors[i] = new Vector3[4];

Here's some documentation from msdn http://msdn.microsoft.com/en-us/library/2s05feca.aspx

Another way, inline:

Vector3[][] anchors = new Vector3[][]{new Vector3[4],new Vector3[4],new Vector3[4],new Vector3[4]};

Hope it helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.