+ Reply to Thread
Results 1 to 4 of 4

Return reference from object function

  1. #1

    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. #2
    Peter T
    Guest

    Re: 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

    <[email protected]> wrote in message
    news:[email protected]...
    > 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. #3

    Re: 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. #4
    Peter T
    Guest

    Re: 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

    <[email protected]> wrote in message
    news:[email protected]...
    >
    > 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 to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1