4

Potentially easy question here, I'm getting the error: Cannot apply indexing with [] to an expression of type 'System.Array'

    public Array hello()
    {
        var damn = new[] { a2,a3,a4,a5,a6,a7,a8,a9};
        return damn;
    }
    private void a1disable()
    {

        var a = new[] { a1, a2, a3, a4, a5, a6, a7, a8, a9 };
        var b = hello();

        a[1].Enabled = false;
        b[1].Enabled = false;
    }

a[1].Enabled = false; works absolutely fine! it's just b[1].Enabled = false; which throws the error i described above, i haven't used arrays much before so i'm sorry if the answer seems obvious, i'm just looking for clarification as to why this is happening. Thanks in advance if you can help :)

1
  • I'm surprised the hello() method compiles as you are casting SomeClass[] to Array. Upvote for a question that has shown me something I didn't know. Commented Dec 4, 2013 at 14:06

6 Answers 6

8

All arrays derive from Array but Array is not indexable. Only concrete arrays are indexable. Without knowing the element type that the array has it is impossible getting a value out of it in a strongly typed way.

Make Hello return an int[] or whatever the right element type is.

Sign up to request clarification or add additional context in comments.

Comments

4

Array class does not have any indexer, you have to use the GetValue method, suppose the type of each element in b is TextBox, try this:

((TextBox) b.GetValue(1)).Enabled = false;

If you know the type of all elements beforehand, such as TextBox, why not just use the type TextBox[] as the return type of your hello() method?

public TextBox[] hello(){
  //....
}
//Then you can keep the old code.

2 Comments

a1,a2 etc are actually textboxes, does this mean i can/should change 'Button' to 'Textbox'?
@LucasHolmes no, just replace Button with TextBox, that's just my suppose.
2

Array is primarily a base class for for the system. You shouldn't be using Array as the return here:

The Array class is the base class for language implementations that support arrays. However, only the system and compilers can derive explicitly from the Array class. Users should employ the array constructs provided by the language.

Change the return type to whateverType[].

public whateverType[] hello()
{
    var damn = new[] { a2,a3,a4,a5,a6,a7,a8,a9};
    return damn;
}

Comments

0

You make your syntax a little more simple by returning a generic instead of an array. In your case that would look like:

    public List<object> hello()
    {
        return new List<object> { a2, a3, a4, a5, a6, a7, a8, a9 };
    }

Just replace "object" with the appropriate type. The advantage these have is you can add/remove items later if necessary more easily than you could with an array. These also have expanded support for LINQ syntax. Just a suggestion.

Comments

0

If you are using controls, then you can do:

public Control[] hello()
{
    return new Control[] { a2,a3,a4,a5,a6,a7,a8,a9};
}

This way you can access needed control properties, like Enabled.

I think it is even better to simplify it to a private field:

private Control[] _hello = new Control[] { a2,a3,a4,a5,a6,a7,a8,a9};

Or if you are intend to use it from outside, to a public auto-property:

public Control[] Hello {get; private set;}

    // init somewhere (to example, in the constructor)
    Hello = new Control[] { a2,a3,a4,a5,a6,a7,a8,a9};

Comments

0

If you can't modify the function returning type Array, hello() in your example, and know a base type stored in the array, I use int in my example below. Then you can add a using System.Linq; and change:

var b = hello();

to

var b = hello().Cast<int>().ToArray();

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.