Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a class with some functions. The functions can be categorize in "NameSpace".
I search a way to organize each function into somekind of namespace into my class.
It was suggested to me to change my approach. Using Interface instead of inheritance.
Here some sample for be understand
public class Class1
{
public string A = "A";
public string B = "B";
public int one = 1;
public int two = 2;
public Printer globalPrinter;
}
public class Class10 : Class1
{
public class Letter
{
public void FunctionA()
{
globalPrinter.print(A);
}
public void FunctionB()
{
globalPrinter.print(B);
}
}
public class Number
{
public void Function1()
{
globalPrinter.print(one);
}
public void Function2()
{
globalPrinter.print(two);
}
}
}
public class ClassX : Class10
{
public ClassX()
{
Letter.FunctionA();
Letter.FunctionB();
Number.Function1();
Number.Function2();
}
}
var c10 = new ClassX();
This code doesn't work. You could look, at the end of the sample, you'll see
Letter.FunctionA();
Letter.FunctionB();
Number.Function1();
Number.Function2();
I could simply put FunctionA and FunctionB inside Class10. But if there's 100 functions. It could be more interesting to split function into Categories.
Let me see your approach of the problem.
I added some item for helping the categorization.
Answer Full Code
public class Class1
{
public string A = "A";
public string B = "B";
public int one = 1;
public int two = 2;
public void globalPrinter(object t)
{
}
}
public class Class10 : Class1
{
public class Letter
{
readonly Class10 parent;
public Letter(Class10 parent)
{
this.parent = parent;
}
public void FunctionA()
{
parent.globalPrinter(parent.A);
}
public void FunctionB()
{
parent.globalPrinter(parent.B);
}
}
public class Number
{
readonly Class10 parent;
public Number(Class10 parent)
{
this.parent = parent;
}
public void Function1()
{
parent.globalPrinter(parent.one);
}
public void Function2()
{
parent.globalPrinter(parent.two);
}
}
}
public class ClassX : Class10
{
public ClassX()
{
new Letter(this).FunctionA();
new Letter(this).FunctionB();
new Number(this).Function1();
new Number(this).Function2();
}
}
ClassX c10 = new ClassX();
The problem is that a sub class like Class10.Letter is not part of the inheritance hierarchy that its parent class Class10 is part of.
So, Class10.Letter does not inherit from Class1, only Class10 inherits from Class1, so Class10.Letter needs an instance of Class1 or Class10 so it can access globalPrinter
public class Class10: Class1 {
public class Letter {
readonly Class10 parent;
public Letter(Class10 parent) {
this.parent = parent;
}
public void FunctionA() {
parent.globalPrinter.print(parent.A);
}
}
}
Also ClassX inherits from Class10 so it has access to Class10 functions, but it does not inherit from Class10.Letter, soClassX needs an instance of Letter if it is calling non-static functions:
public ClassX() {
new Letter(this).FunctionA();
...
Related
Here is an example of what I am looking to do.
public class ParentA {}
public class ChildA : ParentA
{
public string x;
}
public class A
{
public virtual void DoSomething(Parent a)
{
// perform something
}
}
public class B : A
{
public Override void DoSomething(Child a)
{
// perform something slightly different using both strings
a.x = "something";
}
}
but turn out I got an error with "No Suitable Method found to Override".
So I want to override DoSomething from class A and pass a different set of child class parameter. Is this possible?
When you override something, the signature of the method has to be the same.
So in your case, you can do something like this
public class Parent { }
public class Child : Parent
{
public string x;
}
public class A
{
public virtual void DoSomething(Parent a)
{
// perform something
}
}
public class B : A
{
public override void DoSomething(Parent a)
{
if (a is Child child)
{
// perform something slightly different using both strings
child.x = "something";
}
}
}
I'm not 100% sure exactly what you're trying to accomplish, however generics may help you:
public class Parent { }
public class Child : Parent
{
public string x;
}
public class A<T> : Parent where T : Parent
{
public virtual void DoSomething(T a)
{
}
}
public class B : A<Child> // Child could also be Parent here
{
public override void DoSomething(Child a)
{
a.x = "test";
}
}
I have a abstract base class, starting a timer which is common to all derived class,
public abstract class BaseClass
{
public virtual void Start() { _timer.Start(); }
}
Now I need to load different JSON configuration files for each derived class and create the file,
public class DerivedClass1 : BaseClass
{
private readonly List<config> configs = new List<config>();
public DerivedClass1()
{
configs = JsonSettings.GetConfigurations(#"./Configurations/1.json");
}
public override void Start()
{
base.Start();
foreach (var configuration in configs)
{
JsonSettings.CreateConfigFile(configuration);
}
}
}
public class DerivedClass2 : BaseClass
{
private readonly List<config> configs = new List<config>();
public DerivedClass2()
{
configs = JsonSettings.GetConfigurations(#"./Configurations/2.json");
}
public override void Start()
{
base.Start();
foreach (var configuration in configs)
{
JsonSettings.CreateConfigFile(configuration);
}
}
}
As I see there are lots of codes are duplicated in various derived class.
Can I move these piece of code as well as abstract base class or is there another way?
I think you could simplify your code to this:
public abstract class BaseClass
{
protected virtual List<config> configs { get; set; } = new List<config>();
public virtual void Start()
{
_timer.Start();
foreach (var configuration in configs)
{
JsonSettings.CreateConfigFile(configuration);
}
}
}
public class DerivedClass1 : BaseClass
{
public DerivedClass1()
{
configs = JsonSettings.GetConfigurations(#"./Configurations/1.json");
}
}
public class DerivedClass2 : BaseClass
{
public DerivedClass2()
{
configs = JsonSettings.GetConfigurations(#"./Configurations/2.json");
}
}
public interface BaseClass
{
void Start();
}
public interface IBaseClassUtil
{
void Start();
void setConfigs(List<config> configs);
}
public class BaseClassUtil : IBaseClassUtil
{
System.Timers.Timer _timer;
public List<config> _configs { get; set; } = new List<config>();
public void Start()
{
_timer.Start();
foreach (var configuration in _configs)
{
JsonSettings.CreateConfigFile(configuration);
}
}
public void setConfigs(List<config> configs)
{
_configs = configs;
}
}
public class DerivedClass1 : BaseClass
{
private IBaseClassUtil _baseUtility;
public DerivedClass1(IBaseClassUtil baseUtility)
{
_baseUtility = baseUtility;
_baseUtility.setConfigs( JsonSettings.GetConfigurations(#"./Configurations/1.json"));
}
public void Start()
{
_baseUtility.Start();
}
}
public class DerivedClass2 : BaseClass
{
private IBaseClassUtil _baseUtility;
public DerivedClass2(IBaseClassUtil baseUtility)
{
_baseUtility = baseUtility;
_baseUtility.setConfigs(JsonSettings.GetConfigurations(#"./Configurations/2.json"));
}
public void Start()
{
_baseUtility.Start();
}
}
This might be oveer engineered. Or might not suit ur current requirement.
Advantages would be
In future if you want u want to have different implementation for IBaseClassUtil it will be easier
And huge advantage would be this code is testable
If the classes differ by nothing but the configuration path, then you can have only one derived class that takes the path as a parameter in its ctor.
public DerivedClass(string configurationPath)
{
configs = JsonSettings.GetConfigurations(configurationPath);
}
Put please note that a decision on including inheritance in your architecture is not about code duplication, and by not giving us any information on the functions or even names of the classes (BaseClass and DerivedClass mean nothing. What do they represent? What's their function? Why are they related?) you give us no way of really helping you with your design.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Lets say that i have a "Timer" class with a method that every 1 second is called, and it calls another method in the class "Gear":
public class Timer
{
public void OnTick()
{
Gear.Update();
}
}
public class Gear
{
public static void Update() { }
}
This kinda works, but it's only called on the base class.
The method "Update" should be called in all the childrens of "Gear":
e.g:
public class AnotherClass : Gear
{
public override void Update() { // do stuff }
}
public class YetAnotherClass : Gear
{
public override void Update() { // do stuff }
}
public class AndAnotherClass : Gear
{
public override void Update() { // do stuff }
}
How can i do this?
In order for the code to work the way you want, you'd need to do something like this (I would worry about a memory leak):
public abstract class Gear
{
readonly static List<Gear> gears = new List<Gear>();
public Gear()
{
gears.Add(this);
}
public static void Update()
{
foreach (var gear in gears)
gear._Update();
}
protected abstract void _Update();
}
public sealed class Gear1 : Gear
{
protected override void _Update()
{
//do stuff
}
}
public sealed class Gear2 : Gear
{
protected override void _Update()
{
//do stuff
}
}
public sealed class Gear3 : Gear
{
protected override void _Update()
{
//do stuff
}
}
public static void Main(string[] args)
{
var timer =
new Timer(o => Gear.Update(), null, 0, SOME_INTERVAL);
}
However, you might be better off by defining the base case thusly:
public abstract class Gear
{
public abstract void Update();
}
And then define a collection class:
public sealed class GearCollection : List<Gear>
{
public void Update()
{
foreach (var gear in this)
gear.Update();
}
}
And then
public static void Main(string[] args)
{
var gears = new GearCollection();
//add gear instancs to gears
var timer = new Timer(o => gears.Update(), null, 0, SOME_INTERVAL);
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I got a C# project where I need to have a base class and two sub classes and I want the sub classes to be private to the base classes. This is how it looks like:
public class MyBaseClass
{
public void doMyWork()
{
doSubClass1();
doSubClass2();
}
}
private class MySubClass1
{
public void doSubClass1()
{
//Do stuff
}
}
private class MySubClass2
{
public void doSubClass2()
{
//Do stuff
}
}
How i can i get this to work?
Does it hurt if the classes themselves are not-so-private but still wrapped in your base & unaccessible outside?
public class MyBaseClass
{
private readonly MySubClass1 _sub1 = new MySubClass1();
private readonly MySubClass2 _sub2 = new MySubClass2();
public void DoMyWork()
{
_sub1.DoSubClass1();
_sub2.DoSubClass2();
}
}
public class MySubClass1
{
public void DoSubClass1() { /* Do stuff */ }
}
public class MySubClass2
{
public void DoSubClass2() { /* Do stuff */ }
}
If this is not good enough and you need those in separate files, as you stated, then you can use partial classes, like so.
Class1.cs file
public partial class MyBaseClass
{
public void DoMyWork()
{
(new MySubClass1()).DoSubClass1();
(new MySubClass2()).DoSubClass2();
}
}
Class2.cs file
public partial class MyBaseClass
{
private class MySubClass1
{
public void DoSubClass1() { /* Do stuff */ }
}
}
Class3.cs file
public partial class MyBaseClass
{
private class MySubClass2
{
public void DoSubClass2() { /* Do stuff */ }
}
}
But still it's not quite clear what you are trying to achieve here.
If you put your subclasses inside of the base class, the base class can access them. To use your example, it would be like this:
public class MyBaseClass
{
public void doMyWork()
{
new MySubClass1().doSubClass1();
new MySubClass2().doSubClass2();
}
private class MySubClass1
{
public void doSubClass1()
{
//Do stuff
}
}
private class MySubClass2
{
public void doSubClass2()
{
//Do stuff
}
}
}
Note that all I did was move the last } to the bottom.
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();
}
}