Grasshopper

algorithmic modeling for Rhino

I'm working on a VB scriptable component that generates outputs from multiple SUBs.  Yet when I try to execute a print("data") command from any SUB other than the 'RunScript' SUB (all the others are under <Custom additional code>) I get:

 

Error:  Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class

 

How can I output data thats generated in SUBs other than 'RunScript'?

Views: 259

Replies to This Discussion

Hi Eric,

 

you're going to have to post some code for this. But at an initial guess it's probably because you're trying to access a variable inside a method body from within another method body.

 

Public Sub A()

  Dim num As Integer = 6

  B(num)

  Print(num)

  Print(value) <------- problem!

End Sub

 

Public Sub B(ByVal data As Integer)

  Dim value As Double = (data * data) / 3.0

  ...

End Sub

 

From inside Sub A you cannot 'see' any variables declared inside Sub B. In fact it would be impossible to do so. Consider this; If you're currently inside A, then it means that B is not running (unless B is on another thread, but let's not get into that can'o'worms). If B is not running then it has no data assigned to any of its variables, because .NET automatically cleans up after itself.

Also, what's to stop you from having a variable called data in more than one Sub? Then how would VB know which data you meant?

 

If you want to print values of variables, then you either have to print them from within the same Sub:

 

Public Sub B(ByVal data As Integer)

  Dim value As Double = (data * data) / 3.0

  Print(value)

  ...

End Sub


Or you have to share data between Subs:

 

Public Sub B(ByRef data As Integer)

  data = (data * data) / 3.0

  ...

End Sub

 

Since data is now passed by reference (ByRef), both Sub A and Sub B have access to the very same variable.

 

--

David Rutten

david@mcneel.com

Poprad, Slovakia

I removed 'Shared' from 'Public Shared Sub...' and I was able to print.

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