I am new to C# and visual studio 2005
I created a new Console Application project in VS2005 and added a Class1.cs file to the existing Program.cs file that was created by default.
The Class1.cs file has the following simple code:
public class Class1
{
public Class1()
{
}
~Class1()
{
}
public void PrintMessage()
{
Console.WriteLine("\nHello\n");
Console.Read();
}
}
And program.cs file has the following:
class Program
{
static void Main(string[] args)
{
PrintMessage();
}
}
When I try to compile I get the following error:
The name ‘PrintMessage’ does not exist in the current context.
Any help?
Thanks, Viren
You need a reference to the class where the method is. You must do that with:
Since Class1 is not static, you also need an instance of that class. You could do that with:
Finally, you have to call the method prefixed with the instance variable. Your code will look something like this:
If you want, you can shorten this code with the sentence
that do the same as before.