[RESOLVED] Return position in an array via a method
Hi,
How do I return position in an array via a method?
I have this code to retrieve the corresponding phone number from the array when a customer name is entered in the textbox name.
c# Code:
private void txtName_DoubleClick(object sender, EventArgs e)
{
bool found = false;
//Search for a phone number
//Check and make sure the entered name is in the text file and the name field is not blank
if (!found && txtName.Text == "")
{
return;
}
else
{
cboPhone.Text = RetrievePhone(Name).ToString();
}
}
private int RetrievePhone(string Name)
{
//Retrieves the customers phone number from the array
int counter = 0;
int Number = 0;
bool found = false;
string criteria = txtName.Text;
while (!found && counter < cPhone.Length)
{
if (criteria.ToUpper() == cName[counter].ToUpper())
{
found = true;
}
else
{
counter += 1;
}
}
//CAUTION: found = true Assigns true to bool variable!
if (found == true && txtName.Text != "")
{ //NOTE: found == true;
Number = cPhone[counter];
}
return Number;
}
However, I was told I needed to retrieve the name by entering the phone number and returning the position the match occurred.
Thanks,
Nightwalker
Re: Return position in an array via a method
Sounds like a job for Array.IndexOf.
Re: Return position in an array via a method
Re: Return position in an array via a method
Pretty much as the code example in the documentation shows.
Re: Return position in an array via a method
This is method I was suppose to use to retrieve the names associated with the phone numbers.
c# Code:
private int RetrieveCustomerNameIndex(string customerPhone)
{
//Retrieves the customers phone number from the array
int counter = 0;
int Number = -1;
bool found = false;
while (!found && counter < cPhone.Length)
{
if (customerPhone == cPhone[counter])
{
found = true;
Number = counter;
}
else
{
counter += 1;
}
}
return Number;
}
private void cboPhone_SelectedIndexChanged(object sender, EventArgs e)
{
string customerPhone = cboPhone.Text;
int position = 0;
//Search for a customer number
//Check and make sure the entered name is in the text file and the name field is not blank
position = RetrieveCustomerNameIndex(customerPhone);
txtName.Text = cName[position];
}
Re: [RESOLVED] Return position in an array via a method
At no point did you tell us that you had to use a loop. The only reason that I can think of that you would have to use a loop rather the vastly simpler Array.IndexOf is because this is homework and you're learning how to use loops. Is that the case? Your RetrieveCustomerNameIndex method is functionally equivalent to Array.IndexOf, so why bother re-inventing the wheel otherwise? If you're asking questions about homework then it's polite to say so. Otherwise we might think that you're trying to trick us into doing your homework for you. If this is not homework then I'm at a loss to see the point.
Re: [RESOLVED] Return position in an array via a method
Ah ok, yes it was homework sorry for not mentioning it.
Re: [RESOLVED] Return position in an array via a method
It's always a good idea to mention that it's a homework question because the answers will often be different. People will often be more inclined to post instructions rather than code, which is only fair, I think you'll agree. Also, learning projects are often rather contrived because they are designed to illustrate a point. This is a fine example, as you were required to use a loop to achieve something that, in a real-world app, would be a one-line no-brainer. Knowing the requirements is always important.
Re: [RESOLVED] Return position in an array via a method