[C#.NET] Inheritance - can i do this?

Soldato
Joined
12 Jun 2005
Posts
5,361
Hi there,

I know what inheritance is, I just don't really know when to apply it and how to use it properly, but I am pretty sure I should be using it for my current project, I just don't know how to go about doing it.

In Sudo code here is what my main class does (it does more stuff - but heres the important bit):

Code:
On Class Load -> Create [Class A] or [Class B]

Class A and Class B have the same public variables/events/methods, but have different logic for how they are raised/what they do and have different private variables/methods

Is there something I can do so that when either Class A or Class B are created (only one is used at one time), they are assigned to a variable/reference and i can use that variable to call the methods regardless of which class is referenced, because at the moment, for ever method i have in my main class, I have to do an if statement to check which class is created.

Hope this makes sense.

Thanks.
 
Last edited:
I'm pretty sure I have to do the second thing on your list.

Heres an example of what I am currently doing:

Note that I have two variables to reference the two different classes and when I need to check which class is being used, I just check if refA == null and if so, then I use the refB variable. What I want, is just 1 variable to reference both classes.

Code:
public class MasterClass
{
   ClassA refA;
   ClassB refB;

   public MasterClass(int WhichClass)
   {
      if (WhichClass > 0)
      {
         refA = new ClassA();
      }
      else
      {
         refB = new ClassB();
      }
   }
}


public class ClassA
{
   public ClassA(Control ctrl)
   {
      //...logic...
   }

   public void Method1(string someStr)
   {
      //...logic...
   }

   private void SomeOtherMethod(int Number)
   {
      //...logic...
   }
}

public class ClassB
{
   public ClassB(Control ctrl)
   {
      //...logic...
   }

   public void Method1(string someStr)
   {
      //...logic...
   }

   private void AnotherMethod()
   {
      //...logic...
   }
}

EDIT:

Now definitely think its an interface I need, thanks (did some googling)





EDIT AGAIN:

But its also got me thinking, although it might make the code a bit messy, it might be best to put them in one big class rather than two?
 
Last edited:
Think very careful about your class structure - I don't see how you can jump from 2 classes to one... there was obviously enough differences in the first place. As Haircut has said, a factory might be the right idea, but either way - both will need to inherit from an interface (or abstract class, depending on whether you have any code repetition).

It sounds like you are very new to coding AND C# - so get your class structure down on paper before coding it, else you are going to have to recode it many times.

I have thought about the structure, it could make sense in one large class which is overloaded (think thats the right term) to choose the type of class it is, but two me, it makes sense to have them in two difference classes as they are two separate entities. I think the interface is the best way to go about this.

lol....I wouldn't say I was new to coding or c#, but the way i have learnt it (self-taught) is in totally the wrong order tbh. I am at the point where i would need to right the structure of a class of this size as i can see it in my head........but i've always tried to accomplish things with the knowledge i already have, rather than the correct, most efficient way of doing it.

Thanks for your help guys.


EDIT:

When i have actually finished what I am doing, I will post up the project for people to have a look and critique.
 
Last edited:
Ok - confused now.

I have done it via an interface, but with the interface I can't have fields which is annoying as it means i can't have eventhandlers.

------

So should i be using this Factory Pattern thing? - because, from what i gather from the link, it sure as hell doesn't look like what i want as it looks like its just extracting the common methods to another class. In my project, the method names are the same and they have the same parameters/return the same things, but the logic in them is different.
 
Last edited:
Well from what i can see there, the factory is essentially useless as I can do that in a simple switch statement? The example in the link shows what the factory might be more worth while for, but for my project I don't think i need this:

Code:
IDoStuff classVariable = factory.GetClass("A");

as I could just have the "GetClass" logic in the same place where you assign to "classVariable"





-----------------------

EDIT: Been reading up on abstract classes and it looks like that might provide the solution i need as I can have EventHandlers in it.
 
Last edited:
Right ok, there is a lot of terminology there which i don't particularly understand, but I am going to have a stab at explaining what it makes me think, just tell me if i'm wrong or right.

That's the point though - if you had a switch statement in the client it introduces a dependency on the explicit concrete classes, ClassA and ClassB.

Ok, is this something to do with the classes being public/private? And what do you mean by client? Remember all this code and the classes are in one user control.

With the factory you only have a dependency on the interface and not the implementation.
If you need to introduce a ClassC at some point you will need to modify you client rather than simply having the factory encapsulate the logic of creating the concrete class and leaving it up to that object.

Ok, what I think this means is, I won't have to edit any references to the factory, I will only have to edit the factory when i add classes. But surely this can be done with a simple function, rather than having to declare a factory?

If you're 100% sure you will only ever have the two classes then by all means don't have the factory, but it's a good design principle to be aware of.

No there might be more than two classes, but as said above, surely what is done with that factory instance can be done in a function?


----

By the way, I am sorry if it seems like i am challenging anyone, I know I am far inferior when it comes to this, I am just trying to understand it and really do appreciate all the help :)
 
Last edited:
Back
Top Bottom