【问题标题】:How to transfer a variable from a struct to a method?如何将变量从结构转移到方法?
【发布时间】:2009-06-08 23:20:43
【问题描述】:

假设我们有一个名为“points”的数组,其结构中的类型为 Point3D,并且想要 在方法中使用这些点。如何从结构转移到方法? 以下来自代码sn-p。

问候

杰米尔

public MeshGeometry3D GetMesh3D()

{

**(just here, we want to use the 3D points coming from the GetVortices method.)**

}

public Point3D[] GetVortices()

{

      points[0] = new Point3D(1,1,1);

.

      points[100] = new Point3D(3,1,5);

}

.

.

【问题讨论】:

    标签: arrays struct transfer


    【解决方案1】:

    GetVortices() 中使用return 语句,并从GetMesh3D() 调用该方法。

    public MeshGeometry3D GetMesh3D()
    {
        Point3D[] points = GetVortices();
    }
    public Point3D[] GetVortices()
    {
          // Declare points as an array of Point3D
          points[0] = new Point3D(1,1,1);
          // ...
          points[100] = new Point3D(3,1,5);
          return points;
    }
    

    【讨论】:

      【解决方案2】:

      您的问题的背景尚不清楚。这些方法属于哪些类(如果有的话),顺便说一下,是什么语言?谁在调用 GetMesh3D 方法?

      不过,简而言之,为什么不直接传递呢?

      GetMesh3D( points );
      

      当然,这需要重写方法签名,我假设你可以这样做。

      【讨论】:

        【解决方案3】:

        我假设 GetVorticies() 在最后返回点数组(返回点;)那么您在 GetMesh3D 中所要做的就是......

        public MeshGeometry3D GetMesh3D()
        {
          Point3D[] points = GetVorticies();
          Point3D   somePoint = points[0];
        
          // make meshgeometry3d out of points and return;
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-05-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多