Grasshopper

algorithmic modeling for Rhino

Am I? I don't know...

I am switching from VB to C# and everything makes sense except for type "conversions"

the code is simply 

MeshFace mf = x;

x is a system.Object because you cant specify meshface... but I don't always want to have to set my input hint type. Can someone help me with errors like the following?

0. Error: Cannot implicitly convert type 'object' to 'Rhino.Geometry.MeshFace'. An explicit conversion exists (are you missing a cast?) (line 90)

Thanks

Views: 2356

Replies to This Discussion

To perform the cast, you can do something like the following:

MeshFace mf = (MeshFace) x;

This will work fine provided you can be sure that your inputs to x are always MeshFaces. If you feed it other kinds of geometry it will throw an error.

If you want to check first, use:

if (x is MeshFace)

{

  mf = x as MeshFace;

}

--

David Rutten

david@mcneel.com

Poprad, Slovakia

Perfect. Thank you both.

I've the same problem.

I tried with this:

private void RunScript(object x, ref object mf)
{

if (x is MeshFace)
{
mf = x as MeshFace;
}

}

but it doesn't work

the error is:

Error (CS0077): The as operator must be used with a reference type or nullable type ('Rhino.Geometry.MeshFace' is a non-nullable value type) (line 74)

I started to use C # a couple of weeks ago, so I miss the basics.

Yup, the 'as' operator only works if the result (in this case MeshFace) can be null. It seems that my original code never would have worked.

You cannot use 'as' to cast something to a valuetype. You'll have to use old-fashioned casting:

if (x is MeshFace)

  mf = (MeshFace)x;

--

David Rutten

david@mcneel.com

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service