GrasshopperDocument.Objects in VS

unhandled

#1

I have a code that works perfectly fine in the C# component in grasshopper, but when I copy it over to VS, it gives me error CS0103
The name ‘GrasshopperDocument’ does not exist in the current context

I Understand that things are handled slightly differently between C# component and VS.
So how would I change these following code so it’d work in VS?

GrasshopperDocument.Objects;

(David Rutten) #2

Visual Basic gives you a compiler error with a CS code?


(David Rutten) #3

I’d say lose the semi-colon. But without a way to replicate the problem I’m driving blind here.


#4

Yes, when i try to compile, i get the error

basically I’m trying to get all the objects currently in the canvas.
GrasshopperDocument.Objects works in C# script, but not in VS.


(David Rutten) #5

This is Visual Studio, not a VB component, which is what I thought you were doing.
You’re clearly working in a C# project, so no VB code allowed. Visual Studio does not allow you to mix and match programming languages within a single project.

Also the code you write inside a script component cannot be siphoned over into Visual Studio. GrasshopperDocument does not exist within classes that derive from GH_Component. You have to use (in C#):

GH_Document doc = OnPingDocument();
if (doc == null)
  return; // document may be null, you need to decide how to continue when that happens.

(David Rutten) #6

Ok, so VB was a typo, VS was meant.
Now I understand the problem. The OnPingDocument method is what you need when developing within Visual Studio. Something along these lines:

private IGH_DocumentObject[] AllCanvasObjects()
{
  var doc = OnPingDocument();
  if (doc == null)
    return new IGH_DocumentObject[0];
  return doc.Objects.ToArray();
}