Private in class call parent class method - c#

A newbie question, I got the following C# code where there is a inner-class B that need to call a method on class A.
Please advise how.
class A
{
void MethodA() {
}
class B {
void MethodB {
// Now method B need to call Method A above
}
}
}

Nested types don't automatically have an instance of their parent type; you would need something like:
class B {
private readonly A a;
public B(A a) { this.a = a; }
void MethodB() { a.MethodA(); }
}
and instead of new B(), you would use new B(this).

Make object of A inside class B. And use it inside MethodB.
class B
{
private A objectA;
void MethodB()
{
objectA.MethodA();
}
}
Initialize objectA before using. You can do this in constructor.

Related

Multilevel Inheritance - Invoke Child Level New Method

In the below program, since new keyword is used in the derived class, I am expecting the output "C" but am getting the output as "A". Can some one please explain the reason behind this?
Also, I would like to know how to invoke Foo method declared in the class C.
class A
{
public void Foo()
{
Console.Write("A");
}
}
class B : A
{
new void Foo()
{
Console.Write("B");
}
}
class C : B
{
new void Foo()
{
Console.Write("C");
}
}
public static void Main(string[] args)
{
C c = new C();
c.Foo();
}
C.Foo() has no visibility on the private Foo you new'ed up inside C - it will call the public derived Foo from A.
class C : B
{
public void Test()
{
// this has visibility on the private new'ed up method and can call it
this.Foo();
}
// this is just a private method - it does NOT shadow the public one
// "new" does nothing valuable here
new void Foo()
{
Console.Write("C");
}
}
public static void Main(string[] args)
{
C c = new C();
c.Foo(); // calls the public visible derived A.Foo
c.Test(); // this will call the "new"ed up C.Foo()
}
Output:
AC
You can call private methods only from inside the class they belong to - not from the outside (unless you put the call to the private one inside a public method - see Test).
Most of the time new-ing up a method (to me) is a code smell.
The keyword new in this context prevents inheritance, i.e., it hides the inherited methods instead of providing a new implementation of them. There are only rare situations where you want to do this. Mainly for static members, since those can't be overridden.
The right way to inherit and possibly override methods is to make them virtual:
class A
{
public virtual void Foo()
{
Console.Write("A");
}
}
class B : A
{
public override void Foo()
{
Console.Write("B");
}
}
class C : B
{
public override void Foo()
{
Console.Write("C");
}
}
Now, your test will work as expected. It will even work if you assign the new C to variable typed as A:
A a = new C();
a.Foo(); // Prints: C
That being said, your Foo methods in B and C don't have a public modifier and are thus private. I.e., you can only access A.Foo, which is public, from the outside.

Understand a type as another

Suppose I have a class A and a class B.
class A {
public void foo1(){ print("foo1"); }
public void foo2(){ print("foo2"); }
...
public void fooN(){ print("fooN"); }
}
class B {
public A a;
}
When I call B, I'd like it to behave as if it was an instance of A, so I could do this:
B b = new B();
B.a = new A();
B.foo1();
How can I achieve this behaviour? I tried to add this function in B but it didn't work:
public static implicit operator A(B instance) { return instance.a; }
PS: A cannot be inherited from.
If you want to call B.foo1() change class B to :
class B
{
public void foo1() {
if(a!=null) a.foo1();
}
.........
.........
public A a {get; set;}
}
Any other way you will not be able to compile your code. b.foo1() will give a compiler error. You can use reflection and call something like b.aMethod("foo1") but it is a different story.

C# Restrict which classes can call a method

I want to be able to restrict which classes have access to call a method of another class. I have a the following:
public class A: B
{
private void DoSomething()
{
C.Method1(); // should compile
}
}
public abstract class B
{
}
public class D
{
private void DoSomething()
{
C.Method1(); // shouldn't compile
}
}
public static class C
{
public static void Method1()
{
}
public static void Method2()
{
...
Method1();
...
}
}
All of these classes are in the same assembly, but class B is in a different assembly.
My goal is for class A to be able to call C.Method1, but have class D not able to call C.Method1
I was thinking of making class C a parent class, and have class A inherit class B, but class A already inherits from class B.
Method1 doesn't belong in class A or B.
A practical use for this is when Method1 is a utility method, and should only be called by class A and class C
Without moving methods around, you'd have to make C non-static, make Method1 protected, then have B inherit from C, which would look like:
public class A : B
{
private void DoSomething()
{
C.Method1(); // should compile
}
}
public abstract class B : C
{
}
public class D
{
private void DoSomething()
{
C.Method1(); // shouldn't compile
}
}
public class C
{
protected static void Method1()
{
}
}

How to identify object calls to parent functions

I would like to identify the calls to members of parent class by child class.
Is it possible?
For eg:
public class base1
{
public void getvalue()
{
}
}
public class B:base1
{
}
public class C:base1
{
}
public static void main()
{
B objb=new B();
C objc=new C();
objb.getvalue();//this should display B
objc.getvalue();//this should display C
}
How can we modify getvalue() accordingly?
//Not using Overriding concepts
You can use reflection to do this:
string type = objb.GetType().Name; //will be "B"

Override an overridden method (C#)

I'm trying to override an overridden method (if that makes sense!) in C#.
I have a scenario similar to the below, but when I have a breakpoint in the SampleMethod() in the "C" class it's not being hit, whilst the same breakpoint in the "B" method is being hit.
public class A
{
protected virtual void SampleMethod() {}
}
public class B : A
{
protected override void SampleMethod()
{
base.SampleMethod();
}
}
public class C : B
{
protected override void SampleMethod()
{
base.SampleMethod();
}
}
Thanks in advance!
Edit:
Ok, the context would help:
This is in the context of a composite control so class A inherits from CompositeControl and calls SampleMethod() after overriding the CreateChildControls() method.
Overriding can be performed in a chain as long as you like. The code you have shown is correct.
The only possible explanation for the behaviour you are seeing is that the object to which you are referring is actually of type B. I suggest that you double check this, and if things still don't make sense, post the other appropiate code.
Without seeing the code that calls SampleMethod, my guess would be that you have an object of type B and call SampleMethod on that.
The breakpoint is more than likely not being hit because you actually instantiated an instance of the "B" class.
Method override resolution works based on the actual runtime type of the class whose method should be called. So, if you had the following code:
C c = new C();
c.SampleMethod();
and the following:
C c = new C();
B b = (B)c;
b.SampleMethod();
both the runtime types of the class whose SampleMethod will be called is type B.
That solution works fine; although to actually use it outside the class the method is in, you need to set the access of SampleMethod to public rather than protected in all of the cases it appears, so:
public class A
{
public virtual void SampleMethod()
{
Console.WriteLine("lol");
}
}
public class B : A
{
public override void SampleMethod()
{
base.SampleMethod();
}
}
public class C : B
{
public override void SampleMethod()
{
base.SampleMethod();
}
}
for Overriding more than once in the hierarchy use something like this
// abstract class
abstract class A
{
public abstract void MethodOne();
}
// class B inherits A
class B : A
{
public override void MethodOne()
{
Console.WriteLine("From Class B");
}
}
// class C inherits B
class C : B
{
public override void MethodOne()
{
Console.WriteLine("From Class C");
}
}
// class D inherits C
class D : C
{
public override void MethodOne()
{
Console.WriteLine("From Class D");
}
}
// etc......
// class Main method Class
class MainClass
{
public static void Main()
{
B[] TestArray = new B[3];
B b1 = new B();
C c1 = new C();
D d1 = new D();
TestArray[0] = b1;
TestArray[1] = c1;
TestArray[2] = d1;
for (int i = 0; i < TestArray.Length; i++)
{
TestArray[i].MethodOne();
}
Console.ReadLine();
}
}
I did it in this code in this link
http://www.4shared.com/rar/0SG0Rklxce/OverridingMoeThanOnce.html
Method overriding is OOP feature that allows a child class to provide a specific implementation of a method that is already provided by one of its parent classes.

Categories

Resources