How to Access method in Parent from Child Class - c#

I have one method which is in child class and I want to fetch that in Parent with the help of this.
public class Class1
{
private class Class2
{
public void Add(int a, int b) // Method in Class 2
{
this.Add(a, b);
}
}
public Class1() // constructor of Class 1
{
// Get this Add method by This.Add ??
// Not able to fetch the Add method here.
}
}

you have declared the method but it is in class2. that means you need to create an instance of class2 in order to use the method
public class Class1
{
private class Class2
{
public void Add(int a, int b) // Method in Class 2
{
this.Add(a, b);
}
}
public Class1() // constructor of Class 1
{
class2 cs = new class2();
cs.Add(4,5);
}
}

You would either have to create an instance of Class2 in the constructor of Class1 and use an instance method, or change the method Add to static in Class2
Static version
Something like
public class Class1
{
private class Class2
{
public static void Add(int a, int b)
{
}
}
public Class1()
{
Class2.Add(1,2);
}
}
Instance version
Something like
public class Class1
{
private class Class2
{
public void Add(int a, int b)
{
}
}
public Class1()
{
new Class2().Add(1,2);
}
}
Maybe have a look at static (C# Reference)

public class Class1
{
private class Class2
{
public void Add(int a, int b) // Method in Class 2
{
this.Add(a, b);
}
}
public Class1() // constructor of Class 1
{
Class2 newclass2 = new Class2();
newclass2.Add(1, 2);
// Get this Add method by This.Add ??
// Not able to fetch the Add method here.
}
}

You need to create instance for Class2
public class Class1
{
private class Class2
{
public Class2() // constructor of Class2
{
}
public void Add(int a, int b) // Method in Class2
{
this.Add(a, b);
}
}
public Class1() // constructor of Class1
{
Class2 cs2 = new Class2();
cs2.Add(4,5);
}
}

Related

Two projects, a derived class should be able to access one method but not the other one

I was asked this question in interview, but couldn't answer it.
There are two projects: P1 and P2
P1 has a class A with methods M1 and M2
P2 has a class B and class C, both of these classes (inherit or reference)* from class A in Project P1
Class B should be able to access method M1 but not M2
Class C should be able to access method M2 but not M1
*I don't remember if he said inherit or said reference
How can this be done?
You want to use interfaces to extract methods from class.
P1:
public interface IM1
{
void M1();
}
public interface IM2
{
void M2();
}
// Not public
internal class A : IM1, IM2
{
public void M1() {}
public void M2() {}
}
P2:
public class B
{
private readonly IM1 _m1;
}
public class C
{
private readonly IM2 _m2;
}
You can implement some like: https://dotnetfiddle.net/0mYxdU
public interface InterfaceCommon
{
void Function();
}
public class Implementacion1 : InterfaceCommon
{
public void Function()
{
Console.WriteLine("I am Method1");
}
}
public class Implementacion2 : InterfaceCommon
{
public void Function()
{
Console.WriteLine("I am Method2");
}
}
public abstract class A
{
private readonly InterfaceCommon interfaceCommon;
protected A(InterfaceCommon interfaceCommon)
{
this.interfaceCommon = interfaceCommon;
}
public void CallFunction()
{
interfaceCommon.Function();
}
}
public class B : A
{
public B() : base(new Implementacion1()) { }
}
public class C : A
{
public C() : base(new Implementacion2()) { }
}
public class Program
{
public static void Main(string[] args)
{
var b = new B();
b.CallFunction();
var c = new C();
c.CallFunction();
Console.ReadLine();
}
}

Mark a method for static use

How can I mark a method to use only if the class object was initialized as static?
For example:
public class A
{
public A()
{
}
public void DoIt()
{
{
public void DoItStatic()
{
}
}
public class B
{
private A _aa = new A();
private static A _aaStatic = new A();
public B()
{
}
public void SomeMethod()
{
_aa.DoItStatic(); //Generate Error for that.
_aaStatic.DoItStatic(); //it's fine
{
}
So, if someone tries to use _aa.DoItStatic(), where _aa is not initialized as static, we generate an error.
Is this possible?
Thanks!

Giving object in dll a click method from another project

is it possible to adding a click method to a class in dll from another project?
I want to create a class (Class1) in a class library and build a dll from it.
I will use that class in a project with references the dll.
This is my class (Class1)
public class Class1
{
public ImageMap map = null;
public Class1(Form f)
{
map = new ImageMap();
map.RegionClick += f.RegionMap_Clicked;
}
}
and this is my form (Form1) in another project.
public partial class Form1 : Form
{
Class1 c = null;
public Form1()
{
InitializeComponent();
c = new Class1(this);
}
void RegionMap_Clicked(int index, string key)
{
MessageBox.Show(key);
}
}
This is my first time asking here. So, sorry if my english is bad.
Class1 can be made independent from Form1:
public class Class1
{
public ImageMap Map = null;
public Class1()
{
this.Map = new ImageMap();
}
}
And Form1 uses Class1 however it likes:
public partial class Form1 : Form
{
private Class1 c = null;
public Form1()
{
InitializeComponent();
this.c = new Class1();
this.c.Map.RegionClick += this.RegionMap_Clicked;
}
private void RegionMap_Clicked(int index, string key)
{
MessageBox.Show(key);
}
}
Thus only the Form1 project needs a reference to the Class1 project.
Yes it's possible, don't forget to make your handler public:
public void RegionMap_Clicked(int index, string key)
{
MessageBox.Show(key);
}
You should do this
public class Class1
{
public ImageMap map = null;
public Class1(Form1 f)
{
map = new ImageMap();
map.RegionClick += f.RegionMap_Clicked;
}
}
then
public partial class Form1 : Form
{
Class1 c = null;
public Form1()
{
InitializeComponent();
c = new Class1(this);
}
public void RegionMap_Clicked(int index, string key)
{
MessageBox.Show(key);
}
}
and of couse you shold add using for this assembly.
I think now it will be working well

Can I inherit from an instantiated class?

I'm trying to inherit from an instantiated class. Why is the value of Inherited, in the code below, a null value? Is there a way to do this correctly?
namespace Sample {
public class Class1 {
static void Main() {
Class2 SecondClass = new Class2();
SecondClass.StartSomething("hello world");
}
}
public class Class2 {
public string Inherited;
public void StartSomething(string value) {
Inherited = value;
InheritSomething();
}
public void InheritSomething() {
Class3 ThirdClass = new Class3();
ThirdClass.DoSomething();
}
}
public class Class3 : Class2 {
public void DoSomething() {
Console.WriteLine(Inherited);//when complied Inherited is null
Console.ReadLine();
}
}
}
Inheriting occurs at compile time. (Therefore 'Inherited' does not have a value yet)
Values are assigned at run-time.
Inheriting from a class does not inherit the INSTANCE of that class at instantiation of the inherited class. Instead, you'd need to pass along the instance of that class. One option is to inject it into the constructor of class 3
public class Class1
{
static void Main()
{
Class2 SecondClass = new Class2();
SecondClass.StartSomething("hello world");
}
}
public class Class2
{
public string Inherited;
public void StartSomething(string value)
{
Inherited = value;
InheritSomething();
}
public void InheritSomething()
{
Class3 thirdClass = new Class3(this);
thirdClass.DoSomething();
}
}
public class Class3 : Class2
{
private Class2 _class2;
public Class3(Class2 class2)
{
_class2 = class2;
}
public void DoSomething()
{
Console.WriteLine(_class2.Inherited);
Console.ReadLine();
}
}

adding methods from class A to a delegate of class B and calling that delegate from class A in C#

How can I add a method from class A to a delegate of class B without knowing in advance which method I will be adding and what class A is? And then call that delegate from class A?
class Class {
public string someProperty;
public delegate void myDelegate(Class obj);
myDelegate handler = new myDelegate(mainClassMethod); //here is the problem..
public void someMethod() {
handler();
}
}
class MainClass {
public static void Main() {
Class classObj = new Class();
classObj.someProperty = "hello";
public void mainClassMethod(Class obj) {
System.Console.WriteLine(obj.someProperty);
}
classObj.someMethod();
}
}
Should I use something other than delegates for this? By the way I am doing this in C#!
make mainClassMethod static and access it via class name MainClass. Also you cant declare nested functions as class members, you need to declare mainClassMethod separately.
class MainClass {
public static void Main()
{
Class classObj = new Class();
classObj.someProperty = "hello";
classObj.someMethod();
}
public static void mainClassMethod(Class obj)
{
System.Console.WriteLine(obj.someProperty);
}
}
Also you declared delegate void myDelegate(Class obj); so you need to pass instance of a Class as a parameter. In my example I pass object found by this reference, which is an object that you call someMethod at.
Now you can write:
class Class {
public string someProperty;
public delegate void myDelegate(Class obj);
myDelegate handler = new myDelegate(MainClass.mainClassMethod); //no error
public void someMethod()
{
handler(this);
}
}

Categories