user3301402
user3301402

Reputation: 11

Call method in class from another class in C#

I have a small problem when I'm trying to call method from another class (not Main class). Here's my testing code:

using System;

namespace ConsoleApplication3
{
    public class Program
    {
        static void Main(string[] args)
        {
            Class1 cl = new Class1();
            cl.TestMethod();
        }
    }

    public class Class1
    {
        public string TestMethod()
        {
            return "test";
        }
    }

    public class Class2
    {
        Class1 cl = new Class1();
        cl.TestMethod(); //Error here
    }
}

What should I do to call TestMethod in Class2?

Upvotes: 0

Views: 35254

Answers (6)

HIMANSHU GOHIL
HIMANSHU GOHIL

Reputation: 1

class is by default body is private and private access modify Rules : Not assign value in private variable and use only method other class method is pubic but use this class is private then you not access this object to method

Upvotes: 0

Pavel Arhipov
Pavel Arhipov

Reputation: 1

There is a simle solution. Just as you do it with Math.Abs(); You should use static method,

using System;

namespace ConsoleApplication3
{
    public class Program
    {
        static void Main(string[] args)
        {
            Class1 cl = new Class1();
            cl.TestMethod();
            C_Auxilary.F_TestMeth();   
        }
    }

    public class Class1
    {
        public string TestMethod()
        {
            return "test";
        }
    }

    public class Class2
    {
        Class1 cl = new Class1();
        cl.TestMethod(); //Error here
    }
}

namespace ConsoleApplication3
{
    public static class C_Auxilary
    {
        public static void F_TestMeth()
        {
            ;
        }
    }
}

Upvotes: -1

Nikita Staroseltsev
Nikita Staroseltsev

Reputation: 31

You can't call methods into class body. Specify method or, for example, put your code into a Class2 constructor like below:

using System;

namespace ConsoleApplication3
{
    public class Program
    {
        static void Main(string[] args)
        {
            Class1 cl = new Class1();
            cl.TestMethod();
        }
    }

    public class Class1
    {
        public string TestMethod()
        {
            return "test";
        }
    }

    public class Class2
    {
        Class1 cl = new Class1();

        public Class2()
        {
            cl.TestMethod();
        }
    }
}

Upvotes: 3

NewZeroRiot
NewZeroRiot

Reputation: 552

You've defined a second class, which needs a method, or constructor inside it in order for this to work. Wrap it around the instantiation and it will be accessible :)

Upvotes: 0

Nick Bailey
Nick Bailey

Reputation: 3162

I think you have classes confused with methods.

A method is a subroutine, a bit of code that can be invoked from otjer parts of the code and runs sequentially.

A class is a data type. You create specific instances of a class and then access its properties or call its methods.

So Main is a method, not a class. Specifically it is a method of the program class.

In a class, all executable code must be inside a method. The one (sort of) exception to this rule is that you can initialize members outside of a method. That's what you did with c1 = new Class1(). These statements are implicitly executed when you create the class with new.

To invoke the class1 method you have to either declare a method on class2 to do it.l, or define a constructor which inokes it. However it is very bad practice to have a constructor that does a lot of work. A construct should only do things like initialize members which are required for the class to function.

Upvotes: 1

Coder1409
Coder1409

Reputation: 498

You can't call a method from outside a method,constrcutor or a property change class two to this:

public class Class2
{
    public Class2
    {
       Class1 cl = new Class1();
       cl.TestMethod(); //Error here
    }}

Upvotes: 2

Related Questions