Results 1 to 7 of 7

Thread: how to clear class by self in vb6?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2020
    Posts
    3,749

    how to clear class by self in vb6?

    Is there any way to reclaim variables in class1?

    Set Me = Nothing 'err here

    Code:
    form1.frm:
    dim  a as new class1
    
    a.DelObject
    Sub DelObject()
    Set Me = Nothing 'err here
    End Sub
    
    
    Sub DoRelease()
        dim class1Objptr as long
       class1Objptr=ObjPtr(Me)
       clear by api with class1Objptr??
    End Sub
    
    
    Private Sub Class_Terminate()
    MsgBox "Class_Terminate"
    End Sub
    Last edited by xiaoyao; Feb 1st, 2024 at 07:02 PM.

  2. #2

    Thread Starter
    PowerPoster
    Join Date
    Jan 2020
    Posts
    3,749

    Re: how to clear class by self in vb6?

    why Set C = Nothing,
    C.TEST

    why Set C = Nothing,
    C.TEST

    Why can I use the methods in the object when the variables are empty? (Will automatically reinitialize the class object)

    Code:
    Dim C As New Class1
    
    Private Sub Command1_Click()
    C.TEST
    End Sub
    
    Private Sub Command3_Click()
    Set C = Nothing
    End Sub

  3. #3
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,538

    Re: how to clear class by self in vb6?

    Yeah, this is a quirk of VB6 thast confuses a lot of people the first time.... so, when you do this::
    Code:
    Dim C As New Class1
    causes this:
    Code:
    C.TEST
    To be proccessed like this by th compiler:
    Code:
    If C is Nothing Then
       set C = new Class1
    End If
    C.TEST
    So even though you set C to Nothing, the next time you click Command1, it'll get re-crreated. Now.... if you this:
    Code:
    Dim C As Class1
    
    private sub Form_Load()
      set C = New Class1
    End Sub
    
    Private Sub Command1_Click()
    C.TEST
    End Sub
    
    Private Sub Command3_Click()
    Set C = Nothing
    End Sub
    Now it'll operate as you'd expect.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Jan 2020
    Posts
    3,749

    Re: how to clear class by self in vb6?

    yes,Dim C As Class1

    thank you

  5. #5
    Fanatic Member
    Join Date
    Jan 2013
    Posts
    765

    Re: how to clear class by self in vb6?

    Here's a "compromise" that gets you as close to VB.Net syntax as VB "proper" will allow:

    Code:
    Dim C as Class1 : Set C = New Class1
    Regards, Phill W.

  6. #6
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,940

    Re: how to clear class by self in vb6?

    I use the pre-declared form names all the time, with no problem.

    However, to get the COM object uninstantiated when the form unloads, you have to explicitly use that form's pre-declared variable name. Let's say you have a Form1. To get its COM object uninstantiated, you'd do the following:

    Code:
    Private Sub Form_Unload(Cancel As Integer)
        Set Form1 = Nothing
    End Sub
    And you can't use the "Me" object because that's another reference to this Form1 object. You need to explicitly set that Form1 object variable to Nothing to get the last reference destroyed. And obviously, you need to make sure there aren't any other references that will re-load that COM object.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Jan 2020
    Posts
    3,749

    Re: how to clear class by self in vb6?

    how to set class1=nothing by api or asm code
    some times i need do like this:
    form1:
    Code:
    dim classA as new class1
    SUB TIEMR1
    SET classA =NOTHING
    TIMER1.ENABLE=FALSE
    END SUB
    class1:
    sub doCloseMe()
    form1.timer1.enable=true
    end sub

Posting Permissions

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



Click Here to Expand Forum to Full Width