tags:

views:

232

answers:

5

in C# oops 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 am i right. Then i am not able to access the static method in my example can u help me. this is my code.....

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();
    }
}

}

when i am trying to access the static method like "myclass.get()" i am not getting the static method

+4  A: 

You should change it to

public static void get() 

and access it with

myclass.get();

Not an instance of the class.

astander
Thank you it is working fine thank for response Mr. astander
Surya sasidhar
No, it's not certain that he actually **should** change it to public. There are other accessibility levels that makes it accessible, which may be better suited for the situation.
Guffa
+1  A: 

You need to make myclass.get a public method.

mrjoltcola
ya i got it thank you Mr.mrjoltcola and for response also
Surya sasidhar
No, he doesn't need to. There are other accessibility levels that makes it accessible, and might be better depending on the situation. Oh, and what's a "pubic" method? ;)
Guffa
You aren't a public-phobe are you? ;)
mrjoltcola
@mrjoltcola: I often use public myself, but an answer should not say that public is the only possible option when it isn't.
Guffa
@Guffa: Who says what an answer should not say? This is a public Wiki. I think you are being the pedantic police. I chose not to mention all of the possible scope modifiers. There is an answer (besides your own) that explains internal; upvote it. Ever hear a statement like, "You need to get yourself a car"? The implication is a car is a great idea, for some. But a motorcycle or a bike may be more appropriate. public will do. But I think we all get your point.
mrjoltcola
+1  A: 

The default accessibility of a member is the most private that is possible for it. That means that your method is private as you haven't specified any accessibility level.

You have to specify it as public (or internal if you only need access within the same project) to reach it:

public static void get()

or:

internal static void get()
Guffa
ya thank you Mr. Guffa it is working thank you for response
Surya sasidhar
+2  A: 

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.

Enigmativity
A: 

You can have any access modifier you want on your static methods.

Access of course only limits the way it is used outside the class itself.

However, static methods cannot use the this keyword as references to the parent class.

Depending on the access level of the method you can call a method statically as:

ClassName.Property
ClassName.Method
ClassName.Field
Olaseni