Cannot implicitly convert type... with property
I've been trying to set up a triangle class that contains three Vector3's and an integer.
Here's the class constructor: (not sure that's the right term, I'm an amateur)
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;
}
This is how I'm setting up the property:
public Vector3 GetVertex1
{
get { return vertex1; }
set { vertex1 = vValue; }
}
And this is how I'm calling the property from another class:
Vector3 cVertex1;
TriXYZ cTriangle = triangleList[listPos]; // triangleList is a TriXYZ[] array
.
.
.
cVertex1 = cTriangle.GetVertex1;
The error I am getting is:
Cannot implicitly convert type 'Microsoft.Xna.Framework.Vector3' to 'Icosahedron_Test.TriXYZ'
I understand what this error usually means, essentially that I am trying to assign a string to an integer or something like that. What I don't understand is why I am seeing the error here. the variable cVertex1 is a Vector3 variable, and the return value of vertex1 is also a Vector3 variable.
Can anyone see why I am running into this problem?
Here's teh full code for the TriXyZ class and the Icosahdron class that I've developed so far:
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;
}
}
}
and 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;
}
}
}
You declare GetVertex1 like so:
public Vector3 GetVertex1
This is fine assumign that's what you want. However in the isocahedron class in the Refinement method you have the following snippets:
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;
So this is then trying to set vertex1 to be a Vector3 when it is declared as a TriXYZ.
Work out what types things should be and you should be sorted. :)
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
these lines overriding Vertex3 of 'Microsoft.Xna.Framework.Vector3' to TriXYZ so you would change the object name of TriXYZ
精彩评论