Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default Return reference from object function

I have to admit that I still not get how things work with references in

VB.

I have an object that looks like:


---- myObjectClass -----
private dim myColl as collection


private sub class_initialize()
set myColl = new collection
end sub


private sub class_terminate()
set myColl = nothing
end sib


private function add(v as variant)
myColl.add(v)
end function


public function initilize(a as string)
dim b as variant
dim i as integer


b = split(a,";")
for i = lbound(b) to ubound(b)
myColl.add b(i)
next


end function
----------------------------------


Now I want to write an object function that returns a reference to a
new myObjectClass, with just some of the elements in myColl. I wrote it

like this


---- myObjectClass -----


public function return_match(a as string) as myObjectClass
dim newObject as new myObjectClass


for each c in myColl
if a c then newObject.add(c)
next


return_match = newObject


end function
----------------------------------


I now use it like this:


dim full_list as myObjectClass
dim sub_list as myObjectClass


set full_list as new myObjectClass
full_list.initialize("apples;bananas;citrus;dates; figs")
set sub_list = full_list.return_match("c")


-----------------------------------


I think it works, but I have crashes now and then. Does this have
memory problems, since I allocate a new object in return_match but
never free it? If so, how would I go ahead to return the object as a
reference?


Best Regards
Eric

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Return reference from object function

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

wrote in message
ps.com...
I have to admit that I still not get how things work with references in

VB.

I have an object that looks like:


---- myObjectClass -----
private dim myColl as collection


private sub class_initialize()
set myColl = new collection
end sub


private sub class_terminate()
set myColl = nothing
end sib


private function add(v as variant)
myColl.add(v)
end function


public function initilize(a as string)
dim b as variant
dim i as integer


b = split(a,";")
for i = lbound(b) to ubound(b)
myColl.add b(i)
next


end function
----------------------------------


Now I want to write an object function that returns a reference to a
new myObjectClass, with just some of the elements in myColl. I wrote it

like this


---- myObjectClass -----


public function return_match(a as string) as myObjectClass
dim newObject as new myObjectClass


for each c in myColl
if a c then newObject.add(c)
next


return_match = newObject


end function
----------------------------------


I now use it like this:


dim full_list as myObjectClass
dim sub_list as myObjectClass


set full_list as new myObjectClass
full_list.initialize("apples;bananas;citrus;dates; figs")
set sub_list = full_list.return_match("c")


-----------------------------------


I think it works, but I have crashes now and then. Does this have
memory problems, since I allocate a new object in return_match but
never free it? If so, how would I go ahead to return the object as a
reference?


Best Regards
Eric



  #3   Report Post  
Posted to microsoft.public.excel.programming
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

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Return reference from object function

In a quick glance I can't see anything obvious wrong, all the objects looks
as if they clear down in an orderly way. Not sure if same applies to your
real life set up if say object ref's to class's are at global level.

You could change a few things along the following lines and see if it makes
any difference.

'Public Function setdata(ss As String)
' data_ = ss
'End Function
'
'Public Function getdata() As String
' getdata = data_ ' why the underscore's
'End Function
Public Property Let propData(ss As String)
'data_ = ss
data = ss
End Property

Public Property Get propData() As String
propData = data ' data_
End Property

use like this

newMySmallObject.propData = ss
ss = newMySmallObject.propData

similarly with functions setname & getname


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

Regards,
Peter T

wrote in message
ps.com...

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



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
function to find value and return cell reference rcc Excel Discussion (Misc queries) 6 June 27th 12 02:55 AM
how do I create a cell reference from ADDRESS function return? Coachdenny Excel Worksheet Functions 3 December 2nd 08 04:22 AM
return the column reference number of a function result Mahendhra Excel Discussion (Misc queries) 2 May 16th 05 12:46 PM
Function return by reference R Avery Excel Programming 3 August 10th 04 10:43 PM
Function unable to return Collection object Adrian[_7_] Excel Programming 1 July 12th 04 06:22 PM


All times are GMT +1. The time now is 05:21 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"