【问题标题】:array in struct C#结构 C# 中的数组
【发布时间】:2011-12-31 15:56:49
【问题描述】:

我对 C# 中的数组有疑问。我是 C# 的新手,我习惯用 java 做程序。 我正在尝试将此代码从 C++ 传输到 C#。 这是 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;

我在 C# 中有 struct Vector3,它是 float x,y,z(我不需要 double ...) 现在我正在尝试制作结构 bpatch,但我在声明数组时遇到了问题

[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; 

}

我做错了什么??我需要一个4x4的结构,它的类型应该是结构Vector3,它被声明为float x,float y,float z。 谢谢

【问题讨论】:

  • 是什么让您认为 C++ POINT_3D 转换为 .NET Vector3
  • 为什么不呢?我根据 NEHE nehe.gamedev.net/tutorial/bezier_patches__fullscreen_fix/18003 的本教程工作,point3D 具有坐标 x、y、z 和我提到的 Vector3 已准备好在图书馆中使用,这使我们在大学时给了我们老师,还有操作 add 和其他......所以我用它。

标签: .net visual-studio c#-4.0


【解决方案1】:

你可以使用:

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

【讨论】:

  • 没错!这是 c# 中的矩阵
【解决方案2】:

在 C# 中,Vector3[][] 不是矩阵,而是数组数组。所以,你需要这样做:

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

这是来自 msdn http://msdn.microsoft.com/en-us/library/2s05feca.aspx 的一些文档

另一种方式,内联:

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

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-07
    • 2013-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多