-
OOP Question
Hey all,
I have a OOP question I need some advice on. I have several different classes and I need each different type of class to have access to specific identical functions. Instead of writing the functions in each class, I know that OOP specializes in this sort of thing. I first thought about writing a seperate class with all my functions and inheriting the function class into each class that needs access to these specific functions, but I can't do that since some of the classes already have parent classes and VB only allows one inherit per class. I have been doing some reading and I was wondering if I am correct in saying this is where Interfaces come into play? I have never used a interface and if this is the time to use em, can anyone lead me in the right direction?
Thanks..
Hinder
-
Re: OOP Question
interface may be the way to go.. but a way to tell is to determine what said function does. Interfaces are different than inheritence in that they only tell the class what it needs to have.. but not give it the actual code (like inheritence does)
it may be hard to wrap your mind around it at first.. but think of it like this:
If your class implements an interface, all that means is that your class will contain certain routines that YOU need to provide the code for. IDisposable is a good example... you probably have seen plenty of .net objects with a .dispose method.. well IDisposable is an interface, and any class that implements it will have a .dispose method, but each class has its OWN code for handling the method, as each class generally has different things to dispose of and clean up. Starting to make sense?
So if each of your classes need a similar method, but the actual code in each class will vary, then an interface is what you want... but if the function is identical for each class... then you may just want to make ANOTHER class that holds this function, and have each class create an instance of that class (or make it shared so you can just call it without an instance)
-
Re: OOP Question
Thanks Kleinma,
Since all the code in each function will be identical (Basically just need to structure Datatables at runtime) , Creating a class and making it shared sounds like the way to go.. thanks again..
Hinder
-
Re: OOP Question
The shared routine in a new class would be the most efficient. Of course, if there are no member variables needed for this new class, using a module is an option, as well. I generally use modules for public functions that don't necessarily belong to a single conceptual object.However, since modules are classes in .NET, so this is really just playing with semantics. There is a potential issue with this approach, though.
I want a class to be independent of the context it is in. Therefore, a class needs to be able to function without requiring the existence of any objects external to itself (except the framework or framework components) within the project. When I use a module, those functions are all aware of the existence of any global (or module level) variables, and are free to use them. This would violate the philosophy behind classes, so I use a module....even though it actually is a class.
I haven't worked this through thoroughly, because within a project, it often makes no sense for a class not to require other external classes. You might have a class that updates a database, and pass the database connection (or something) into the class function, but that only works in certain cases.
In your case, having the classes independent of objects external to them puts you in a tricky spot. You would have to include the common class as a member in each of the classes that needed it, which seems a bit redundant, but is the only way to maintain independence of the classes.
One truly odd thought that just occured to me is that in some cases, you could make the other classes members of some larger class that held the functions that the other classes needed, but there are major problems with this in most cases, so I won't go further with it.