Grasshopper

algorithmic modeling for Rhino

VB Passing data of a customized type (class instance) to another component fails

Hi, 

I have defined a data type (class) and made an instance of it and passed the instance to another GH component. I copy the class definition to the other component as well but it fails to recognize it as the same type. It refers to a different type definition apparently.

Error:

error: [A]MassPix cannot be cast to [B]MassPix. Type A originates from '7ea7fec0-99c5-49a8-ae80-af752ac2be94, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'LoadFrom' at location 'C:\Users\pnourian\AppData\Local\Temp\7ea7fec0-99c5-49a8-ae80-af752ac2be94.dll'. Type B originates from 'fd0b2126-e10f-49de-9fc9-5504405d4135, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'LoadFrom' at location 'C:\Users\pnourian\AppData\Local\Temp\fd0b2126-e10f-49de-9fc9-5504405d4135.dll'. (line: 82)

This is the case: 

in component A: 

Private Sub RunScript(ByVal x As Object, ByVal y As Object, ByRef A As Object)
Dim kjh As New MassPix(2.1, 2.3, 4, 5)
A = kjh
End Sub

'<Custom additional code>
Public Class MassPix
Private x As Double
Private y As Double
Private S As Integer
Private K As Integer
Sub New(xu As Double, yv As Double, SZ As Integer, KL As Integer)
x = Xu
y = yv
s = Sz
k = Kl
End Sub
End Class
'</Custom additional code>
End Class

and in component B:

Private Sub RunScript(ByVal x As Object, ByVal y As Object, ByRef A As Object)
Dim ABC As MassPix = CType(x, MassPix)


End Sub

'<Custom additional code>
Public Class MassPix
Private x As Double
Private y As Double
Private S As Integer
Private K As Integer
Sub New(xu As Double, yv As Double, SZ As Integer, KL As Integer)
x = Xu
y = yv
s = Sz
k = Kl
End Sub
End Class
'</Custom additional code>
End Class

the file is attached

ANY HELP IS VERY MUCH APPRECIATED! 

Views: 680

Attachments:

Replies to This Discussion

Two different classes that have the same code are still two different classes. You'll have to somehow share a single class between the two components. This is cumbersome to do using the scripting components, as it requires you to create a dll with the shared type and reference that dll with all components that need it.

Another approach is to have the same class definition in both components, but share the data using an available format. For example you could convert your class into a string or a dictionary or array, output that data and convert it back into a custom class again at the other end. Still pretty cumbersome.

Final option is to switch to Visual Studio and make a proper GHA plugin, then all your code is compiled from the same repository and sharing classes is trivial.

Hi David, 

Thanks for your clear response. I have now a GHA built, without errors in Visual Studio, but it gives me the following error in GH: 1. Solution exception:Unable to cast object of type 'Grasshopper.Kernel.Types.GH_ObjectWrapper' to type 'Raster3D.Raster3D'.

My type (class) is called Raster3D

I have tried using a GH_ObjectWrapper and casting its value to a variable of my type and also getting it in as an object. It fails to cast both ways. I have put the class definition as a separate class in my GHA definition and it works fine in VS, as both components have access to it. It just fails in GH. Maybe I am missing something about GH_ObjectWrapper?

I just succeeded in doing it by producing a dll added as a reference for two scripted component; the problem is the same thing fails when I make a GHA and bring the dll with it to the grasshopper components folder. 

If you only want to pass the data (some members around), you can use anonymous types in .net, see http://msdn.microsoft.com/en-us/library/bb384767.aspx

In your case:

A = New With {Key.x = 2.1, .y = 2.3, .s = 4, .k = 5}

Here with an example

Attachments:

Thank you very much Arend. I finally solved it by making a dll. But this is indeed a nice solution! :)

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service