View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
[email protected] e_hobsbawm@hotmail.com is offline
external usenet poster
 
Posts: 7
Default Return reference from object function


Peter T wrote:
Hi Eric,

There's a lot that doesn't make sense. I started trying to second guess the
objectives but gave up, can't see how it ever worked at all yet alone make
Excel crash. You have typed code direct into the post rather than copying
the original, I suspect typos and/or omissions occurred while doing that.

Regards,
Peter T


Hi Peter,

Sorry for typing code directly (sincerely - I myself hate when people
do that. It's always wrong). This is a scaled-down, tested version
which I copied and pasted.

The idea is that I have objects with data which I have in my own
container class.

This is the data-class:
----------- mySmallObjectClass -------------
Private data_ As String

Public Function setdata(ss As String)
data_ = ss
End Function

Public Function getdata() As String
getdata = data_
End Function
---------------------------------------------------

This is the container-class

---------- myContainerClass -----------------
Dim mySmallObjectList As Collection
Dim name As String

Private Sub Class_Initialize()
Set mySmallObjectList = New Collection
End Sub

Private Sub Class_Terminate()
Set mySmallObjectList = Nothing
End Sub

Public Function setname(ss As String)
name = ss
End Function
Public Function getname() As String
getname = name
End Function
Public Function add(ss As String)
Dim newMySmallObject As New mySmallObjectClass
newMySmallObject.setdata (ss)
mySmallObjectList.add newMySmallObject, ss
End Function

Public Function printout()
Dim ss As mySmallObjectClass
Dim i As Integer
i = 0
For Each ss In mySmallObjectList
i = i + 1
Debug.Print ("Item " & i & " in " & name & ": " & ss.getdata())
Next
End Function

Public Function subcontainer(s As String) As myContainerClass
Set subcontainer = New myContainerClass

Dim ss As mySmallObjectClass

For Each ss In mySmallObjectList
If ss.getdata < s Then subcontainer.add (ss.getdata)
Next

End Function

Public Function item(key As String, val As String)
mySmallObjectList.item(key).setdata (val)
End Function
------------------------------------------------------------------------------

And finally a sub for doing tests:
------------------- MODULE 1 -----------------
Public Sub runtests()
'Fill my container
Dim myContainerObject As myContainerClass
Set myContainerObject = New myContainerClass
myContainerObject.setname ("OrigContainer")
myContainerObject.add ("apple")
myContainerObject.add ("banana")
myContainerObject.add ("citrus")
myContainerObject.add ("dates")
myContainerObject.add ("esparagus")
myContainerObject.add ("figs")
myContainerObject.add ("gherkins")
myContainerObject.add ("hallonberries")

'Print out the members in the container
myContainerObject.printout

'Create a sub-container
Dim subContainerObject As myContainerClass
Set subContainerObject = myContainerObject.subcontainer("d")
subContainerObject.setname ("SubContainer")
subContainerObject.printout

'Now change an object in original container and look if it's
changes in subcontainer
myContainerObject.item "apple", "pear" 'set object data to "pear"
instead of apple
myContainerObject.printout
subContainerObject.printout

End Sub
--------------------------------------------------


So to my problems:
It works as expected, but I am a bit worried since my Excel crashes
every time I shut it down. I would be very happy if someone could say
if my setup with the container class function "subcontainer" returning
a reference is a good idea or not.

I am quite new to VB, but not to OO-programming, so I would like all
help, tips and pointers!

//Eric