When we have a static method in a class it access only static members right and the static method can access only with class name. So I am not able to access the static method in my example:
class myclass
{
int i ; static int j ;
static void get()
{
j = 101;
Console.WriteLine(j.ToString ());
}
public void test()
{
i = 11; j = 12;
Console.WriteLine(i.ToString());
Console.WriteLine(j.ToString());
}
}
class Program
{
static void Main(string[] args)
{
myclass clsmyclas = new myclass();
clsmyclas.test();
Console.ReadLine();
}
}
}
A static function in C is a function that has a scope that is limited to its object file. This means that the static function is only visible in its object file. A function can be declared as static function by placing the static keyword before the function name.
Access to static functions is restricted to the file except where they are declared. When we want to restrict access to functions from outer world, we have to make them static. If you want access functions from other file, then go for global function i.e non static functions.
You should change it to
public static void get()
and access it with
myclass.get();
Not an instance of the class.
Your issue is a simple one. The default accessor for a static void
method is private
. Simply add either public
or internal
in front of the get
method and you're good to go.
Also, it would be best not to call the method get
to avoid confusion with properties.
You need to make myclass.get a public method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With