Return multiple values from a method : tuple types and tuple literals with Grasshopper C#

Hello :slight_smile:

Just a little question for developpers into Grasshopper about syntax:
Is it possible to use c# tuple types and tuple literals into a grasshopper c# component ?

Wich is the version of C# used by grasshopper ? I think it is possible since C#7
I’m using Rhino 5, and Grasshopper Latest Build : 0.9.0076 (Version August 2014)
In Rhino 6 is Grasshopper using the latest C# Version (C#7.1) ?

And I’m afraid that it is too old since C#7 was released in March 2017 (C# Versions dates)

What is the best way for me and my old version to return multiple values types from a Method ?
Like in that exemple ? (wich only works for C#7).

(string, string, string) MyCoolFunction() // tuple return type
{
//…
return (firstValue, secondValue, thirdValue);
}

Wich can be used like this :

var values = MyCoolFunction();
var firstValue = values.Item1;
var secondValue = values.Item2;
var thirdValue = values.Item3;

Have a nice evening/day !
Adrian

Just found a way to d that but it behave really strange when I’m trying to get the second item…
So Item1 is perfect and then when I’m getting Item2 all meshes are flying around ^^

So maybe you’ll have another way to get multiple types that will work better.

private void RunScript(object x, object y, ref object A)
{
MyFunctionToInvoke();
A = helloString.ToString() + helloInt.ToString();

}
// Custom additional code

string helloString = “”;
int helloInt = new int();

private void MyFunctionToInvoke()
{
helloString = Getinfo().Item1;
helloInt = Getinfo().Item2;
}

public Tuple<string, int> Getinfo()
{
return Tuple.Create(“Adrian”, 28);
}
// </Custom additional code>
}

File : GetMultipleType_Method.gh (3.3 KB)

Ooo Ok I think I got the issue…
It seems somehow to the same exact value to the Transform when I add the second Item o_O

So I’m “Very” lonely on that topic but at least i’m going to go ahead :slight_smile:

In that exemple the problem is obvious :
File: GetMultipleType_Method_Test1.gh (45.3 KB)

//Adrian

And there we are, just found out how to manage that strange flying mesh.
Basically I was just entering to times the same mesh into the function wich is odd indeed.
So i changed the mesh for the second function :

oMesh = Orient(Pt0, Pt1, Pt2, Anchor, teaPotMesh).Item1;
oPlane = Orient(Pt0, Pt1, Pt2, Anchor, newMesh).Item2;

…The question ended to be a tutorial XD
But anywas, nice for the people that will type “Return multpiles values from a method”
Here there is the explanation + exemples :rofl:

But I still have a…
Question : It is kind of strange that we have to re-process pt0,pt1,pt2 and anchor into the second function to get something we could already have from the first function…
Is there a way to Process things just one time and get the two items form this “just on time Process” ?

Also… the solution of putting an “empty fake mesh” is not as elegant as it should be I guess :thinking:

The file : GetMultipleType_Method_Test2_working.gh (45.8 KB)

//Adrian

 public Tuple<string, string, string> MyCoolFunction()
 {
     return new Tuple<string,string,string>("firstValue", "secondValue", "thirdValue");
 }

Thanks Dani.
But I don’t really see the difference with :

public Tuple <string, string, string> MyCoolFunction()
{
string string0 = “firstValue”;
string string1 = “secondValue”;
string string2 = “firstValue”;
return Tuple.Create(string0, string1, string2);
}

…Of course yours is more elegant ^^

About the question :
I was wondering if there is a way to Invoke different items from the same function at the same time by using just one time the function.
In your exemple I’ll still have to do :

string string0 = MyCoolFunction().Item1;
string string1 = MyCoolFunction().Item2;
string string2 = MyCoolFunction().Item3;

To invoke the three items, wich obviously will call three times a function that already had the three items since the first run…
Something like this would be cool :

List< string> threeStrings = MyCoolFunction().Item1,item2,item3;

But that its not working ^^
I mean I don’t know if this exists, but coud be nice because for my exemple calling two times :

oMesh = Orient(Pt0, Pt1, Pt2, Anchor, teaPotMesh).Item1;
oPlane = Orient(Pt0, Pt1, Pt2, Anchor, teaPotMesh).Item2;

Instead of that it seems more efficient for the process to just run one time and return objects (in that case there is two different data types, Mesh and Plane) like :

List < Object> = Orient(Pt0, Pt1, Pt2, Anchor, teaPotMesh).Item1,Item2;

Let me know if I’m totally wrong :zipper_mouth_face:

Thanks fore the reply, I’m not alone anymore :slight_smile:

new Variant File : GetMultipleType_Method_Test2_workingVariant.gh (44.3 KB)

//Adrian

Tuple<string, string, string> resultStore = MyCoolFunction();
string resultA = resultStore.Item1;
//...