N
N
Nikita2018-10-25 15:20:42
C++ / C#
Nikita, 2018-10-25 15:20:42

How to parse String to Int in VS - via toolbox?

Hello everyone, such a question
. I have String Q1; Q2; Q3; Q4; Q5; Q6; Q7; - this is for example, the scores in each of them are Choice from 1 to 5.
I need to pass them to one Score variable which is Int (but if necessary, you can do it and String)
All this is done through the Visual Studio toolbox
At the moment it looks like this

Score = Convert.ToInt32(Q1) + Convert.ToInt32(Q2) + Convert.ToInt32(Q3) +
 Convert.ToInt32(Q4) + Convert.ToInt32(Q5) + Convert.ToInt32(Q6) + 
Convert.ToInt32(Q7)

View from the studio
5bd1b4ba8db72087264704.png

but i get 5555555 tobish they are not added as an int but added as a string.
wtf? :D

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sttrox, 2018-10-25
@etozhetisachniy

Try this function

public int SumStrings(params string[] numbers)
{
   return numbers.Sum(numberString => Convert.ToInt32(numberString));
}

Аргументом функции является массив, т.е. она может принимать сколько угодно строк
You can get the result by passing all string as arguments
Score = SumStrings(Q1, Q2, Q3, Q4, Q5, Q6, Q7);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question