Transfering Values through several classes

U

Ueli Werner

Hi newsgroup

I have a little problem with the design of my classes.

I have 3 classes. The first class is named 'Top', the second 'Middle' and
the third is named 'Bottom'.

I also have a class called 'Parameters'.

The class 'Top' has a collection of 'Middle' and the classe 'Middle' has an
object of 'Bottom'. The class Top also has a object of Parameters. This
object 'Parameters' must be also used in the class 'Bottom' with the values
the Class 'Top' sets.

This is my code in a similar way.

Public Class Parameters

end class

Public Class Top
dim param as new Parameters
dim mymiddle as MiddleCol
end class

Public Class Middle
dim mybottom as Bottom
end class

Public Class MiddleCol

'Collection of Middle

end class

Public Class Bottom
'Use ParametersClass of Top
end class

I know that the design is not very good, but that's the way my customer want
it. Maybe I can change the design if I have a better idea.

My question is now how to transfer the object of Paramters to the Class
Bottom without transfering it through the Class middle, because the
MiddleColletion can have more than 300 entries

Any ideas?

Thanks for every help.

Regards

Ueli Werner
 
C

Cor Ligthert[MVP]

Ueli,

Be aware that you tranfer in OOP mostly only references.

I think that keeping that in mind can help you easier to get the fish.

Cor
 
J

James Hahn

Top can expose a property called ParamProp (which is equal to param, an
instance of Parameters) so that in Bottom you can access param members using
code such as
var = TopInstance.ParamProp.var

So I think your question comes down to "In Bottom, how can I get a reference
to the instance of Top that contains the collection of which I am an item?"

If you are creating the Middle instances in Top, then you can pass a
reference to TopInstance in the constructor for Middle, and pass that same
reference into the constructor for Bottom.

eg
Public Class Parameters
Property var
...
end class

Public Class Top
dim param as new Parameters
dim mymiddle as MiddleCol
For <whatever>
MiddleCol.items.add(New Middle(Me))
next
end class

Public Class Middle
sub New(myCreator as Top)
...
end sub

dim mybottom as Bottom = New Bottom(myCreator)
end class

Public Class MiddleCol
...
end class

Public Class Bottom
sub new(myCreatorsCreator as Top)
...
end sub

var = myCreatorsCreator.params.var
end class

If you are creating only the collection in Top, and populating it (in Top)
with instances of Middle from some other source, then you could add a
property to Middle that is updated with the reference to TopInstance when it
is added to the collection. That reference is then passed in the constructor
for Bottom.

If you are creating only the collection in Top, and both the creation of the
Middles and adding then to the myMiddle collection is happening elsewhere,
then you would need to add a property to MiddleCol that contains a reference
to TopInstance so that the property could be accessed by whatever routine
adds the items to MiddleCol in order to update a property in Middle that
could be used in Middle to pass the reference to the constructor for Bottom.
 
A

Armin Zingler

Ueli said:
Hi newsgroup

I have a little problem with the design of my classes.

I have 3 classes. The first class is named 'Top', the second 'Middle'
and the third is named 'Bottom'.

I also have a class called 'Parameters'.

The class 'Top' has a collection of 'Middle' and the classe 'Middle'
has an object of 'Bottom'. The class Top also has a object of
Parameters. This object 'Parameters' must be also used in the class
'Bottom' with the values the Class 'Top' sets.

This is my code in a similar way.

Public Class Parameters

end class

Public Class Top
dim param as new Parameters
dim mymiddle as MiddleCol
end class

Public Class Middle
dim mybottom as Bottom
end class

Public Class MiddleCol

'Collection of Middle

end class

Public Class Bottom
'Use ParametersClass of Top
end class

I know that the design is not very good, but that's the way my
customer want it. Maybe I can change the design if I have a better
idea.
My question is now how to transfer the object of Paramters to the
Class Bottom without transfering it through the Class middle, because
the MiddleColletion can have more than 300 entries

Any ideas?

When is the Bottom object created? Where is it created? Does it have to have
a reference to a Parameters collection during it's whole life time (as a
field)? If yes, it must be passed to the Bottom object's constructor. Now,
if the Bottom object is created inside the Middle object, you have to ask
which is the correct parameters collection you have to pass. As the author
of the Middle class you might say: It's my "containing" Top object's
parameters collection. So, even if you wrote you don't want it, it may be
necessary to pass the parameters object from the Top object to the Middle
object. It's a kind of object oriented problem bubbling to solve it.


Armin
 
P

Phill W.

Ueli said:
I have a little problem with the design of my classes.

I have 3 classes. The first class is named 'Top', the second 'Middle'
and the third is named 'Bottom'.

I also have a class called 'Parameters'.

The class 'Top' has a collection of 'Middle' and the classe 'Middle' has
an object of 'Bottom'. The class Top also has a object of Parameters.
This object 'Parameters' must be also used in the class 'Bottom' with
the values the Class 'Top' sets.
My question is now how to transfer the object of Paramters to the Class
Bottom without transfering it through the Class middle, because the
MiddleColletion can have more than 300 entries

You don't.
Leave them stored in the Top class, but allow the [Middle and] Bottom
class[es] to access them. To do this, each object needs to know about
it's [immediate] Parent:

Class Top

Friend ReadOnly Property Params() as Parameters
Get
Return m_params
End Get
End Property

Private m_params as new Parameters

End Class

Class Middle

Private Sub New()
End Sub

' Middle objects can only be created from a Top object
Friend Sub New( ByVal parent as Top )
m_parent = parent
End Sub

Private m_parent as Top = Nothing

Friend ReadOnly Property Parent() as Top
Get
Return m_parent
End Get
End Property

End Class

Class Bottom

Private Sub New()
End Sub

' Bottom objects can only be created from Middle ones
Friend Sub New( ByVal parent as Middle )
m_parent = parent
End Sub

Private m_parent as Middle = Nothing

Friend ReadOnly Property Parent() as Middle
Get
Return m_parent
End Get
End Property

End Class

Now, any instance of Bottom can access the Params property on the Top
class using :

Public Sub Button1_Click( ... )
Me.Parent.Parent.Params ...

' Me is the [current] Bottom object
' Me.Parent is the Middle object
' Me.Parent.Parent get you up to the Top object

End Sub

HTH,
Phill W.
 

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