How to implement a "helper" class

B

BobRoyAce

Let's say I have a class called ClassA which has a bunch of member
variables, properties, private and public methods. Then, let's say
that I want to create another class, ClassACalcEngine, that will do
certain calc's related to ClassA, but together with some fields that
would be populated just for the calc's...not related to ClassA itself
(e.g. PeriodBeginDate, PeriodEndDate, etc.). However, to perform the
calculations, ClassACalcEngine needs access to certain property
values, and public methods, of ClassA. What is the best way to
implement something like this?

I had originally thought that I'd have a member variable in ClassA
which was an instance of a ClassACalcEngine. However, I don't know how
the ClassACalcEngine will have access to the needed properties and
methods of ClassA. It should be noted that ClassACalcEngine would only
ever calculate things for a ClassA (i.e. has no value unless
associated with a ClassA).
 
T

Tom Shelton

Let's say I have a class called ClassA which has a bunch of member
variables, properties, private and public methods. Then, let's say
that I want to create another class, ClassACalcEngine, that will do
certain calc's related to ClassA, but together with some fields that
would be populated just for the calc's...not related to ClassA itself
(e.g. PeriodBeginDate, PeriodEndDate, etc.). However, to perform the
calculations, ClassACalcEngine needs access to certain property
values, and public methods, of ClassA. What is the best way to
implement something like this?

I had originally thought that I'd have a member variable in ClassA
which was an instance of a ClassACalcEngine. However, I don't know how
the ClassACalcEngine will have access to the needed properties and
methods of ClassA. It should be noted that ClassACalcEngine would only
ever calculate things for a ClassA (i.e. has no value unless
associated with a ClassA).

You might consider implementing ClassACalcEngine as a nested class -
in other words something like:

class ClassA
calc as classacalcengine
sub new()
calc = new classacalcengine (me)
end sub

class ClassACalcEngine
parent as classA

sub new (byval parent as classa)
' classaclacengine instance now has access to all members
(including
' private members) of parent classa instance
me.parent=parent
end sub
end class
end class

Nested classes can even access private members of their enclosing
class.

HTH
 
B

BobRoyAce

Thanks for the reply, Tom. I had thought of doing something similar
except that I would have defined the ClassACalcEngine in a separate
file, outside of the ClassA class. I guess that the reason that I
thought of doing it that way was in order to not have a really big
"file" containing both. Then again, by doing it that way, I would lose
access to the ClassA member variables...would have to go through
properties. I don't know as I care about that loss.

Soooo, basically, the solution includes ClassA having a reference to a
ClassACalcEngine, and vice-versa. For some reason, I think I had it in
my head that that was "not good" or violated some better way to do it
(i.e. design pattern).
 
G

Guest

Thanks for the reply, Tom. I had thought of doing something similar
except that I would have defined the ClassACalcEngine in a separate
file, outside of the ClassA class.

Why areyou thinking in terms of "files"? You should be thinking in terms of
objects :)
Soooo, basically, the solution includes ClassA having a reference to a
ClassACalcEngine, and vice-versa. For some reason, I think I had it in
my head that that was "not good" or violated some better way to do it
(i.e. design pattern).

You could always inherit from a base class - ClassAClacEngine can always
extend the base object with additional properties.
 
J

Jack Jackson

Thanks for the reply, Tom. I had thought of doing something similar
except that I would have defined the ClassACalcEngine in a separate
file, outside of the ClassA class. I guess that the reason that I
thought of doing it that way was in order to not have a really big
"file" containing both. Then again, by doing it that way, I would lose
access to the ClassA member variables...would have to go through
properties. I don't know as I care about that loss.

You can use partial classes to split the code between multiple source
files.
 
B

BobRoyAce

I've got it in my head that it's not "ideal" to have very large
classes with tons of properties and methods. I'm not sure where I got
that from. Perhaps from my days in database design where I was taught
that it's not ideal to have tables with lots and lots of fields.

Besides that, I was thinking that having a separate class to do all
the calc'ing was a better way to encapsulate all the calc logic. I
suppose that, by doing the nested class, I do just that.
 
C

Chris Anderson [MVP-VB]

BobRoyAce said:
Let's say I have a class called ClassA which has a bunch of member
variables, properties, private and public methods. Then, let's say
that I want to create another class, ClassACalcEngine, that will do
certain calc's related to ClassA, but together with some fields that
would be populated just for the calc's...not related to ClassA itself
(e.g. PeriodBeginDate, PeriodEndDate, etc.). However, to perform the
calculations, ClassACalcEngine needs access to certain property
values, and public methods, of ClassA. What is the best way to
implement something like this?

I had originally thought that I'd have a member variable in ClassA
which was an instance of a ClassACalcEngine. However, I don't know how
the ClassACalcEngine will have access to the needed properties and
methods of ClassA. It should be noted that ClassACalcEngine would only
ever calculate things for a ClassA (i.e. has no value unless
associated with a ClassA).
Since the helper class is essentially useless w/o ClassA, what about
overriding the constructor to include a reference to ClassA, then store
the reference as a member variable. Then the helper would have full
access to the dataclass.

-ca
 
C

Cor Ligthert[MVP]

Bob,

I get the idea that you are afraid about the size of the information you are
passing to your "helper" class.
That is always only some bytes. Everything in .Net is passed by its
reference (as long as it is not Web).

Therefore simple create a friend property in your "helper" class and pass
the reference (the name) to it.

As long as you don't use multithreading it will be accessed synchronized as
if it was in the class itself.

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top