I have two classes like Class A and Class B. Class A have some properties, methods and Class B have only the properties. But both Classes have the same set of properties.
My Question is, If I add any new property in Class A, I need to add that in Class B also. If I did not add means, need to show error. How can I achieve this through C#?
You may achieve this by using an Interface and implementing it both in class A and class B. In the interface, define the property that is required in class A and B:
public interface ICommonProperty
{
string MyProperty{ get; set; }
}
Or you can use keyword abstract to create a class in common for A and B.
abstract class BaseClass // Abstract class
{
public int X {get;set;} // property in common for 2 class
}
class A : BaseClass
{
}
class B : BaseClass
{
public int Y {get;set;} // other property of B
}
You can go with the abstract class. The abstract keyword enables you to create classes and class members that are incomplete and must be implemented in a derived class.
Here is a simple example related to your question However you can understand and learn about Abstract classes here : Abstract Class
using System;
public class Program
{
public static void Main()
{
A objA = new A();
objA.printA();
B objB = new B();
objB.printB();
}
}
abstract class Parent{
public int a = 5;
}
class A : Parent
{
public void printA()
{
Console.WriteLine("In class A, value is "+a);
}
}
class B : Parent
{
public void printB()
{
Console.WriteLine("In class B, value is "+a);
}
}
Output of the above program is:
In class A, vlaue is 5
In class B, vlaue is 5
Hope this helps you.
Related
My question may be complicated , but I will try to explain it . Suppose that I have a Interface called IA and class called A ,
Interface IA:
public interface IA
{
public void Test();
}
Class A
public class A : IA
{
public string Name { get; set; }
public A()
{
}
}
When I'm trying to use Dependency injection :
Main Class :
public class MainClass
{
IA objectA = new A();
objectA.Name = "test A"; // Not working , I didn't get this Name and I can't find it .
}
IA does not contains a definition for Name ...
What's the reason of this problem?
I have created a Gist https://gist.github.com/ian-bowyer/f1a57a5fc8e4df41cc63d9816276d708 with the code on.
I changed IA to IApple and A to Apple.
The reason is that you have created an variable of type IApple (which then does not have the Name Property in it)
IA objectA = new A();
This says create me a variable called objectA of this interface IA using the constructor A(). The objectA will have the shape of the interface (IA) and not your A (object).
If you are wanting to have Name using the code then perhaps add it to the interface to make it available.
Why do you create a Instance of interface class?
Try to create Instance of Class A like:
public class MainClass
{
A objectA = new A();
objectA.Name = "test A";
}
So you can get your Test() Method and Name Property.
Or you put Name Property in Interface Class IA and all classes inherited from this Interface have Name Property.
Run it here: http://rextester.com/ZMLMB2576
public interface IClass {
int number {get;}
}
public abstract class BaseClass : IClass {
public BaseClass(int n){
number = n+100;
}
public int number { get;set;}
}
public class DerivedClass : BaseClass {
public DerivedClass(int n) : base(n) {
number = n;
}
public int number { get;set;}
}
public class Program
{
public static void Main(string[] args)
{
var foobar = new DerivedClass(1);
Console.WriteLine(GetNumber(foobar)); // 101
}
public static int GetNumber(IClass foo){
return foo.number;
}
}
Why does the function GetNumber not use the most derived class and instead only treats the passed object as BaseClass ?
If it was treating the passed object (foo) as DerivedClass then I suspect the base constructor to run first and then DerivedClass constructor, overriding 101 with 1
Shadowing (number in derived class hides one from base) and the fact that interface is implemented by base class leads to this behavior. Mapping interface methods defined by base class and IClass.number is mapped to BaseClass.number which is completely different from DerivedClass.number.
Fixes:
make property number to be virtual in base class (more standard approach)
derive both classes from the interface so interface actually picks up implementation from derived class (will confuse readers and everyone will try to remove interface from derived thus breaking it again)
class Derived : BaseClass, IClass { ...
Notes:
it is better to explicitly specify new public int number {get;set;} when you shadow members of base class.
C# is not Java and methods/fields are not virtual by default.
Difference between shadowing and overriding in C#?
I am using C#
Scenario: Same project
FolderA - ClassA
FolderB - ClassB
I have a method in ClassB that needs the methods from ClassA.
How can I do that?
The folder part does not matter, but one common way to expose methods to other classes is to make them public. You can either use static or instance methods.
Ex
public class A
{
public void SomeMethod(){}
public static void SomeStaticMethod(){}
}
public class B
{
public B()
{
A a = new A();
a.SomeMethod();
A.SomeStaticMethod();
}
}
Another alternative is to use inheritance and let class A inherit from class B
public class A : B
{
public A()
{
//you can now call the methods defined in B
base.SomeMethod();
}
}
Above is an example of how to do it.
Let say i have a base class
class baseClass
{ }
And another class that inherit from that baseClass
class foo : baseClass
{ }
I tryed to cast it back directly (baseClass)foo but the compiler say it cannot be done.
Is there a way from foo to get only the baseClass?
foo is a baseClass you don't need any casting. And your code should work without any problems:
var foo = new foo();
baseClass x = (baseClass) foo;
If you derive a class A from class B you can always refer to A as if it were B. Of course this is not true in the revese case. In general you can always refer down the chain of inherited classes.
If the compiler complain on such a thing, it could just mean you have several baseClass defined in several Namespace and you're actually not referencing the right baseClass.
Check your Namespaces it should solve your bug.
Here a working example with different Namespaces
namespace BaseNameSpace
{
public class BaseClass
{
public string Name { get; set; }
}
}
namespace TestNameSpace.Class
{
public class TestClass : BaseClass
{
public string Address { get; set; }
}
}
Use :
TestClass test1 = new TestClass();
BaseClass b = test1;
Ensure that there is the correct using :
using BaseNameSpace;
using TestNameSpace.Class;
Consider the following example:
interface IPropertyCollection
{
public MethodWrapper GetPropertySetterByName(string name);
//<<-- I want the implementation from A and B merged into here somehow
}
class A : IPropertyCollection
{
static PropertyMap properties = new PropertyMap(typeof(A));
public MethodWrapper GetPropertySetterByName(string name)
{
return properties.SomeFunc(name);
}
}
class B : IPropertyCollection
{
static PropertyMap properties = new PropertyMap(typeof(B));
public MethodWrapper GetPropertySetterByName(string name)
{
return properties.SomeFunc(name);
}
}
I want to have a static member in each class keeping track of things in that class only and i want it to behave exactly the same for every class, but with different content. Each static member should only keep track of one single class. I want to be able to get access to the classes' static member by just having an instance of any IPropertyCollection.
Something like this:
IPropertyCollection a = new A();
IPropertyCollection b = new B();
a.GetPropertySetterByName("asdfsj"); //Should end up in static A.properties
b.GetPropertySetterByName("asdfsj"); //Should end up in static B.properties
Now this will work with my example code but i don't want to repeat all those lines inside A and B and 50 other classes.
Use a (curiously recurring) generic common base class:
abstract class Base<T> : IPropertyCollection where T : Base<T> {
static PropertyMap properties = new PropertyMap(typeof(T));
public MethodWrapper GetPropertySetterByName(string name) {
return properties.SomeFunc(name);
}
}
class A : Base<A> { }
class B : Base<B> { }
Since the base class is generic, a different "version" of its static members will be "generated" for each different type parameter.
Just be careful with evil dogs :-)
class Evil : Base<A> { } // will share A's static member...
Static members can't be inherited. They belong to the class they are declared on.