mrName
mrName

Reputation: 117

Reversed for loop

I made this small nested for loop, and it shows no error in C# whatsoever, but when I try to run my small program I get the following error in my TextBox:

System.Windows.Forms.TextBox, Text: System.Windows.Forms.TextBox, Text: Syst...

Here is my code:

int number = textBox.Text..ToString();
for (int row = 0; row < number; row++)
{
    for (int x = number - row; x > 0; x--)
    {
        textBox2.Text = textBox2.Text + "X";
    }
    textBox2.Text = textBox2 + Environment.NewLine;
}

My result should be something like this:


XXXX
XXX
XX
X

I can't figure out what may cause this error.

Upvotes: 0

Views: 349

Answers (9)

Austin Salonen
Austin Salonen

Reputation: 50215

This might inspire you to think about this problem differently:

// I created a simple textbox class so I could do this in a console app
var textBox = new TextBox();
var textBox2 = new TextBox();
textBox.Text = "4";

var number = Convert.ToInt32(textBox.Text);
var descendingXStrings = Enumerable.Range(1, number)
                                   .Select(n => new string('X', n))
                                   .Reverse();
textBox2.Text = string.Join(Environment.NewLine, descendingXStrings);

Console.WriteLine(textBox2.Text);

CW as this does not answer the question directly.

Upvotes: 1

Ankit Jain
Ankit Jain

Reputation: 1286

Try this

int number = int.Parse(textBox1.Text);
for (int row = 0; row < number; row++)
{
    for (int x = number - row; x > 0; x--)
    {
        textBox2.Text = textBox2.Text + "X";
    }
    textBox2.Text = textBox2.Text + Environment.NewLine;
}
  1. Convert TextBox Value in integer
  2. Change textBox2 to textBox2.Text in second last line
  3. Use textBox as multiline textbox

Upvotes: 0

Shumail
Shumail

Reputation: 3143

You cannot assign string to an int , what you are doing as:

int number = textBox.Text..ToString();

Better option is to use int.TryParse(textBox.Text, out number)

AND

Change

textBox2.Text = textBox2 + Environment.NewLine; 

to

textBox2.Text = textBox2.text + Environment.NewLine;

Edit: Even if you change 2 dots to 1, it will give error for int number = textBox.Text.ToString(); - you can't assign string to int

Upvotes: 2

Riv
Riv

Reputation: 1859

int number = textBox.Text..ToString();        

Suppose that was a typo? Either way, check if the value is numeric first.

if (int.TryParse(textBox.Text, out number))
{
       //run your loop here
}

Also,

textBox2.Text = textBox2 + Environment.NewLine;

should be:

textBox2.Text = textBox2.Text + Environment.NewLine;

Upvotes: 2

Reed Copsey
Reed Copsey

Reputation: 564373

You can't assign a string to a number. You need to convert it:

// int number = textBox.Text..ToString();
int number;
if (!int.TryParse(textBox.Text, out number)
{
    // Handle improper input...
}

// Use number now

In addition, when you add the newline, you need to actually append to the Text property, not the TextBox itself:

textBox2.Text = textBox2.Text + Environment.NewLine;

Upvotes: 6

Michael Gunter
Michael Gunter

Reputation: 12811

You're missing a .Text in the second to last line. It should be:

textBox2.Text = textBox2.Text + Environment.NewLine;
                        ^^^^^

or just:

textBox2.Text += Environment.NewLine;

Upvotes: 3

Manoj Purohit
Manoj Purohit

Reputation: 3453

textBox2.Text = textBox2 + Environment.NewLine;

Should be

textBox2.Text = textBox2.Text + Environment.NewLine;

System.Windows.Forms.TextBox is just class name

Upvotes: 3

Golo Roden
Golo Roden

Reputation: 150624

Instead of

textBox2.Text = textBox2 + 

use

textBox2.Text = textBox2.Text + 

in the last line.

That's it ;-)

Upvotes: 5

Jeroen Vannevel
Jeroen Vannevel

Reputation: 44439

You have two dots here.

textBox.Text..ToString();

This should throw a compilation error by the way. And you can't assign it to a variable of type integer.

textBox2.Text = textBox2 + Environment.NewLine;

You have to call a method of the textbox here, presumably textBox2.Text.

Upvotes: 1

Related Questions