Results 1 to 9 of 9

Thread: [RESOLVED] Return position in an array via a method

  1. #1

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Resolved [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:
    1. private void txtName_DoubleClick(object sender, EventArgs e)
    2.         {
    3.             bool found = false;
    4.             //Search for a phone number
    5.             //Check and make sure the entered name is in the text file and the name field is not blank
    6.             if (!found && txtName.Text == "")
    7.             {
    8.                 return;
    9.             }
    10.             else
    11.             {
    12.                 cboPhone.Text = RetrievePhone(Name).ToString();
    13.             }
    14.         }
    15.         private int RetrievePhone(string Name)
    16.         {
    17.             //Retrieves the customers phone number from the array
    18.             int counter = 0;
    19.            int Number =  0;
    20.             bool found = false;
    21.             string criteria = txtName.Text;
    22.            
    23.                 while (!found && counter < cPhone.Length)
    24.                 {
    25.                     if (criteria.ToUpper() == cName[counter].ToUpper())
    26.                     {
    27.                         found = true;
    28.                     }
    29.                     else
    30.                     {
    31.                         counter += 1;
    32.                     }
    33.  
    34.                 }
    35.                 //CAUTION: found = true Assigns true to bool variable!
    36.                 if (found == true && txtName.Text != "")
    37.                 {              //NOTE: found == true;      
    38.                     Number = cPhone[counter];
    39.                 }
    40.                 return Number;
    41. }

    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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Return position in an array via a method

    Sounds like a job for Array.IndexOf.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Return position in an array via a method

    How would I use 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

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Return position in an array via a method

    Pretty much as the code example in the documentation shows.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    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:
    1. private int RetrieveCustomerNameIndex(string customerPhone)
    2.         {
    3.             //Retrieves the customers phone number from the array
    4.             int counter = 0;
    5.            int Number =  -1;
    6.             bool found = false;
    7.            
    8.                 while (!found && counter < cPhone.Length)
    9.                 {
    10.                     if (customerPhone == cPhone[counter])
    11.                     {
    12.                         found = true;
    13.                         Number = counter;
    14.                     }
    15.                     else
    16.                     {
    17.                         counter += 1;
    18.                     }
    19.  
    20.                 }
    21.                 return Number;
    22.            
    23.         }
    24.  
    25.         private void cboPhone_SelectedIndexChanged(object sender, EventArgs e)
    26.         {
    27.             string customerPhone = cboPhone.Text;
    28.             int position = 0;
    29.             //Search for a customer number
    30.             //Check and make sure the entered name is in the text file and the name field is not blank
    31.             position = RetrieveCustomerNameIndex(customerPhone);
    32.             txtName.Text = cName[position];
    33.         }
    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

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    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

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: [RESOLVED] Return position in an array via a method

    Thanks for the advice.
    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
  •  



Click Here to Expand Forum to Full Width