Begginer problem with class in another file

Viewed 45
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace MyFirstProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            
        


        }
    }
}

And another class in another file

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Rock_Paper_Scissors
{
    internal class ComputerGenerateSign
    {
       public string computer;

        Console.ReadKey()


    }
}

Why Visual Studio code have a problem ?>"Error IDE1007 The name 'Console.ReadKey' does not exist in the current context"

2 Answers

Use 'Console.ReadKey();' in the body of a method, That way it will recognize. You didn't write a method inside the 'ComputerGenerateSign' class like the class 'program' has the main method. hope this helps.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Rock_Paper_Scissors
{
    public class ComputerGenerateSign
    {
        public void method1()
        {
            public string computer;

            Console.ReadKey();
        }
       
    }
}

You have made function call in class which was not allowed. In class you can define properties, fields and functions.

You need to call that function inside a function body.

Main.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Rock_Paper_Scissors;


namespace MyFirstProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            ComputerGenerateSign obj=new ComputerGenerateSign(); 
            obj.WaitForUserInput(); 
        }
    }
}

And another class in another file

using System;
using System.Text;

namespace Rock_Paper_Scissors
{
    public class ComputerGenerateSign
    {
        public string computer;

        public void WaitForUserInput()
        {           
            Console.ReadKey();
        }
    }
}
Related