无法隐式转换类型...与属性

时间:2011-07-19 13:55:30

标签: c# properties type-conversion implicit

我一直在尝试设置一个包含三个Vector3和一个整数的三角形类。

这是类构造函数:(不确定这是正确的术语,我是业余爱好者)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Icosahedron_Test
{
    class TriXYZ
    {
        Vector3 vertex1;
        Vector3 vertex2;
        Vector3 vertex3;
        int depth;
        float material1; // float for first material value amount (in %)  deals with blending
        float material2; // float for second material value amount (in %)  deals with blending

        Vector3 vValue;  // place holder for vertex properties "set"
        int dValue;  // place holder for depth properties "set"

    public TriXYZ(Vector3 pos1, Vector3 pos2, Vector3 pos3, int tDepth)
    {
        vertex1 = pos1;
        vertex2 = pos2;
        vertex3 = pos3;
        depth = tDepth;
    }

这就是我设置属性的方式:

public Vector3 GetVertex1
{
    get { return vertex1; }
    set { vertex1 = vValue; }
}

这就是我从另一个班级调用该属性的方式:

Vector3 cVertex1;
TriXYZ cTriangle = triangleList[listPos];   // triangleList is a TriXYZ[] array
.
.
.

cVertex1 = cTriangle.GetVertex1;

我得到的错误是:

Cannot implicitly convert type 'Microsoft.Xna.Framework.Vector3' to 'Icosahedron_Test.TriXYZ'

我理解这个错误通常意味着什么,本质上我正在尝试将字符串分配给整数或类似的东西。我不明白的是为什么我在这里看到错误。变量cVertex1是Vector3变量,vertex1的返回值也是Vector3变量。

任何人都可以看到我遇到这个问题的原因吗?

这是我迄今为止开发的TriXyZ类和Icosahdron类的完整代码:

TriXYZ:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Icosahedron_Test
{
class TriXYZ
{
    Vector3 vertex1;
    Vector3 vertex2;
    Vector3 vertex3;
    int depth;
    float material1; // float for first material value amount (in %)  deals with blending
    float material2; // float for second material value amount (in %)  deals with blending

    Vector3 vValue;  // place holder for vertex properties "set"
    int dValue;  // place holder for depth properties "set"

    public TriXYZ(Vector3 pos1, Vector3 pos2, Vector3 pos3, int tDepth)
    {
        vertex1 = pos1;
        vertex2 = pos2;
        vertex3 = pos3;
        depth = tDepth;
    }

    public TriXYZ(Vector3 pos1, Vector3 pos2, Vector3 pos3, int tDepth, float tMaterial1, float tMaterial2)
    {
        vertex1 = pos1;
        vertex2 = pos2;
        vertex3 = pos3;
        depth = tDepth;
        material1 = tMaterial1;
        material2 = tMaterial2;
    }

    // public access to triangle data, read-write

    public Vector3 GetVertex1
    {
        get { return vertex1; }
        set { vertex1 = vValue; }
    }
    public Vector3 GetVertex2
    {
        get { return vertex2; }
        set { vertex2 = vValue; }
    }
    public Vector3 GetVertex3
    {
        get { return vertex3; }
        set { vertex3 = vValue; }
    }
    public int GetDepth
    {
        get { return depth; }
        set { depth = dValue; }
    }



    public Vector3 Midpoint(Vector3 pos1, Vector3 pos2, int tDepth)
    {
        Vector3 midpoint;  // returned midpoint between the two inputted vectors

        //PLACEHOLDER

        return midpoint;
    }

}
}

和ICOSAHDRON:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Icosahedron_Test
{
class Icosahedron
{
    int radius;  // radius of the planet
    int refinement;  // number of times to refine the traingles
    int faces = 20;
    Vector3[] basePositions; // Vertex points for three defining rectangles
    TriXYZ[] vertices;  // Vertex points for triangles which define the spherical surface

    public Icosahedron(int tRadius, int tRefinement, TriXYZ[] tVertices)
    {
        radius = tRadius;
        refinement = tRefinement;
        vertices = tVertices;
    }

    protected void Initialize()
    {
        double t = radius*((1+Math.Sqrt(5))/2);

        Vector3[] basePositions = 
        {
            //First Rectangle
            Vector3.Normalize(new Vector3(-radius, (float)t, 0)),
            Vector3.Normalize(new Vector3(radius, (float)t, 0)),
            Vector3.Normalize(new Vector3(-radius, (float)-t, 0)),
            Vector3.Normalize(new Vector3(radius, (float)-t, 0)),

            //Seconds Rectangle
            Vector3.Normalize(new Vector3(0, -radius, (float)t)),
            Vector3.Normalize(new Vector3(0, radius, (float)t)),
            Vector3.Normalize(new Vector3(0, -radius, (float)-t)),
            Vector3.Normalize(new Vector3(0, radius, (float)-t)),

            //Third Rectangle
            Vector3.Normalize(new Vector3((float)t, 0, -radius)),
            Vector3.Normalize(new Vector3((float)t, 0, radius)),
            Vector3.Normalize(new Vector3((float)-t, 0, -radius)),
            Vector3.Normalize(new Vector3((float)-t, 0, radius))
        };

        TriXYZ[] vertices =
        {
            new TriXYZ(basePositions[0], basePositions[11], basePositions[5], 1),
            new TriXYZ(basePositions[0], basePositions[5], basePositions[1], 1),
            new TriXYZ(basePositions[0], basePositions[1], basePositions[7], 1),
            new TriXYZ(basePositions[0], basePositions[7], basePositions[10], 1),
            new TriXYZ(basePositions[0], basePositions[10], basePositions[11], 1),

            new TriXYZ(basePositions[1], basePositions[5], basePositions[9], 1),
            new TriXYZ(basePositions[5], basePositions[11], basePositions[4], 1),
            new TriXYZ(basePositions[11], basePositions[10], basePositions[2], 1),
            new TriXYZ(basePositions[10], basePositions[7], basePositions[6], 1),
            new TriXYZ(basePositions[7], basePositions[1], basePositions[8], 1),

            new TriXYZ(basePositions[3], basePositions[9], basePositions[4], 1),
            new TriXYZ(basePositions[3], basePositions[4], basePositions[2], 1),
            new TriXYZ(basePositions[3], basePositions[2], basePositions[6], 1),
            new TriXYZ(basePositions[3], basePositions[6], basePositions[8], 1),
            new TriXYZ(basePositions[3], basePositions[8], basePositions[9], 1),

            new TriXYZ(basePositions[4], basePositions[9], basePositions[5], 1),
            new TriXYZ(basePositions[2], basePositions[4], basePositions[11], 1),
            new TriXYZ(basePositions[6], basePositions[2], basePositions[10], 1),
            new TriXYZ(basePositions[8], basePositions[6], basePositions[7], 1),
            new TriXYZ(basePositions[9], basePositions[8], basePositions[1], 1),

        };
    }

    private TriXYZ[] Refinement(TriXYZ[] rVertices, int rRefinement)
    {
        TriXYZ[] tVertices;  // Temp list of triangles

        int cDepth = 1; // current depth integer

        TriXYZ vertex1; // position of first vertex of base triangle
        TriXYZ vertex2; // position of second vertex of base triangle
        TriXYZ vertex3; // position of third vertex of base triangle
        int tDepth; // depth of the current triangle

        TriXYZ mid1; // position of first midpoint
        TriXYZ mid2; // position of second midpoint
        TriXYZ mid3; // position of third midpoint

        int listPos = 0; // base list position integer
        int nListPos = 0; // new list position integer

        int cRefine = 1; // current refinement iteration

        while(cRefine < rRefinement)  // loop until the icosphere has been refined the inputted number of times
        {
            tVertices = null;

            foreach (TriXYZ i in rVertices)
            {
                TriXYZ cTriangle = tVertices[listPos];

                vertex1 = cTriangle.GetVertex1;
                vertex2 = cTriangle.GetVertex2;
                vertex3 = cTriangle.GetVertex3;
                tDepth = tVertices[listPos].GetDepth;

                mid1 = TriXYZ.Midpoint(vertex1, vertex2, tDepth);
                mid2 = TriXYZ.Midpoint(vertex1, vertex2, tDepth);
                mid3 = TriXYZ.Midpoint(vertex1, vertex2, tDepth);
            }
        }

        return rVertices;
    }

}
}

2 个答案:

答案 0 :(得分:2)

你像这样声明GetVertex1:

public Vector3 GetVertex1

这很好,这就是你想要的。但是,在Refinement方法的isocahedron类中,您有以下片段:

    TriXYZ vertex1; // position of first vertex of base triangle
    TriXYZ vertex2; // position of second vertex of base triangle
    TriXYZ vertex3; // position of third vertex of base triangle
    ...
    ...
    vertex1 = cTriangle.GetVertex1;
    vertex2 = cTriangle.GetVertex2;
    vertex3 = cTriangle.GetVertex3;

因此,当它被声明为TriXYZ时,尝试将vertex1设置为Vector3。

确定应该是什么类型的东西,你应该排序。 :)

答案 1 :(得分:0)

  TriXYZ vertex1; // position of first vertex of base triangle   
  TriXYZ vertex2; // position of second vertex of base triangle   
  TriXYZ vertex3; // position of third vertex of base triangle 

这些行覆盖'Microsoft.Xna.Framework.Vector3'的Vertex3到TriXYZ,因此您将更改TriXYZ的对象名称