locked
System.Array - How to access an occurrence or element? RRS feed

  • Question

  • I am trying to create and use an array of integers that starts at 1 and end at 7.

                   Array A= Array.CreateInstance(typeof(int), new[] { 7 }, new[] { 1 });

    //Trying to reference an occurance givs errors!!!
    A[1]=1;  //error

    I get:

    Compilation error (line 11, col 3): Cannot apply indexing with [] to an expression of type 'System.Array'

    How can I resolve this. I want to use an array because I call a method that expects an array.

    Thursday, November 26, 2020 4:08 AM

Answers

  • In that case, you will have to use GetValue and Setvalue. Although I have no idea why you would want to do that.



    • Edited by Ante Meridian Thursday, November 26, 2020 5:47 AM Changed the pic slightly.
    • Marked as answer by ForEverLearning Thursday, November 26, 2020 7:16 AM
    Thursday, November 26, 2020 5:41 AM

All replies

  • Greetings ForEverLearning.

    That code of yours will create a multi-dimensional array. I suggest something a bit simpler, like so.

             int[] A = new int[] { 1, 2, 3, 4, 5, 6, 7 };
    
             for(int i = 0; i < A.Length; i++)
             {
                Console.WriteLine(A[i]);
             }

    Also, don't forget that the first element of an array is 0 not 1.

    • Proposed as answer by KHURRAM RAHIM Thursday, November 26, 2020 10:00 AM
    Thursday, November 26, 2020 4:31 AM
  • Thanks for your reply. In my case, I want to use an array that starts from 1 not zero, so I used the strange System.Array class with CreateInstance.
    • Proposed as answer by KHURRAM RAHIM Thursday, November 26, 2020 9:53 AM
    • Unproposed as answer by KHURRAM RAHIM Thursday, November 26, 2020 10:00 AM
    Thursday, November 26, 2020 4:34 AM
  • In that case, you will have to use GetValue and Setvalue. Although I have no idea why you would want to do that.



    • Edited by Ante Meridian Thursday, November 26, 2020 5:47 AM Changed the pic slightly.
    • Marked as answer by ForEverLearning Thursday, November 26, 2020 7:16 AM
    Thursday, November 26, 2020 5:41 AM
  • EXCELLENT. Thank you. I wanted to use this approach initially thinking it is a simple thing to do and since I am trying to re-code an algorithm from the very old past where arrays used 1 as a start and wanted not to alter the algorithm code, I thought this would be the way to get. Apparently it is not obvious. Congratulations on finding this as I have done an extensive search.
    • Proposed as answer by KHURRAM RAHIM Thursday, November 26, 2020 12:09 PM
    Thursday, November 26, 2020 7:15 AM
  • Note that you should be able to use conventional array accessing as well.

    Array arr = Array.CreateInstance(typeof(int), new[] { 7 }, new[] { 1 });
    foreach(int n in arr)
    {
        Console.WriteLine(n);
    }
    Console.WriteLine();
    for(int n = 1; n <= arr.Length; ++n)
    {
        arr.SetValue(n, n);
    }
    foreach (int n in arr)
    {
        Console.WriteLine(n);
    }
    
    Output:

    0
    0
    0
    0
    0
    0
    0

    1
    2
    3
    4
    5
    6
    7

    - Wayne

    Thursday, November 26, 2020 7:33 AM
  • This is very good and furhter helps me to understand about System.Array. Thanks for posting and taking the time to explain. Stay safe :)
    Thursday, November 26, 2020 9:55 AM