+ Reply to Thread
Results 1 to 7 of 7

Lifetime and class modules

Hybrid View

  1. #1
    Registered User
    Join Date
    01-16-2007
    Location
    Near the box
    Posts
    58

    Lifetime and class modules

    Hi there,

    I need to have some object during all excel session. I have some class module and need to access the object of that. Now I have some trouble when using it - everytime I access the object the constructor is called each time and all old values/settings within the object are missed and even destructor is called after each access.

    I would need to declare it on the start and on the end only like that:

    Private Sub Workbook_Open()
        Set custom_object = New TCustom_Object
    End Sub
    
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
        Set custom_object = Nothing
    End Sub
    And then access it from VA modules like Global variable

    Module1 :: Public Sub make_change1()
    		custom_object.	
    	   End Sub
    
    Module2 :: Public Sub make_change2()
    		custom_object.	
    	   End Sub
    I've tried to declare it as Static object inside procedure - still no work.

    Public Sub...
             Static custom_object As New TCustom_Object
    Any idea ? TIA
    ? ? ? I like the way how Excel can access system API. Really cool ! ? ? ?

  2. #2
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    Declare a public variable, fixed_TCustom, in a normal module.
    Public fixedCustomObject as new TCustom_Object
    In the class module
    Private Sub Class_Initialize()
        fixedCustomObject.Name = "fCO"
    End Sub
    (Or some other property of the class)

    This makes sure that fixedCustomObject always has the values that you want.

    I hope it helps.

  3. #3
    Registered User
    Join Date
    01-16-2007
    Location
    Near the box
    Posts
    58
    Hi mikerickson,

    first of all THX for replay but I don't think it's the solution. Moreover this is not a correct OOP construction.

    What I need is to have object which is instanced only once on the Excel's start and available all the Excel's session time without any reinstancing.

    It should have something to do with Static word statement and a correct scope (and even maybe instancing of the module class too).

    Anyway THX for nfo !
    Last edited by bettatronic; 07-28-2007 at 05:46 PM.

  4. #4
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    fixedCustomObject will be the first object instanced. Every time a new fixed_TCustom is instanced, the "value" of fixedCustomObject will be re-set (to the same "value") , but it will not create a new instance.

  5. #5
    Registered User
    Join Date
    01-16-2007
    Location
    Near the box
    Posts
    58

    Thumbs up

    Sorry but I don't agree with you still. Your maybe work for constant but I need to have counter variable inside the class. Like this one:

    Class modules :: TCustom_Object

    
    Private p_sheet As Worksheet
    Private p_left as Integer
    Public count As Integer
    
    Private Sub Class_Initialize()
              ' constructor
               count = 0
    End Sub
    
    Private Sub Class_Terminate()
               ' destructor
    End Sub
    
    Public Property Let left(vdata As Integer)
             p_left = vdata
    End Property
    
    Public Sub proccess1()
             ....                         ' some code here
             count = count + 1
    End Sub
    This is my class module. It's all about how to stay alive during full Excel's session. When Excel's terminating I want to show count variable of the object. That's all. But now everytime I call obj.proccess1 both constructor & destructor is called -> no old count status...

    Problem still persists but THX for your effort !

  6. #6
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    I've tried similar things. I've declared a public objectCount variable in a normal module together with
    Private Sub Class_Initialize()
    objectCount = objectCount + 1
    End Sub
    
    Private Sub Class_Terminate()
    objectCount = objectCount - 1
    End Sub
    The results have not been what I expected. I'm not sure if the counting technique flawed or my expectations are wrong.

    The technique in the previous posts was used to create a "null" for my class, but (as you noted) it's constant.

    Good luck with the problem.

+ 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