Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make method call another one in classes?

Tags:

methods

c#

call

Now I have two classes allmethods.cs and caller.cs.

I have some methods in class allmethods.cs. I want to write code in caller.cs in order to call a certain method in the allmethods class.

Example on code:

public class allmethods
public static void Method1()
{
    // Method1
}

public static void Method2()
{
    // Method2
}

class caller
{
    public static void Main(string[] args)
    {
        // I want to write a code here to call Method2 for example from allmethods Class
    }
}

How can I achieve that?

like image 318
Mina Hafzalla Avatar asked Apr 25 '13 23:04

Mina Hafzalla


People also ask

How do I call a method in another class?

A private method can be called from another Class by using reflection API. A private method has more accessibility restrictions than other methods. A private method can be accessed from another class using getDeclaredMethod(), setAccessible() and invoke() methods of the java. lang.

Can you call a method within a class?

In this program, you have to first make a class name 'CallingMethodsInSameClass' inside which you call the main() method. This main() method is further calling the Method1() and Method2(). Now you can call this as a method definition which is performing a call to another lists of method.

How do you call a class inside another class?

Inner classes To instantiate an inner class, you must first instantiate the outer class. Then, create the inner object within the outer object with this syntax: OuterClass. InnerClass innerObject = outerObject.


1 Answers

Because the Method2 is static, all you have to do is call like this:

public class AllMethods
{
    public static void Method2()
    {
        // code here
    }
}

class Caller
{
    public static void Main(string[] args)
    {
        AllMethods.Method2();
    }
}

If they are in different namespaces you will also need to add the namespace of AllMethods to caller.cs in a using statement.

If you wanted to call an instance method (non-static), you'd need an instance of the class to call the method on. For example:

public class MyClass
{
    public void InstanceMethod() 
    { 
        // ...
    }
}

public static void Main(string[] args)
{
    var instance = new MyClass();
    instance.InstanceMethod();
}

Update

As of C# 6, you can now also achieve this with using static directive to call static methods somewhat more gracefully, for example:

// AllMethods.cs
namespace Some.Namespace
{
    public class AllMethods
    {
        public static void Method2()
        {
            // code here
        }
    }
}

// Caller.cs
using static Some.Namespace.AllMethods;

namespace Other.Namespace
{
    class Caller
    {
        public static void Main(string[] args)
        {
            Method2(); // No need to mention AllMethods here
        }
    }
}

Further Reading

  • Static Classes and Static Class Members (C# Programming Guide)
  • Methods (C# Programming Guide)
  • using static directive (C# Reference)
like image 183
p.s.w.g Avatar answered Oct 17 '22 23:10

p.s.w.g