0

I am new to C# and I'm trying to get a basic I/O program to work asking for the users name and age. The program doesn't allow me to run because the "static void GetUserData()" part, giving this error:

Expected class, delegate, enum, interface, or struct. 
It does not like the void keyword.

Also the method GetUserData() gives the error Does not exist in current context. As far as I know this follows the rules in C#, I am declared a method to use later, and the declaration is made within a class so it should be ok?

Thanks all I got it sorted. I typed the program exactly how the book said, its a lesson learned anyway.


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

namespace BasicConsoleIO
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("basic input output");
            GetUserData();
            Console.ReadLine();
        }
    }

    static void GetUserData()
    {
    Console.Write("please enter your name: ");
    string userName = Console.ReadLine();
    Console.Write("please enter your age: ");
    string userAge = Console.ReadLine();

    //changes echo colour
    ConsoleColor prevColor = Console.ForegroundColor;
    Console.ForegroundColor = ConsoleColor.red;

    //echo to console
    Console.WriteLine("hello {0}! Your are {1} years old.", userName, userAge);

    //Restore previous color
    Console.ForegroundColor = prevColor;
    }   
}
1
  • 1
    Class methods must be defined inside er... classes. Commented Jul 1, 2013 at 11:30

6 Answers 6

1

Put GetUserData() in a class, for example the one you currently have;

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("basic input output");
        GetUserData();
        Console.ReadLine();
    }

    static void GetUserData()
    {
        Console.Write("please enter your name: ");
        string userName = Console.ReadLine();
        Console.Write("please enter your age: ");
        string userAge = Console.ReadLine();

        //changes echo colour
        ConsoleColor prevColor = Console.ForegroundColor;
        Console.ForegroundColor = ConsoleColor.red;

        //echo to console
        Console.WriteLine("hello {0}! Your are {1} years old.", userName, userAge);

        //Restore previous color
        Console.ForegroundColor = prevColor;
    }   
}

Also, as they're in the same namespace and class, if you're only ever going to need them from there there's no need to have your method as static, it all depends on how you want to be able to access them, or more so, WHERE you want to access them from

Sign up to request clarification or add additional context in comments.

Comments

0

Your method needs to be inside a class, like so:

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("basic input output");
        GetUserData();
        Console.ReadLine();
    }

    static void GetUserData()
    {
        ...
    }
}

1 Comment

oh...one misplaced bracket, because I closed the class and the method had to be inside it. the example in the book did not make this clear.
0

GetUserData() method out of the scope of the class Program. Should be:

class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("basic input output");
            GetUserData();
            Console.ReadLine();
        }

        static void GetUserData() {/*code here*/}
    }

Comments

0

In C#, each method must be inside a class.

You have defined your program

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("basic input output");
        GetUserData();
        Console.ReadLine();
    }
}

and then defined the GetUserData after the closing brace - move it inside the class definition.

Comments

0

The method must be with in the class

namespace BasicConsoleIO
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("basic input output");
            GetUserData();
            Console.ReadLine();
        }

        static void GetUserData()
        {
            Console.Write("please enter your name: ");
            string userName = Console.ReadLine();
            Console.Write("please enter your age: ");
            string userAge = Console.ReadLine();

            //changes echo colour
            ConsoleColor prevColor = Console.ForegroundColor;
            Console.ForegroundColor = ConsoleColor.Red;

            //echo to console
            Console.WriteLine("hello {0}! Your are {1} years old.", userName, userAge);

            //Restore previous color
            Console.ForegroundColor = prevColor;
        }
    }
}

Comments

0

Basically in C# the code should be enclosed within a class.The error can be resolved by moving the GetUserData() to the program class

class Program
{
    static void Main(string[] args)
    {
     Console.WriteLine("basic input output");
     GetUserData();
     Console.ReadLine();
    }

 static void GetUserData()
  {
    Console.Write("please enter your name: ");
    string userName = Console.ReadLine();
    Console.Write("please enter your age: ");
    string userAge = Console.ReadLine();

    //changes echo colour
    ConsoleColor prevColor = Console.ForegroundColor;
    Console.ForegroundColor = ConsoleColor.red;

    //echo to console
    Console.WriteLine("hello {0}! Your are {1} years old.", userName, userAge);

    //Restore previous color
    Console.ForegroundColor = prevColor;
  }   
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.