I have one method called "PopulateNameArray" and another method called "FindStudentPosition".
In FindStudentPosition(string name, string[] array), I have
int intLocation = -1;
In a loop I have to compare the contents of the current string array element with the student name. If the names match, assign the element number to intLocation Once found, break out of loop
How am I supposed to do this? Since the parameter in the FindStudentPosition is a different array than where the names are stored, how can I get it to check that array in a loop?
public static void PopulateNameArray(string[] names)
{
Console.WriteLine("*Names Of The Students*");
Console.WriteLine("--------------------- \n");
int intNumber = 5;
for (int i = 0; i < intNumber; i++)
{
Console.Write("Please Enter A Name: ");
names[i] = Console.ReadLine();
}
}
private static int FindStudentPosition(string name, string[] array)
{
int intLocation = -1;
for ()
{
break;
}
return intLocation;
}
This is already implemented for you, with the Array.IndexOf
method:
int index = Array.IndexOf(array, name);
Or use the fact that an array implements IList<T>
, and use IList<T>.IndexOf
:
// IndexOf is implemented explicitly
IList<string> list = array;
int index = list.IndexOf(name);
See more on this question at Stackoverflow