|
-
Jun 1st, 2010, 07:34 PM
#1
[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
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
-
Jun 1st, 2010, 07:42 PM
#2
Re: Return position in an array via a method
Sounds like a job for Array.IndexOf.
-
Jun 1st, 2010, 08:29 PM
#3
Re: Return position in an array via a method
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
-
Jun 1st, 2010, 08:40 PM
#4
Re: Return position in an array via a method
Pretty much as the code example in the documentation shows.
-
Jun 3rd, 2010, 01:50 AM
#5
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];
}
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
-
Jun 3rd, 2010, 01:59 AM
#6
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.
-
Jun 3rd, 2010, 02:11 AM
#7
Re: [RESOLVED] Return position in an array via a method
Ah ok, yes it was homework sorry for not mentioning it.
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
-
Jun 3rd, 2010, 02:20 AM
#8
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.
-
Jun 3rd, 2010, 03:06 AM
#9
Re: [RESOLVED] Return position in an array via a method
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|