MTS MTS - 1 year ago 81
C# Question

C# Array Issues; Can't Save Array Index to a Variable

So I have this code here that's supposed to detect whether or someone is a male or female. Essentially, we have a piece of software that connects us to different people on the phone and their name is readily available. What this should (in theory) do is detect the person's name and gender in the array and input it into our form. I have included a little sample of the code here, specifically the part that isn't working and I was wondering if any of you have any clue why this is the case.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GenderChecker
class Class

public static void Check(string stringy)
int gender = 0;
string[] arrayexample = { "Example One Male", "Example Two Female" };
if (arrayexample.Contains(stringy))
int arrayPosition = arrayexample.IndexOf(arrayexample, stringy); //part that doesn't work

static void Main(string[] args)
genderChecker("Example One");

Can anyone tell me a different way to save the array position as an integer because this code doesn't work at all and it's a bit infuriating.

Thank you,

Answer Source

IndexOf() is a static method on the Array class.

do this instead:

arrayPosition = Array.IndexOf(arrayexample, stringy);

if the string isn't in the array, it will return -1

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download