Reputation: 301
I learned basic and now I want to learn OOP in C# I have this code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace uceni_cs
{
class Zdravic
{
public void pozdrav()
{
Console.WriteLine("Ahoj světe ! ");
}
}
}
But when I try to call it using this code
namespace uceni_cs
{
class Zdravic
{
public void pozdrav()
{
Console.WriteLine("Ahoj světe ! ");
}
}
Zdravic trida = new Zdravic();
}
In code Zdravic trida = new Zdravic();
is error. A namespace cannot directly contain members such as fields or methods.
What I am doing wrong ? I just want to call the class.
Thanks
Upvotes: 1
Views: 104
Reputation: 3835
In C# there is no such a thing global variable so you can't just create new instance of Zdravic type that does not belong to any class.
I suggest you to read General Structure of a C# Program, and c# Classes and Structs.
Upvotes: 3
Reputation: 13
Create your class object in main method and then use the class properties using that object.
Zdravic trida = new Zdravic();
in main method of you program/application.
Upvotes: 0
Reputation: 1574
You need to create an entry point to your application and instantiate the class there.
class EntryPoint
{
static void Main()
{
Zdravic trida = new Zdravic();
trida.pozdrav();
}
}
Upvotes: 1