Cannot apply indexing to type System.Array?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Chiefy
    New Member
    • Nov 2007
    • 10

    Cannot apply indexing to type System.Array?

    C# APP
    Basically i am trying to use indexing on an array that i have fed into a method as its parameter. Here is a simplified version of my code.

    Code:
    public void Inputs(string path)
    {
                string line;
                line = inputstream.ReadToEnd();
                string[] words;
                char[] seperators = { '\t', '\n' };
                words = line.Split(seperators);
    
                listSeperation(words, line);
    }
    public void listSeperation(Array words, string line)
    {  
                string[] UID = new string[studentlength];
                int j=0;
    
                for (int i = 1; i < words.Length; i = i + 5)
                {
                    UID[j++] = words[i];  //error
                }
    }
    I get an error saying that it "Cannot apply indexing with [] to an expression of type System.Array" I cant see why there should be any problem with my code. Any help or advice would be great.

    Thanks
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    System.Array is a very base class for dealing with arrays (sorting, etc)
    You could do it with
    public void listSeperation( Array words, string line)
    But you would have to use the .GetEnumerator( ) instead of indexing.

    Instead, change your function protype to be this:
    Code:
    public void listSeperation(string[] words, string line)
    Also, you are not starting at the first spot in the array (which would be index 0) you are starting with the 2nd spot (index 1)

    As we've had this conversation before, I again suggest you read some basics on arrays.


    As well as basics on program flow and logical statement creation.

    Comment

    • Chiefy
      New Member
      • Nov 2007
      • 10

      #3
      Originally posted by Plater
      System.Array is a very base class for dealing with arrays (sorting, etc)
      You could do it with
      public void listSeperation( Array words, string line)
      But you would have to use the .GetEnumerator( ) instead of indexing.

      Instead, change your function protype to be this:
      Code:
      public void listSeperation(string[] words, string line)
      Also, you are not starting at the first spot in the array (which would be index 0) you are starting with the 2nd spot (index 1)

      As we've had this conversation before, I again suggest you read some basics on arrays.


      As well as basics on program flow and logical statement creation.

      Thanks for the help, starting at 1 is intentional as i dont want the first part of the array as its the heading of the list

      Comment

      • Anton McCovovic
        New Member
        • Jul 2010
        • 1

        #4
        The correct answer is to use Array.GetValue( int index):

        Code:
        UID[j++] = words.GetValue(i);

        Comment

        • Nate66873
          New Member
          • Aug 2013
          • 1

          #5
          It works

          Originally posted by Anton McCovovic
          The correct answer is to use Array.GetValue( int index):

          Code:
          UID[j++] = words.GetValue(i);
          Yep this solution works. I had a similar issue read the response from Plater that did not make sense in my situation. The link he referenced was generic and did not apply specifically enough to the situation at hand. The response from Anton was concise and a huge help.

          Thanks guys.

          Comment

          Working...