Access methods from another class in C #

I have several classes in the Classes file, and I want all of them to have access to the same global method of saving duplicate code. The problem is that I cannot access the method from another class in my file - any ideas?

So my class1.cs layout looks like this:

public class Job1 { public Job1() { } } public class Methods { public static void Method1() { //Want to access method here from Job1 } } 
+7
c # class
source share
3 answers

You need to specify the class in which they are located. For example:

 public Job1() { Methods.Method1() } 

If the Job1 class is in a different namespace from Methods , then you need to either add a use clause or specify a namespace when calling the method. Name.Space.Methods.Method1()

+10
source share

In fact. Public Job1 () {} is a constructor, not a method. It can be called from the main class by creating an object in the class JOB1. Add the following code here:

 public static void method1() { Job1 j1=new Job1(); } 
Constructor

can be called by creating an object in the corressponding class ....

+1
source share

To access methods of other classes, methods must be static with the public Access modifier.

static - Not bound to an instance of the class, but used by all other instances.

private - access to data is possible only from one class.

public - data can be accessed from other classes, but must be specified.

0
source share

All Articles