algorithmic modeling for Rhino
Hello,
I've stumbled onto a not so nice behavioral discrepancy between working with c# in VS and the native GH c# component. Identical code that works in one environment (native gh c# component) behaves quite differently when taken into VS and compiled.
I've isolated the errors around an exception being thrown from a single for loop. The for loop is cycling through a jagged array. It's quite puzzling.
Some help would be appreciated. Does anyone have thoughts about what should I look out for in debugging this?
Tags:
Steve,
I hear you - and I apologize for not doing so more quickly. I was in the process of cleaning up the code to provide an example and found the heart of the problem - here it is. I’ve isolated it from the jagged array and into its simplest form.
There seems to be a problem with calling the IsClosed method while in a for loop.
// This works in GH c# but not in VS
bool[] someCurveStatus = new double[someCurve.Length];
for(int i = 0; i < someCurve.Length; i++)
{
someCurveStatus = Convert.ToInt32(someCurve[i].IsClosed);
}
// This works in VS and GH c#
bool[] someCurveStatus = new double[someCurve.Length];
for(int i = 0; i < someCurve.Length; i++)
{
someCurveStatus = 1;
}
Steve,
Actually the problem is even weirder than I had thought because it seems that whenever the loop has a statement placed within it that demands a method it fails.
For example if you replace the IsClosed with another method like GetLength() it also fails:
// This does not work in VS but does work in GH c# component
double[] anotherTest = new double[someCurve.Length];
for(int i = 0; i < someCurve.Length; i++)
{
anotherTest[i] = someCurve[i].GetLength();
}
Several issues, you should check. I'm not sure if this is just a problem of sizing down the code... btw.: it might help to know what exception is being thrown.
I assume, someCurve is an array of Curves. If so, that doesn't mean, every curve is valid. If you can't be sure, check every Curve for null/nothing before you try to access any of it's properties or methods.
bool[] someCurveStatus = new double[someCurve.Length];
is not valid code and should not compile in GH C# or VB.
In general, the C# component does some error handling for you that in a VS component, you should take care of yourself. So both would behave slightly different with respect to unhandled exceptions.
Hannes,
Thank you for your help - it was very useful. You were right on the ball with the curve being valid / invalid and that it would not process. It seems that this is a significant difference between the two environments (ie. c# component and the Visual Studio environment). I've actually found that arrays will often process in the c# component when containing null cells but it seems that components compiled in visual studio (VS), when run on the canvas, produce runtime errors when nulls are found.
So many thanks for the thought and suggestion.
(Oh and the instantiation does work in VS. Most texts I've read show it as such too - maybe you can pass on a reference for clarification?)
Welcome to
Grasshopper
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
© 2024 Created by Scott Davidson. Powered by