V
V
Vimake2014-01-07 19:41:16
C++ / C#
Vimake, 2014-01-07 19:41:16

How to add numbers C#?

There is a text in label10 equal to 0.
I wrote such a code that should add 1 as many times as there are lines in textbox2.
But he does not add, but adds instead of 5 it turns out 011111

int lineCount = textBox2.Lines.Length;
            string one = "1";
            for (int line = 0; line < lineCount; line++)
            {
                label10.Text = label10.Text + one;
            }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladlen Grachev, 2014-01-07
@Vimake

I'm in a sisharp with a foot in the tooth, so I may not understand something. But you have explicit string concatenation. Convert values ​​to numbers.
I’m doing an example blindly, I repeat, I’m not familiar with the language, I took the first type conversion manual that came across. I may be wrong somewhere:

int lineCount = textBox2.Lines.Length;
for (int line = 0; line < lineCount; line++)
{
    label10.Text = (Convert.ToInt32(label10.Text)++).ToString();
}

About something like this. In general, if you want to work with numbers, get the numbers first.
Although, given the specifics of your task, everything can be made even easier. Why do extra calculations when the desired value is in the line counter? Just change the postfix increment to the prefix one and make the inequality in the condition non-strict.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question