I want to convert an array of struct to array of Point3D. The code snippet is as follows :
我想将一个struct数组转换为Point3D数组。代码段如下:
class Mymesh { public MeshGeometry3D Mesh3D // Properties tanimlaniyor { get { return GetMesh3D(); } }
class Mymesh {public MeshGeometry3D Mesh3D // Properties tanimlaniyor {get {return GetMesh3D(); }}
public struct mystruct
{
public int m_i;
public int m_j;
public int m_k;
public mystruct(int i, int j, int k)
{
m_i = i;
m_j = j;
m_i = k;
}
}
private mystruct[] mypts =
{
new mystruct(20 , 7 , 7),
.
.
new mystruct(23 , 5 , 7)
};
public MeshGeometry3D GetMesh3D()
{
mesh.Positions.Add(mypts(1); *// The error is given at just this line.*
.
.
mesh.Positions.Add(mypts(50);
}
.
.
}
This code is producing the error message "Cannot convert from 'Mymesh.mystruct' to'System.Windows.Media.Media3D.Point3D'.
此代码生成错误消息“无法从'Mymesh.mystruct'转换为'System.Windows.Media.Media3D.Point3D'。
How can I overcome this error ?
我怎样才能克服这个错误?
Thanks in advance.
提前致谢。
Onder YILMAZ
4 个解决方案
#1
To be able to construct a Point3D you need to use one of its constructors.
From this documentation it seems that Point3D has a constructor that takes the 3 coordinates so you can change this:
为了能够构造Point3D,您需要使用其构造函数之一。从这个文档看来,Point3D似乎有一个构造函数,它采用3个坐标,所以你可以改变这个:
mesh.Positions.Add(mypts[1]);
to this:
mesh.Positions.Add(mypts[1].m_i, mypts[1].m_j, mypts[1].m_k);
You might also want to notice that you have quite a few syntax errors in this snippet of code. For example, indexing an array is done with []
and not ()
and when you're opening a parenthesis you should always close it.
您可能还需要注意,此代码段中存在大量语法错误。例如,使用[]而不是()对数组建立索引,当您打开括号时,应始终关闭它。
#2
There's multiple ways to do this:
有多种方法可以做到这一点:
- Don't use a custom struct, use Point3D to begin with
- Define an explicit or implicit cast for your struct, to cast/convert it to a Point3D
- Add a ToPoint3D method to your struct
- Simply call the .Add method with the i, j and k struct members
不要使用自定义结构,使用Point3D开头
为结构定义显式或隐式转换,以将其转换/转换为Point3D
在结构中添加ToPoint3D方法
只需使用i,j和k结构成员调用.Add方法即可
I'm going to show the third option here:
我将在这里展示第三个选项:
public Point3D ToPoint3D()
{
return new Point3D(i, j, k);
}
then you simply call that method when adding:
那么你在添加时只需调用该方法:
public MeshGeometry3D GetMesh3D()
{
mesh.Positions.Add(mypts[1].ToPoint3D());
Also note that you should probably use a loop here:
另请注意,您应该在此处使用循环:
foreach (mystruct p in mypts)
mesh.Positions.Add(p.ToPoint3D());
Also note the following:
另请注意以下事项:
- You should try to avoid having struct members named i, j and k, unless you're specifying internal loop registers. Try using X, Y and Z instead, as they are universally understood to be coordinate values
- Arrays in C# start at index 0, not 1
- You use [] for indexing array elements, not ()
- You're missing some ending parenthesis in your code
您应该尽量避免使用名为i,j和k的结构成员,除非您指定内部循环寄存器。尝试使用X,Y和Z代替,因为它们通常被理解为坐标值
C#中的数组从索引0开始,而不是1
使用[]索引数组元素,而不是()
您在代码中缺少一些结束括号
Why do I mention this? Because it's obvious to me that the code you posted is not the code you're trying to compile or execute. Never try to simplify code by rewriting it. Instead, make a short, but complete, compilable and testable program, and post the code to that. Let us worry about overload (of course, don't post 1000 lines, but shorten it down to 10-20).
为什么我提这个?因为对我来说很明显,你发布的代码不是你想要编译或执行的代码。永远不要试图通过重写代码来简化代码。相反,制作一个简短但完整,可编译和可测试的程序,并将代码发布到该程序。让我们担心过载(当然,不要发布1000行,但将其缩短到10-20)。
#3
Thanks to all of you and Sorry due to my few and erroneous snippet of code. The original code is very long. I could not write all of them. The original code has not any syntax error .
感谢大家和对不起,因为我的代码片段很少且错误。原始代码很长。我不能写所有这些。原始代码没有任何语法错误。
Thanks again.
Oner YILMAZ
#4
public Point3D MystructToPoint3D(mystruct point)
{
return new Point3D(point.m_i, point.m_j, point.m_k);
}
If you want to use your "mystruct" transparently as if it's a Point3D (e.g. you want to modify coordinates inside a method) you'd have to write an adapter:
如果你想透明地使用你的“mystruct”就像它是一个Point3D(例如你想要修改方法中的坐标),你必须编写一个适配器:
struct Point3DAdapter : Point3D
{
internal mystruct _point;
public Point3DAdapter(ref mystruct point)
{
this._point = point;
}
public override double X
{
get { return _point.m_i; }
set { _point.m_i = value; }
}
// same for Y and Z
}
I have to say that I didn't test this code and I'm not quite sure if I can pass a struct byref or override struct methods.. :)
我不得不说我没有测试这段代码,我不太确定我是否可以传递一个struct byref或覆盖struct方法.. :)
#1
To be able to construct a Point3D you need to use one of its constructors.
From this documentation it seems that Point3D has a constructor that takes the 3 coordinates so you can change this:
为了能够构造Point3D,您需要使用其构造函数之一。从这个文档看来,Point3D似乎有一个构造函数,它采用3个坐标,所以你可以改变这个:
mesh.Positions.Add(mypts[1]);
to this:
mesh.Positions.Add(mypts[1].m_i, mypts[1].m_j, mypts[1].m_k);
You might also want to notice that you have quite a few syntax errors in this snippet of code. For example, indexing an array is done with []
and not ()
and when you're opening a parenthesis you should always close it.
您可能还需要注意,此代码段中存在大量语法错误。例如,使用[]而不是()对数组建立索引,当您打开括号时,应始终关闭它。
#2
There's multiple ways to do this:
有多种方法可以做到这一点:
- Don't use a custom struct, use Point3D to begin with
- Define an explicit or implicit cast for your struct, to cast/convert it to a Point3D
- Add a ToPoint3D method to your struct
- Simply call the .Add method with the i, j and k struct members
不要使用自定义结构,使用Point3D开头
为结构定义显式或隐式转换,以将其转换/转换为Point3D
在结构中添加ToPoint3D方法
只需使用i,j和k结构成员调用.Add方法即可
I'm going to show the third option here:
我将在这里展示第三个选项:
public Point3D ToPoint3D()
{
return new Point3D(i, j, k);
}
then you simply call that method when adding:
那么你在添加时只需调用该方法:
public MeshGeometry3D GetMesh3D()
{
mesh.Positions.Add(mypts[1].ToPoint3D());
Also note that you should probably use a loop here:
另请注意,您应该在此处使用循环:
foreach (mystruct p in mypts)
mesh.Positions.Add(p.ToPoint3D());
Also note the following:
另请注意以下事项:
- You should try to avoid having struct members named i, j and k, unless you're specifying internal loop registers. Try using X, Y and Z instead, as they are universally understood to be coordinate values
- Arrays in C# start at index 0, not 1
- You use [] for indexing array elements, not ()
- You're missing some ending parenthesis in your code
您应该尽量避免使用名为i,j和k的结构成员,除非您指定内部循环寄存器。尝试使用X,Y和Z代替,因为它们通常被理解为坐标值
C#中的数组从索引0开始,而不是1
使用[]索引数组元素,而不是()
您在代码中缺少一些结束括号
Why do I mention this? Because it's obvious to me that the code you posted is not the code you're trying to compile or execute. Never try to simplify code by rewriting it. Instead, make a short, but complete, compilable and testable program, and post the code to that. Let us worry about overload (of course, don't post 1000 lines, but shorten it down to 10-20).
为什么我提这个?因为对我来说很明显,你发布的代码不是你想要编译或执行的代码。永远不要试图通过重写代码来简化代码。相反,制作一个简短但完整,可编译和可测试的程序,并将代码发布到该程序。让我们担心过载(当然,不要发布1000行,但将其缩短到10-20)。
#3
Thanks to all of you and Sorry due to my few and erroneous snippet of code. The original code is very long. I could not write all of them. The original code has not any syntax error .
感谢大家和对不起,因为我的代码片段很少且错误。原始代码很长。我不能写所有这些。原始代码没有任何语法错误。
Thanks again.
Oner YILMAZ
#4
public Point3D MystructToPoint3D(mystruct point)
{
return new Point3D(point.m_i, point.m_j, point.m_k);
}
If you want to use your "mystruct" transparently as if it's a Point3D (e.g. you want to modify coordinates inside a method) you'd have to write an adapter:
如果你想透明地使用你的“mystruct”就像它是一个Point3D(例如你想要修改方法中的坐标),你必须编写一个适配器:
struct Point3DAdapter : Point3D
{
internal mystruct _point;
public Point3DAdapter(ref mystruct point)
{
this._point = point;
}
public override double X
{
get { return _point.m_i; }
set { _point.m_i = value; }
}
// same for Y and Z
}
I have to say that I didn't test this code and I'm not quite sure if I can pass a struct byref or override struct methods.. :)
我不得不说我没有测试这段代码,我不太确定我是否可以传递一个struct byref或覆盖struct方法.. :)