0

How can I call method whithout creating class.

Example

public class1 { class2 = new class2(); int size; private void method() { size = class2.size; } } public class2 { private void method() { //call method from class1 } } 
1
  • As mentioned in the answer from @Matt, you could use the static modifier. Please note that when a method is static it cannot access non-static fields. Commented Mar 13, 2019 at 6:38

2 Answers 2

1

You can do that making the method of class1 static (add the static reserved word before private)

That way you can call the method as class1.method();

Hope this is what you are looking for!

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

2 Comments

internal static void method() {...} - we have to change the method into internal or public
Thats correct, of course he has to change the accesibility of the method too in order to use it from another class. Could have added that.
0

I mean it:

public Class1 { Class2 class2 = new Class2(); public int size; public Class1() { class2.handler += method1; } private void method1() { size = class2.size; } } public Class2 { ... public int size; public delegate void Handler(); public Handler handler; private void method2() { size = UpdateSize(); handler?.Invoke(); } private int UpdateSize() { ... } } 

1 Comment

This does not appear to be an answer to your question. If this is meant to make your question clearer, then edit your question and add this code to it. Then delete this answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.