Best Practice ?

  • Thread starter Thread starter Marten Van Keer
  • Start date Start date
M

Marten Van Keer

Hello;

Given a class A. class A has a function:

calculate()

Class A can calls another Class: B

The problem is: Class B also wants to use the calculate() function. I do
not think it is a good way to duplicate the calculate() function in both
classes.

How do I solve such a problem ?

Thanks in advance!
M.
 
* "Marten Van Keer said:
Given a class A. class A has a function:

calculate()

Class A can calls another Class: B

The problem is: Class B also wants to use the calculate() function. I do
not think it is a good way to duplicate the calculate() function in both
classes.

How do I solve such a problem ?

Maybe creating a base class which provides the 'Calculate' function and
inheriting ('Inherits') from this class will help.
 
If calculate doesn't really require classA to be instantiated, you can use a
shared function

publc classA
public shared function calculate() as integer
....
end function
end class

<from classB>
classA.calculate()
 
Hi Marten,

If it is such an important function than in my opinion of course a class by
itself.

Cor
 
Marten,
In addition to the other's comments.

Can you provide more details on what you are attempting to accomplish?
I do
not think it is a good way to duplicate the calculate() function in both
classes.
You are correct is not good to copy the code for Calculate into both A & B.
However both can have a method called Calculate that performs a different
calculation based on being either an A or a B object.

In addition to the others comments about Shared & Inherits: If a B object
needs to call the Calculate method of an A object, then B needs either a
class variable of type A, or B's method needs an A object as a parameter.

Again can you provide more details, as I am not following what you want.
Maybe include 10 to 15 lines of code to show what you have now, that you are
trying to avoid.

Hope this helps
Jay
 
Hi Jay B,

I seldom start a discussion with you and this one is very slippery ground
because I was you did a lot of investigations in the present time.

But with this one I had to think about Toms message to me about
encapsulation.

And this looks me someting for it.

My idea is, that when you have calculations, let say incometax for a
country, it has to be in one class and not a part of an other class.

And especialy not as part form a class (not a supplier) which has a function
in it that can be used in other programs.

My 2 cents, I am prepared to loose this discussion this time.

:-))

Cor
 
Cor,
I'm sorry: Something is being lost in the translation today:
But with this one I had to think about Toms message to me about
encapsulation.
Encapsulation is good! I vaguely remember your thread with Tom.
My idea is, that when you have calculations, let say incometax for a
country, it has to be in one class and not a part of an other class.
Correct the calculation for incometax by country should be in a specific
class. Especially when you want the calculation to be polymorphic.
Polymorphism is good!
And especialy not as part form a class (not a supplier) which has a function
in it that can be used in other programs.
?? I'm not following this statement, can you elaborate?

Thanks
Jay


Cor said:
Hi Jay B,

I seldom start a discussion with you and this one is very slippery ground
because I was you did a lot of investigations in the present time.

But with this one I had to think about Toms message to me about
encapsulation.

And this looks me someting for it.

My idea is, that when you have calculations, let say incometax for a
country, it has to be in one class and not a part of an other class.

And especialy not as part form a class (not a supplier) which has a function
in it that can be used in other programs.

My 2 cents, I am prepared to loose this discussion this time.

:-))

Cor



"Jay B. Harlow [MVP - Outlook]" <[email protected]> schreef in bericht
Marten,
In addition to the other's comments.

Can you provide more details on what you are attempting to accomplish?

You are correct is not good to copy the code for Calculate into both A & B.
However both can have a method called Calculate that performs a different
calculation based on being either an A or a B object.

In addition to the others comments about Shared & Inherits: If a B object
needs to call the Calculate method of an A object, then B needs either a
class variable of type A, or B's method needs an A object as a parameter.

Again can you provide more details, as I am not following what you want.
Maybe include 10 to 15 lines of code to show what you have now, that you are
trying to avoid.

Hope this helps
Jay
 
Hi Jay,
-----------------------------
I'm sorry: Something is being lost in the translation today:
Was not the translation just an ordinary forgotten correction error from
which I was hoping that your brain autocorrect would change it when I saw
it. It had to be:

I seldom start a discussion with you and this one is on very slippery ground
because I know you did many investigations in the present time.
---------------------------
This was the question from the OP:

The problem is: Class B also wants to use the calculate() function. I do
not think it is a good way to duplicate the calculate() function in both
classes.
--------------------
And I think that is not good to do it this way. He did want to use the
function from class A in class B without extra code. I said it is better to
build a separate "calculate" class. (And use that as well in A as in B of
course)

Cor
 
Cor,
And I think that is not good to do it this way. He did want to use the
function from class A in class B without extra code. I said it is better to
build a separate "calculate" class. (And use that as well in A as in B of
course)
Yes I agree, when its a common "calculation" that is "shared" by both
classes, yet is not specific to either class, a separate calculation class
is the way to go.

It really depends on what the OP is actually doing...

Thanks
Jay
 
Back
Top