1

i'm in a bit of a bind with a problem that's supposed to mediocre, but seemingly i can't implement the solution.

I have buttons, with a single char on each of them, 26 to be exact (english alphabet), When i click any of them, the loop iterates through the string for the text value on the buttons and replaces it with quotation marks.

The code works, and it prints out the newAlphabet without the clicked character. But when i click another button, it returns the newAlphabet albeit with the previously removed character and removes the new clicked character.

The code is as follows

static string alphabet = "abcdefghijklmnopqrstuvwxyz";
static string newAlphabet = string.Empty;
Button tempBtn = (Button)sender;

for (int i = 0; i < alphabet.Length; i++)
{
  if (alphabet[i].ToString().Contains(tempBtn.Text))
  {
    newAlphabet = alphabet.Replace(tempBtn.Text, ""); 

    MessageBox.Show(newAlphabet);
  }
}

Sorry for grammar or spelling errors, english is not my first language.

Regards, HC

5
  • Do you really want to show the messagebox inside the loop in this event handler? Commented Jul 22, 2011 at 22:44
  • Messagebox is only there for testing purposes. Commented Jul 22, 2011 at 22:45
  • Are you wanting to concatenate each new letter onto newAlphabet? Commented Jul 22, 2011 at 22:45
  • No, just write out the string without the entered character. Commented Jul 22, 2011 at 22:52
  • Unless I'm mistaken, your entire for loop could be replaced with the single line: newAlphabet = newAlphabet.Replace(tempBtn.Text, "");. Commented Jul 22, 2011 at 22:53

4 Answers 4

2

This line

 newAlphabet = alphabet.Replace(tempBtn.Text, ""); 

means you're always getting back to "abcdefghijklmnopqrstuvwxyz" and replacing that.

If you want to keep replacing letters, you need to replace on newAlphabet.

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

Comments

1

A simpler solution would be:

static string alphabet = "abcdefghijklmnopqrstuvwxyz";

private void button1_Click(object sender, EventArgs e)
{
  var tempBtn = (Button)sender;
  alphabet = alphabet.Replace(tempBtn.Text, "");
  MessageBox.Show(alphabet);
}

Note 1:
If the code you posted is in your button click event method then it would not compile. In C# you cannot declare variables static inside methods.

Note 2:
Strings are immutable so alphabet.Replace() returns a new string without affecting the original.

1 Comment

Yes i know i cannot declare static variables inside methods, all of the code was simplified respectively to minimize confusion.
0

If the goal is to remove the clicked letter from the list:

static string newAlphabet = "abcdefghijklmnopqrstuvwxyz";
Button tempBtn = (Button)sender;

newAlphabet = newAlphabet.Replace(tempBtn.Text, ""); 

MessageBox.Show(newAlphabet);

Comments

0

Please note that strings are immutable in C#. "newAlphabet" is being continuously replaced by a modified "alphabet". It will never stick.

Comments

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.