3

what word can i write to access static function inside class? like self:: in php?

2
  • Your question is unclear. Please give an example of what you're trying to do, in terms of what you're trying to call and from where. Commented Mar 7, 2011 at 10:41
  • Is the status function defined in the same class or a different class? What have you tried so far? Commented Mar 7, 2011 at 10:41

6 Answers 6

4

You just use the type name:

static class Test
{

  public static string GetSomething()
  {
    return "Something";
  }

}

string s = Test.GetSomething();

If you're in the class already you just call the method.

Sign up to request clarification or add additional context in comments.

3 Comments

What do you mean not pretty? Drop the type name from the call if inside the class, all you need to do is call GetSomething() etc.
i want show that calling function belongs to current class where it called
Without an explicit reference to another class then it is assumed that it belongs to the current class or a base class (and you can show inherited calls through the use of the base keyword).
2

Simply use StaticMethodName(...) (when inside the class where the static method is defined) or ClassName.StaticMethodName(...).

1 Comment

@jeroenh: I fixed the typo ~6 minutes ago :)
2

There is no such keyword in C#. You need to use the class name, e.g.

MyClass.StaticMember

1 Comment

From inside the class itself, you don't need to specify the classname for a static method (it's not an error though).
0

Write the name of the class. For example:

public static class MyClass { 
public static void HelloWorld(){}
}

And use it like:

MyClass.HelloWorld();

Comments

0

If your static class is named etc. SampleClass, you can access its functions with SampleClass.YourFunction(); . If you want to call a function in other static method, just use the name of the function .

Comments

0
public class Discover 
{
    static int myVariable = 1;

    public Discover()
    {
      var test = myVariable;
    }
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.