Converting Variables | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Converting Variables

An error popped up when I entered in this code: SortedList<string,int> test = new SortedList<string,int>(); string score = Console.ReadLine(); Convert.ToInt32(score); test.Add("Anna",score); It said that I couldn't convert string to integer even though I have the conversion statement in the code. When I leave the code at the conversion statement, it just says "No output" like there's nothing wrong with the code, but as soon as that variable is used in any way, there's an error. I have no idea what I did wrong here.

24th Jul 2020, 6:11 PM
ANNA TOWNSEND
ANNA TOWNSEND - avatar
2 Answers
+ 3
Convertion statement returns converted value. You should store that in a variable. It can't modify original. int st=Convert.ToInt32(score); test.Add("Anna",st);
24th Jul 2020, 6:15 PM
Jayakrishna 🇮🇳
+ 4
Or just move it inside the Add() call test.Add("Anna", Convert.ToInt32(score));
24th Jul 2020, 6:18 PM
ChaoticDawg
ChaoticDawg - avatar