views:

61

answers:

4

Hi All,

I want to convert an array of struct to array of Point3D. The code snippet is as follows :

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'.

How can I overcome this error ?

Thanks in advance.

Onder YILMAZ

+1  A: 

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:

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.

shoosh
A: 

There's multiple ways to do this:

  1. Don't use a custom struct, use Point3D to begin with
  2. Define an explicit or implicit cast for your struct, to cast/convert it to a Point3D
  3. Add a ToPoint3D method to your struct
  4. Simply call the .Add method with the i, j and k struct members

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

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).

Lasse V. Karlsen
A: 

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

A: 
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:

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.. :)

VVS