1

I have multiple arrays in a script and I have to find the arrays among them with the string of the name of that array.

For example:

int[] no1, no2, no3, no4, etc..;


void Something(string str) {
     int[] array = int array with name str;
}

So, when I call this function like this:

Something("no1");

it will take the value of array "no1".

Thank you guys in advance.

3
  • 2
    Make a dictionary of arrays instead of a number of array fields?: Dictionary<string, int[]> Commented Jul 12, 2021 at 6:36
  • 3
    Seems XY problem Commented Jul 12, 2021 at 6:37
  • 4
    Why do you want to do this? Commented Jul 12, 2021 at 6:38

1 Answer 1

4

Using a dictionary would solve the problem.
Try this:

// Declare the dictionary
private Dictionary<string, int[]> myArrays;

public void Main()
{
    // Initialize the arrays. This can also be done in constructors or wherever needed
    myArrays.Add("no1",new int[10]);
    myArrays.Add("no2",new int[10]);
    myArrays.Add("no3",new int[10]);
}

void Something(string str){
    int[] array = myArrays[str];
    // CONTINUE YOUR CODE HERE
}

P.S. Don't forget to include this to the top of your file for dictionaries:

using System.Collections.Generic;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.