28

I'm trying to use a List containing string arrays, but when I attempt to access the array elements using square brackets, I receive an error.

My List of arrays is declared like this:

public List<Array> alphabet = new List<Array>();

I also have a string array declared like this:

 string[] encrypted = new string[text.Length];

I am able to access one array, but not the other

string a = alphabet[1][2]; // this gives me an error

string b = encrypted[1]; // this works fine
11
  • 3
    Have you tried using .ElementAt(...? Commented May 11, 2014 at 18:46
  • 2
    How is alphabet declared? Commented May 11, 2014 at 18:52
  • 1
    Please show us the declarations of encrypted, alphabet, text and alphabetc. Since the indexing does not fit together with the declaration, we need to know the declarations. Commented May 11, 2014 at 19:05
  • 5
    Closed as "Off Topic"? This is a perfectly legitimate question and it absolutely is about programming. Commented Nov 13, 2019 at 14:30
  • 1
    Voting to reopen. Commented Sep 16, 2021 at 15:57

5 Answers 5

31

The Error is pretty straightforward; you can't use an indexer on an Array. Array class is a base class for all array types, and arrays are implicitly inherit from Array. But, Array itself doesn't have an indexer. Here is a demonstration of your error:

int[] numbers = new[] {1, 2, 3, 4, 5};

numbers[2] = 11; // Okay

Array arr = numbers as Array;

arr[2] = 11; // ERROR!

So if you want to use the indexer, change your element type to an array of something for example:

public List<string[]> alphabet = new List<string[]>();
Sign up to request clarification or add additional context in comments.

2 Comments

Well actually you can use indexer on Array, but by default it is hidden and you need casting to access it. System.Array implements interface System.Collections.IList, which have indexer. ((IList)arr)[0] will work
The error is pretty straightforward but is pretty counterintuitive. You'd expect to be able to index an array by definition. It even has an indexer, as @SARI stated. And in our case the "definition" is nothing else but the abstract class Array. Now there probably is a strong corner case reason for disallowing indexing but that's another story.
18

Try using .ElementAt. It works on anything that implements IEnumerable, including collections that aren't indexed.

MSDN reference.

I've split your statement up into multiple statements so it's easier to identify the offending line.

Please note - ElementAt is an extension method and you will need to be using the System.Linq namespace to use it.

using System.Linq;

Then in your method:

var n = getnumber(text.ElementAt(i));

var items = alphabet.ElementAt(n);

encrypted[i] = items.ElementAt(symnumb).ToString();

8 Comments

error on the last line. 'System.Array' does not contain a definition for 'ElementAt' and no extension method 'ElementAt' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?)
Edited this answer by accident (wanted to edit my own answer!). Have rolled back. Sorry.
user2517958 - you will need to be using System.Linq.
@Jessedegans see my comment about using System.Linq
This is not true, System.Array implements only IEnumerable and not IEnumerable<T>, while ElementAt() is only available on IEnumerable<T>, what you can do it is either 1) use Cast<T>() on an IEnumerable to cast it to the type IEnumerable<T> you want, or in case you don't care on the type then do Cast<object>(), otherwise 2) use GetValue() as in the answeor of @JeppeStigNielsen
|
11

You should not use the type Array in your code, so change your

public List<Array> alphabet = new List<Array>();

into e.g.

public List<string[]> alphabet = new List<string[]>();

or

public List<List<string>> alphabet = new List<List<string>>();

If you stick to Array for some reason, you cannot use expr[i] but will have to do expr.GetValue(i), but I discourage it because the declared return type is object, and you will end up with a lot of casting.

Comments

0

You can do this, but the way you are referring to an element of array is wrong:

        int[] a4 = {1, 3, 2, 4, 5};
        string[] a5 = new string[5] { "a", "b", "c", "d", "z"};
       
        List<Array> alphabet = new List<Array>();

        alphabet.Add(a4);
        alphabet.Add(a5);

        int x1 = Convert.ToInt32(alphabet[0].GetValue(2));
        string x2 = Convert.ToString(alphabet[1].GetValue(4));

        for (int i = 0; i < alphabet.Count; i++)
        {
            for (int j = 0; j < alphabet[i].Length; j++)
            {
                Console.WriteLine($"element {i} of the alphabet list, element {j} of array =  {alphabet[i].GetValue(j)}");        
            }
        }

Comments

0

SetValue(Object, Int32) Sets a value to the element at the specified position in the one-dimensional Array. The index is specified as a 32-bit integer.

arr.SetValue(2, 11)

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.