Click here to Skip to main content
15,881,776 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DebugFixMethods
{
    class Program
    {
        static void Main(string[] args)
        {
            (new Program()).run();
        }


        void run()
        {
            int choice = 0;

            WritePrompt();
            choice = ReadChoice();
            WriteChoice(choice);

        }

        private void WritePrompt()
        {
            throw new NotImplementedException();
        }

        void Writeprompt()
        {
            Console.WriteLine("Please select a course for which you want to register by typing the number inside []");
            Console.WriteLine("[1]IT 145\n[2]IT 200\n[3]IT 201\n[4]IT 270\n[5]IT 315\n[6]IT 328\n[7]IT 330");
            Console.Write("Enter your choice : ");
        }

        int ReadChoice()
        {
           string s = "";
            s = Console.ReadLine();
            return (s);
        }

        void WriteChoice(int choice) => Console.WriteLine("Your choice is {0}", choice);

        private class choice
        {
        }
    }
}


What I have tried:

I have tried changing string s to int s, but that doesn't work, and I also think that some of the other parts of the code could be changed to run better but I don't know.
Posted
Updated 20-Jan-23 7:33am
Comments
PIEBALDconsult 20-Jan-23 12:42pm    
Look into Int32.TryParse

1 solution

Quote:
cannot implicitly convert string to int

This is not a "debuggable" problem: debugging is the process of finding and fixing problems that occur while your app is running - called runtime errors
What it is is a syntax error and it is produced while your app is being compiled. And any syntax error prevents an EXE file being created, so you app cannot run!

You should expect to get syntax errors every day, probably many times a day while you are coding - we all do regardless of how much experience we have! Sometimes, we misspell a variable, or a keyword; sometimes we forget to close a string or a code block. Sometimes the cat walks over your keyboard and types something really weird. Sometimes we just forget how many parameters a method call needs.

We all make mistakes.

And because we all do it, we all have to fix syntax errors - and it's a lot quicker to learn how and fix them yourself than to wait for someone else to fix them for you! So invest a little time in learning how to read error messages, and how to interpret your code as written in the light of what the compiler is telling you is wrong - it really is trying to be helpful!

So read this: How to Write Code to Solve a Problem, A Beginner's Guide Part 2: Syntax Errors[^] - it should help you next time you get a compilation error!

In this case you have a string value, any you are trying to return it as an integer:
int ReadChoice()
        {
           string s = "";
            s = Console.ReadLine();
            return (s);
        }
The system is saying "You can't do that" because the two types are not related. What you have to do is Parse the string to extract is as an appropriate number:
C#
string inp = "12345";
int val;
if (!int.TryParse(inp, out val))
   {
   ... report problem to user ...
   }
...
return val;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900