How to get an element within an array
Solution 1:
IndexOf
is a static method defined on the Array
class. Since your code is not part of that class, you need to prefix it with the class name, as you have done in your second attempt:
Array.IndexOf(Alarm_Names, code)
Other than that, the only compiler error in your code is the missing semicolon from the Alarm_Names
field.
Having fixed that, your code will compile (barring other errors in the parts of the code which aren't shown).
using System; // needed for accessing "Array"
class YourClass
{
static string[] Alarm_Names = new string[3] {"A1", "A2", "A3"};
static bool[] arr_Alarms = new bool[3] {false, false, false};
internal static void HandleAlarm(..., string code, ...)
{
if (arr_Alarms[Array.IndexOf(Alarm_Names, code)]) return;
If it still won't compile, you'll need to list the actual compiler errors you're getting.
NB: If you're using C# 6 or later, you could use the using static
feature to make all static
members of the Array
class available to call without the Array.
prefix:
using static System.Array;
class YourClass
{
static string[] Alarm_Names = new string[3] {"A1", "A2", "A3"};
static bool[] arr_Alarms = new bool[3] {false, false, false};
internal static void HandleAlarm(..., string code, ...)
{
if (arr_Alarms[IndexOf(Alarm_Names, code)]) return;
static modifier | using directive | C# Reference