1

I know this is a duplicate question that's been asked before, but all of the questions I've found refer to C# in general, but not Unity which I believe supports up to .NET 4.6 and not the newest framework?

I have a function that passes multiple variables if a bool is true for each of several categories.

void CheckData()
{
    if (int 1 > int 2)
    {
        otherScript.Function(var 1, var 2, var 3)
    }
}

That function queries an SQL database and defines the inputted variables based on the SQL data. Is there an easy way to return the value of multiple variables? From the research I've been doing it seems there is a way to do so in newer versions of .NET, but not with Unity since it doesn't support Tuples?

I've also read people say you can return an array and then sort the data outside of the function, but then I've read other people saying the opposite and the information I've found is inconsistent.

Could anyone please let me know the simplest way to achieve this? If needed I'll just hard code each individual variable and do it manually, but I'm trying to keep my code clean so if there's a better way to do it I'd like to know before I spend hours coding something improperly.

Edit: The suggested duplicate post link showed how to accomplish this by making another class, but I was looking for a simpler solution which was answered below. Thank you.

NomadicBinary
  • 67
  • 1
  • 2
  • 8
  • 1
    Possible duplicate of [The right way of returning multiple values from a method](https://stackoverflow.com/questions/25643426/the-right-way-of-returning-multiple-values-from-a-method) – soccer7 Nov 29 '18 at 17:53

3 Answers3

5

The solution is still Tuples.

but not with Unity since it doesn't support Tuples?

Unity now supports Tuples and you will need Unity 2017 or above to use it. Go to Edit --> Project Settings --> Player --> Other Settings --> Configuration --> Scripting Runtime Version --> .NET 4.x Equivalent.

Restart Unity and Visual Studio and Tuple should be available to you.


If you're still using Unity version lower than 2017 then you should use the out param or even List to populate the data in the parameter.

Programmer
  • 121,791
  • 22
  • 236
  • 328
3

You have to use the out keyword in c#. This is designed especially for this case

out parameter modifier (C# Reference)

And here is a simple example:

void CheckData()
{
    if (int 1 > int 2)
    {
        int a;
        int b;
        int b;
        otherScript.Function( out a, out b, out c)
    }
 }

 public class OtherScript
 {
    public Function( out int a, out int b, out in c )
    {
        a = 1;
        b = 2;
        c = 4;
    }
 }
György Gulyás
  • 1,290
  • 11
  • 37
  • 1
    You can also pass the value by reference using the ref key, but you have to initialize it before passing it to the function, here are some nice answers: https://stackoverflow.com/questions/388464/whats-the-difference-between-the-ref-and-out-keywords – Felipe Gutierrez Nov 29 '18 at 18:03
  • 1
    By the way in your void CheckData() you typed twice b, I guess you wanted the last one to be c right? – Felipe Gutierrez Nov 29 '18 at 18:04
  • This is exactly what I was looking for, thank you. I actually did use ref as @Felipe Gutierrez suggested, but out and ref were the methods I needed to learn about. You saved me a lot of time, I appreciate it. – NomadicBinary Nov 29 '18 at 18:10
  • Your welcome! But please careful, the ref and out is not for the same purpose. The ref is designed for pass the reference ( avoid the copy) and out designed for get back the value(s) – György Gulyás Nov 29 '18 at 18:13
1

If tuples and out don't work for you (both of which are supremely better options in my opinion), how about a wrapper class? Note: this is effectively what tuples are except that they are generic types and this isn't.

public class Wrapper {
    var a, b, c;
}
public Function(var a, var b, var c ):Wrapper
{
    Wrapper outobj = new Wrapper();
    outobj.a = 1;
    outobj.b = 2;
    outobj.c = 4;
    return outobj;
}