+ Reply to Thread
Results 1 to 7 of 7

Lifetime and class modules

Hybrid View

  1. #1
    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.

  2. #2
    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.
    ? ? ? I like the way how Excel can access system API. Really cool ! ? ? ?

  3. #3
    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.

  4. #4
    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 !

  5. #5
    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.

  6. #6
    Registered User
    Join Date
    01-16-2007
    Location
    Near the box
    Posts
    58
    Just solved. Works great ! I'll provide my solution later.

+ 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