Reputation: 13
As you can find in the c# documentation
System.Console.WriteLine("Hello World!");
System is a namespace and Console is a class in that namespace. The using keyword can be used so that the complete name is not required, as in the following example:
using System;
Console.WriteLine("Hello");
Console.WriteLine("World!");
I'm trying to apply this to my code so I can use a method in a helper class HelperClass
from another class without the need to use the class name HelperClass.HelperMethod();
Like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Sample.Helpers
{
public static class HelperClass
{
public static void HelperMethod()
{
// Do something here
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sample.Helpers;
namespace Sample
{
class Program
{
static void Main(string[] args)
{
// call HelperMethod
HelperMethod();
}
}
}
Unfortunately, the HelperMethod()
is not found by the compiler
I have seen some tutorials code using this, but I have not yet find out what I'm missing...
Upvotes: 1
Views: 57
Reputation: 20363
You need to add the following:
using static Sample.Helpers.HelperClass;
This will allow you to use the static members of HelperClass
without qualifying them with the class name.
More information on using static
directives.
Upvotes: 4