|
| 1 | +--- |
| 2 | +title: BCP076 |
| 3 | +description: Can't index over expression of type <wrong-type>. Arrays or objects are required. |
| 4 | +ms.topic: reference |
| 5 | +ms.custom: devx-track-bicep |
| 6 | +ms.date: 05/20/2025 |
| 7 | +--- |
| 8 | + |
| 9 | +# Bicep diagnostic code - BCP076 |
| 10 | + |
| 11 | +This diagnostic occurs when attempting to use the index operator ([] or [^index]) on an expression that isn't an array or object. This happens when the expression being indexed is of an incompatible type, such as a string, integer, boolean, or null. |
| 12 | + |
| 13 | +## Description |
| 14 | + |
| 15 | +Can't index over expression of type \<wrong-type>. Arrays or objects are required. |
| 16 | + |
| 17 | +## Level |
| 18 | + |
| 19 | +Error |
| 20 | + |
| 21 | +## Examples |
| 22 | + |
| 23 | +The following code attempts to index a string, which triggers BCP076: |
| 24 | + |
| 25 | +```bicep |
| 26 | +var notAnArray = 'hello' |
| 27 | +var invalidAccess = notAnArray[0] // Error: BCP076 - Can't index over expression of type 'hello'. Arrays or objects are required. |
| 28 | +``` |
| 29 | + |
| 30 | +This code tries to use the reverse index operator on an integer: |
| 31 | + |
| 32 | +```bicep |
| 33 | +var number = 42 |
| 34 | +var invalidIndex = number[^1] // Error: BCP076 - Can't index over expression of type '42'. Arrays or objects are required. |
| 35 | +``` |
| 36 | + |
| 37 | +To fix the error, ensure the expression is an array or object: |
| 38 | + |
| 39 | +```bicep |
| 40 | +var sizes = ['small', 'medium', 'large'] |
| 41 | +var first = sizes[0] // Returns 'small' |
| 42 | +var last = sizes[^1] // Returns 'large' |
| 43 | +
|
| 44 | +var settings = { key1: 'value1', key2: 'value2' } |
| 45 | +var value = settings['key1'] // Returns 'value1' |
| 46 | +``` |
| 47 | + |
| 48 | +If you need to extract parts of a string, use string functions like [split](../bicep-functions-string.md#split) or [substring](../bicep-functions-string.md#substring): |
| 49 | + |
| 50 | +```bicep |
| 51 | +var text = 'hello' |
| 52 | +var chars = split(text, '') // Returns ['h', 'e', 'l', 'l', 'o'] |
| 53 | +var firstChar = chars[0] // Returns 'h' |
| 54 | +``` |
| 55 | + |
| 56 | +## Next steps |
| 57 | + |
| 58 | +For more information about Bicep diagnostics, see [Bicep core diagnostics](../bicep-core-diagnostics.md). |
0 commit comments