How to identify object calls to parent functions - c#

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"

Related

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.

Polymophism in C#, how to turn parent object into a child object in a method?

public class Program
{
public static void Main()
{
var type = new C().getType();
type.Foo();
}
public class A
{
public void Stop()
{
// do something
}
}
public class B : A
{
public void Foo()
{
Console.WriteLine("Foo");
}
}
public class D : A
{
public void Bar()
{
Console.WriteLine("Bar");
}
}
public class C
{
public A getType()
{
if (some condition)
return new B();
if (some condition)
return new D();
return A();
}
}
}
-EDIT-
I updated the code, so now we have two child classes B and D, both have different methods inside them, however since they inherit A, they at least both have access to Stop() method. The problem with making an abstract method Foo() and Bar() inside the parent class A is that, B should not have access to Bar() and D should not have access to Foo(), but by making an abstract method they will need to implement them both.
I know that I can check the type of the returned object inside Main() method and then cast it to that type. But this won't be convenient as in the future I will have more descendants of A.
Is it in my situation ok, to use dynamic? Because that would solve the problem and would be very convenient.
public class C
{
public dynamic getType()
{
if (some condition)
return new B();
if (some condition)
return new D();
return A();
}
}
You almost had it right:
public class Program
{
public static void Main()
{
var type = new C().getType();
type.Foo();
}
public abstract class A
{
public abstract void Foo();
}
public class B : A
{
public override void Foo()
{
Console.WriteLine("Foo");
}
}
public class C
{
public A getType()
{
return new B();
}
}
}
You need a Foo method in A that B can override. I have made A.Foo abstract, which means we do not have to define a base implementation of A.Foo. The A class is declared abstract to prevent someone trying to create an instance of it.
If you want derived classes to inherit a base implementation of Foo from A, then declare Foo as virtual in A. So in the following example, class B overrides the base Foo, whereas class C inherits the base Foo:
public class Program
{
public static void Main()
{
A a1 = new B();
a1.Foo(); // Outputs "B.Foo".
A a2 = new C();
a2.Foo(); // Outputs "A.Foo".
}
public abstract class A
{
public virtual void Foo()
{
Console.WriteLine("A.Foo");
}
}
public class B : A
{
public override void Foo()
{
Console.WriteLine("B.Foo");
}
}
public class C : A
{
}
}
You are confusing the type of a variable and the type of the runtime object it will be pointing to (given its a reference type, with value types, there is no such distinction).
When you declare a variable, you are defining the type of the variable:
Foo a = ... //a is of type Foo at compile time.
What you assign to that variable doesn't matter at all (unless you are using implicitly typed variables with var)
But given the following code:
object o = new Foo();
Now you can see the diferrence; the variable o is of type object but the runtime object it will be pointing to is of type string.
In your question you are asking why you cant do the following:
var stringLength = o.Length; //o is a string isn't it?
Why? Isn't o really a string. Yes, but that information is only availabe at runtime, the compiler knows nothing of what will happen at runtime, it only knows that o is of type object and object has no Length property. The only thing the compiler will make sure of is that the types are compatible; it will let you assign a Giraffe to an Animal typed variable but it won't let you assing a Car.
If you've got this clear, then you'll understando why this doesn't make sense:
But I did explicitly return B inside getType() method, why does it not have access to Foo()?
Because the method getType return type is A not B, not C and not D.

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()
{
}
}

Private in class call parent class method

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.

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