1

I am stuck on an assignment for my Introductory Programming class, and can't get past my instructor's suggestions to generate any working code.

Assignment: The user will type in a full name separated by spaces. When btnGetName is pressed, your program will analyze the text box and extract the first name and place it in the first name label, the middle name in the middle name label and finally the last name into the last name label (assuming they all exist).

If only one name is entered (Smith), assume it is the last name and leave first and middle names blank

If two entries are made (Joe Smith) assume it is the first and last.

If there are three entries in the text box, assume this includes first, middle and last names.

If no entries are made leave all labels empty.

If more than three entries are made give the user an error message, wipe the text box and place the cursor back into the text box.

Prevent an error from occurring using if/then or try/catch (if necessary).

Make the form look somewhat professional. Hint:

Split the text box into an array. Use arrayname.count to determine how many entries are made (how many items in the array). Use an if/then/else to decide how to populate the name labels. This should take less than 7 lines of code in your button click event handler.

Example:

           if (myarray.count==1)
            {
                lblLast.text=myarray[0].ToString();
                //I would wipe out the contents in the other labels here
            }
           else if(myarray.count==2)
            {

                ...

            }

I have been stuck for hours-- probably because I am frustrated with something else, but I cannot get anything to display in my labels. From a lecture, the professor shares the Liststring... as his desired format for splitting the string into an array. This is what I have under btnGetName right now:

    private void btnGetName_Click(object sender, EventArgs e)
    {
        string txtFullName = "";
        List<string> namesArray = txtFullName.Split(' ').ToList();
        namesArray.Count();

        foreach (string name in namesArray)
        {
            if (namesArray.Count == 1)
            {
                lblLast.Text = namesArray[0].ToString();
            }
            else if (namesArray.Count == 2)
            {
                lblFirst.Text = namesArray[0].ToString();
                lblLast.Text = namesArray[1].ToString();
            }
            else if (namesArray.Count == 3)
            {
                lblFirst.Text = namesArray[0].ToString();
                lblMiddle.Text = namesArray[1].ToString();
                lblLast.Text = namesArray[2].ToString();
            }
        }
1
  • Hi Katies, and welcome to Stack Overflow! I think you should start by trying to create a Minimal, Complete, and Verifiable example. Actually, I think if you do this you will probably solve the problem in so doing. You are thinking about all of the problems at once right now, but it sounds like you need to just think about one problem: how to get something to display in a label to begin with! Then I think you should edit your question to be a lot shorter, and ask specifically how to display text in a label, since that is where you are stuck. Thanks! Commented May 29, 2018 at 0:15

1 Answer 1

1

By looking at your example, your List<string> namesArray is empty. You are taking an empty string and splitting it, which will just give you an empty list.

You should instead be something like this, where inputText is the edit text element that a user will enter the names:

string txtFullName = inputText.Text;
List<string> namesArray = txtFullName.Split(' ').ToList();

I would also like to add that the foreach loop is redundant; you are essentially assigning your 3 labels over and over again until it reaches the end of the loop. Remove the foreach block, but keep its contents.

And one more edit, another huge redundancy is the .ToString() cast on each item in namesArray, which is by definition a list of strings. You can safely remove those extra calls.

Sign up to request clarification or add additional context in comments.

7 Comments

Thank you! I completely understand this fix -- also why the foreach/.tostring were redundant--thought I'd throw it all in there just in case. Now I'm running into two other things. I've added the inputText.Text;, but it 'doesn't exist in current context' with .Text or without. Seriously though, you've successfully helped to save my weekend. I was drowning for a second.
@katies1987 the inputText element is arbitrary in my example; In your winforms designer, you need to use the Id of the edit text box that you want the user to enter in the name. So you create the edit text in the designer -> click it -> go to properties and find/edit the Id property. That is the what you should use in place of my inputText in the example. If my answer helped you, you should consider marking it as accepted!
Got that. The user input is txtFullName, so I swapped it...and it still isn't working. That's where I'm stuck ATM.
@katies1987 do you mind describing what you mean by it still isn't working? Does intellisense give you an error(red line underneath a member), and if so what does it say? Does the compiler throw an exception, and if so what does the message say? Does it just not produce any/expected results?
Sure, intellisense is giving me: CS0103 The name 'txtFullName' does not exist in the current context, and I also get a build error, but no explanation.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.