How can be call the method from another class inherited
let's see my code please:
class Program : classA
{
static void Main(string[] args)
{
// how i can call method ToDo without create an instance like below
//classA c = new classA();
//c.ToDo();
Console.ReadLine();
}
}
class Program2 : classB
{
static void Main(string[] args)
{
// how i can call method ToDo
//ToDo()
Console.ReadLine();
}
}
public abstract class classB
{
public void ToDo()
{
Console.WriteLine("classB");
}
}
public class classA
{
public void ToDo()
{
Console.WriteLine("classA");
}
}
how i can call the method in Either way, please help me.
There are a couple ways to do what you want to do (they're kind of similar or even the same).
One way is to create a class with a static method:
public class classA
{
public static void ToDo()
{
Console.WriteLine("classA");
}
}
then call it like:
classA.ToDo();
Another way is to add another static method to the class that contains Main:
class Program2 : classB
{
static void Main(string[] args)
{
ToDo()
Console.ReadLine();
}
static void Todo()
{
// do stuff here
}
}
If u want to call ToDo() function into [class Program : classA] and [class Program : classB]
Without creating Instance.
U have to Define ToDo() function as static, then u can call this method with class name in anywhere.
public static void ToDo(){}
Related
using System;
namespace Testing
{
class Program
{
static void Main(string[] args)
{ }
}
public class A
{
public void method1()
{ }
}
public class B : A
{
public void method2()
{ }
}
public class Test
{
A a = new A();
a.method1();
}
}
Please paste this code in VS and Please explain me why it is not in current context?
Inside a class is not the right place to call most functions:
public class Test
{
A a = new A();
a.method1();
}
Just put stuff into the main function, wich is there specifically for that part of the programming:
static void Main(string[] args)
{
A a = new A();
a.method1();
}
you can not have statements directly in class.
public class Test
{
A a = new A();
a.method1(); // this is not possible.
}
please modify your class as below:
public class Test
{
public void InvokeMethodOnA()
{
A a = new A();
a.method1();
}
}
or like this;
public class Test
{
A a = new A();
public void InvokeMethodOnA()
{
a.method1();
}
}
You can call method in body of method (sounds strange, but I don't have a better explanation).
In your case you mix a definition of the class Test with a context of function. I hope code with comments will be more descriptive:
public class Test
{
// this is not a local variable, this is a definition of field with initialization
A a = new A();
// you try call method on field, but in context of class definition, which is prohibited
a.method1();
}
Here is the code I currently have, the question follows after:
class Program
{
static void Main(string[] args)
{
var obj1 = new A();
obj1.DoIt();
obj1.SetFlyBehavior(new BehaviorB());
obj1.DoIt();
string input = Console.ReadLine();
}
};
class BaseOfA
{
protected ObjectBehavior behavior;
public void DoIt()
{
behavior.DoIt();
}
public void SetBehavior(ObjectBehavior ob) {
behavior = ob;
}
};
class A : BaseOfA {
public A() {
behavior = new BehaviorA();
}
}
interface ObjectBehavior {
void DoIt();
}
class BehaviorA : ObjectBehavior {
void ObjectBehavior.DoIt() {
Console.WriteLine("Behavior: A");
}
}
class BehaviorB : ObjectBehavior {
void ObjectBehavior.DoIt() {
Console.WriteLine("Behavior: B");
}
}
Now my question is, in this case, how am I going to make it work so that I can assign both BehaviorA and BehaviorB to instance obj1 as long as they implement ObjectBehavior?
You are calling obj.SetFlyBehaviour this method is not defined anywhere. The method you define on BaseOfA is called SetBehaviour. Once that is fixed the code you gave compiles fine for me
Hello friends I face this question in one Interview in statics(Myclass). I have two clases one is M1 and M2.
How can I call m2 in m1 and m1 in m2? and also How to create an Instance of an static?
static void Main(string[] args)
{
//in this Portion How can I call all member if MyClass
}
public static class Myclass
{
public static class M1
{
//Here How can I call m2
}
public class m2
{
//Here How can I call m1
}
}
For static class M1, you need to call using class name.
For non static class M2, you need to create instance.
you can call methods/properties, and not classes.
public static class Myclass
{
public static void Main(String[] args)
{
// call something on m2
var m2 = new Myclass.m2();
m2.A2(); // call m2.A2 method
// call something on m1
Myclass.m1.A1();
}
public static class M1
{
//Here How can I call m2
public static void A1()
{
var m2 = new Myclass.m2(); // create m2instance
m2.A2(); // call m2
}
}
public class m2
{
//Here How can I call m1
public void A2()
{
Myclass.M1.A1(); // called M1.A1
}
}
}
You can not call a class, and as it is static you can not Generate new class of that type but you can call methods and functions of it easily like this:
public static class Myclass
{
public static class M1
{
public static class Method2(){
}
new m2().AMethod();
}
public class m2
{
//Here How can I call m1
public static void AMethod(){
//method
}
}
}
And for main:
static void Main(string[] args)
{
new MyClass.m2().AMethod();
MyClass.M1.Method2();
}
public class A
{
private void MethodA(){}
}
public class B
{
private void MethodB() { }
}
public class C
{
private void MethodC() { }
}
I want to make sure that MethodA can be called only from MethodB. Other method can never call MethodA.
Make MethodA protected and use inheritance like this:
public class A
{
protected void MethodA()
{
}
}
public class B : A
{
private void MethodB()
{
//MethodA is accessible just here
}
}
public class C
{
private void MethodC()
{
//MethodA is not accessible here
}
}
But if you don't want to use inheritance and want all the classes in the same assembly you could only nest class B within class A and keep MethodA private. Like this:
public class A
{
private void MethodA()
{
}
public class B
{
private void MethodB()
{
A a = new A();
a.MethodA();
}
}
}
public class C
{
private void MethodC()
{
//MethodA is not accessible here
}
}
public class D : A
{
private void MethodC()
{
//MethodA is not accessible here
}
}
I note that S.Akbari's answer, though good, does not exactly meet your requirement. You said that you wanted MethodA to be callable only within B, but in their answer, MethodA is callable within A.
The solution to the problem you actually posed is to invert the nesting:
class B
{
private class A
{
public void MethodA() { }
}
}
Now MethodA can only be called from within B.
But the question is bizarre. If you have a method that can only be called from B then why is it not a member of B?
This question already has answers here:
Calling a function from a string in C#
(5 answers)
Closed 1 year ago.
I have created a class library, which has 4 classes each class have 1 method.
First class is the main class for me, in my first class, i have a string called calltoaction, in this string i will be getting the one of below list dynamically
class2.Method2()
class3.Method3()
class4.Method4()
now i want to execute the "class2.method2" from the string "calltoaction".
Say for ex:
class Class1
{
public void method1()
{
string calltoaction = "Class2.Method2()";
}
}
How to execute the "Class2.Method" from the string?
I'm not entirely sure what you are trying to accomplish, but I am sure it can be done in a better way. Basically, if I understand your question correctly, calling this function returns the name of a class and method that you wish to execute.
If that is the case, I would drop the whole "string" thing indefinitely and start looking at delegates.
Consider this:
public class Class2
{
public static void Method2() { }
} // eo class 2
public class Class3
{
public static void Method3() { }
} // eo class 3
public class Class4
{
public static void Method4() { }
} // eo class 4
Now we'd come to our main class
public class MainClass
{
private delegate void MethodDelegate();
private List<MethodDelegate> delegates_ = new List<MethodDelegate>();
// ctor
public MainClass()
{
delegates_.Add(Class2.Method2);
delegates_.Add(Class3.Method3);
delegates_.Add(Class4.Method4);
}
// Call a method
public void Method1()
{
// decide what you want to call:
delegates_[0].Invoke(); // "Class2.Method2"
} // eo Method1
} // eo class Main
Use an Action instead of a string (Assuming you don't need a return value. If you do - use Func):
This is for an idea of how to use it:
public Form1()
{
InitializeComponent();
Action<string> calltoaction;
calltoaction = Doit;
calltoaction("MyText1");
calltoaction = Doit2;
calltoaction("MyText2");
}
void Doit(string s)
{ Text = s; }
void Doit2(string s)
{ textBox1.Text = s; }
I guess a low tech way would be to use a switch statement like so:
using System;
namespace ConsoleApplication24
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Which method would you like to run?");
RunMyMethod(Console.ReadLine());
}
private static void RunMyMethod(string p)
{
switch (p)
{
case "MethodOne();":
MethodOne();
break;
case "MethodTwo();":
MethodTwo();
break;
case "MethodThree();":
MethodThree();
break;
}
}
private static void MethodThree()
{
//Do Stuff
}
private static void MethodTwo()
{
//Do Stuff
}
private static void MethodOne()
{
//Do Stuff
}
}
}