0

I have three string arrays:

private readonly string[] PMCTableColumnNames = new string[] { "PMCIP", "Description", "Cam1ReferencePoints", "Cam2ReferencePoints", "DataserverIP" };
private readonly string[] PMDTableColumnNames = new string[] { "PMDIP", "Description" };
private readonly string[] PMDZonesTableColumnNames = new string[] { "PMDIP", "Description", "Zone" };

I want to construct an array that points to these arrays as:

private var[] ArrayReferences = new var[] { PMCTableColumnNames, PMDTableColumnNames, PMDZonesTableColumnNames };

When i put index to this (ArrayReferences) array, i want to get all elements of the specific array like this:

string[] _PMDTableColumnNames = ArrayReferences[1];

How can i do that?

5
  • 1
    Why are you using var[]? And are you telling us that string[] _PMDTableColumnNames = ArrayReferences[1]; is not working? Commented Jul 7, 2012 at 10:52
  • @Oded: I believe it is legal to use var[] here, since the compiler can infer the type? Commented Jul 7, 2012 at 10:54
  • @carlpett - I don't believe it is a valid use. Commented Jul 7, 2012 at 10:55
  • No, i write "var" because i want to express my idea. I know that it can't be "var". Commented Jul 7, 2012 at 10:55
  • Then don't post code that can't even compile (unless that's the point of the question). Commented Jul 7, 2012 at 10:56

3 Answers 3

3

You can create an array of arrays:

private string[][] ArrayReferences;

Note you'll have to initialise ArrayReferences in the constructor since you can't reference the other arrays in the field initialiser. i.e.

public class ClassName
{
    private readonly string[] PMCTableColumnNames = new string[] { "PMCIP", "Description", "Cam1ReferencePoints", "Cam2ReferencePoints", "DataserverIP" };
    private readonly string[] PMDTableColumnNames = new string[] { "PMDIP", "Description" };
    private readonly string[] PMDZonesTableColumnNames = new string[] { "PMDIP", "Description", "Zone" };

    private string[][] ArrayReferences;

    public ClassName()
    {
        ArrayReferences = new string[][] { PMCTableColumnNames, PMDTableColumnNames, PMDZonesTableColumnNames };
    }

    void SomeMethod()
    {
        string[] _PMDTableColumnNames = ArrayReferences[1];
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

@petre - Should? That's not a requirement of the language. How do you mean "should"?
@Oded because the first line of this answer gives an error: Cannot access non-static field in static context if the three arrays are not static.
by the way: the term for an array of arrays is jagged array.
0
string[][] ArrayReferences = new [] { PMCTableColumnNames, PMDTableColumnNames, PMDZonesTableColumnNames };

or just

var ArrayReferences = new [] { PMCTableColumnNames, PMDTableColumnNames, PMDZonesTableColumnNames };

should do it.

Comments

0

You simply can use ArrayList class it's meant for this kind of purposes.

3 Comments

If the collection length is fixed, an array is fine. If you need to dynamically modify the collection, then a generic List<T> is preferable to ArrayList.
No I don't think so, ArrayList has add method by which you can add elements to your ArrayList dynamically, And you don't need to specify the length of the ArrayList in advance. But yes for a typesafe list List<T> is a better option.
@yogi might be a good idea to read @lee's answer again, and read up on List and ArrayList.

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.