Stupid question, but I'm use to using "Stop" from VBscript, and I noticed that was the same as a breakpoint in VB6 Studio.
Printable View
Stupid question, but I'm use to using "Stop" from VBscript, and I noticed that was the same as a breakpoint in VB6 Studio.
Here's the best way to close the application (anything but the EVIL statement, End):
Code:' If you have just one form:
Call Unload(Me)
' If you have multiple forms:
Dim Form As Form
For Each Form In Forms
Call Unload(Form)
Next
For the first time in my VB career I don't agree with Yonatan :rolleyes:
I think this is the proper way to unload your form:
:pCode:
Private Sub From_Unload(Cancel As Integer)
Dim Frm As Form
For Each Frm In Forms
Unload Frm
Set Frm As Noting
Next Frm
End 'this is ok in this case!
End Sub
Jop,
Should be:Code:Set Frm As Nothing
Code:Set Frm = Nothing
For the first time I disagree with Jop
For Each Frm In Forms
Unload Frm
Set Frm As Noting ??????????? --- = Nothing (Noting is the infinitive of the verb to Note...)
Next Frm
Sorry Jop!... I couldn't resist....
Oh oh oh oh oh oh :o
I should shut my big mouth :(
Sorry Yonatan :(
I knew it but was too lazy to pay attention :o
I'm soooo embarassed :o
Jop: I read your code sixty-nine times, and arrived at several conclusions.
- Form_Unload instead of From_Unload. :rolleyes:
- Form is waaaaay cuter than Frm.
- I think Call Unload(Frm) looks better than Unload Frm (well, I'm a Call fan, ignore me :)).
- End isn't ok in any case. It is evil. Evil. EVIL!!! It will haunt you in your nightmares! :eek:
Besides, it would exit the program whether or not you put the End there.- "=" instead of "As" and "Nothing" instead of "Noting" in Set Frm = Nothing. You'll get over it. :rolleyes:
Anyway, Frm is just a "pointer" to the form, so Set Frm = Nothing wouldn't help here.
M'kay? ;)
It is good to set the form to nothing so it frees up any resources and use the Unload statement with it as well.
That's very good Yonatan. So many mistakes! Glad you aren't a teacher ;).
And lighten up on Jop, he had the idea.
I'm not disagreeing with anyone.
But here, use this code.
I'm right, as usual, your all wrong! :rolleyes:
Code:Public Sub UnloadAllForms()
Dim Form As Form
For Each Form In Forms
Unload Form
Set Form = Nothing
Next Form
End Sub
Usage
UnloadAllForms
Matthew, this code looks better... :rolleyes:
Code:Public Sub UnloadAllForms()
Dim Form As Form
For Each Form In Forms
Unload Form
Set Form = Nothing
Next Form
End Sub
Self-contradiction sucks. :rolleyes:Quote:
Originally posted by Matthew Gates
I'm not disagreeing with anyone.
...
I'm right, as usual, your all wrong!
Anyways, as I said, only one instance of each form exists, until it is unloaded with Unload.
So, Set Form = Nothing won't help with anything, and will only make it a few milliseconds slower.
You agree that this code:
Is pretty much equivalent to:Code:For Each Form In Forms
Call Unload(Form)
Next Form
So, how would this help?Code:Set Form = Form1
Call Unload(Form) ' Now Form1 is invalid
Set Form = Form2
Call Unload(Form) ' Now Form2 is invalid
Set Form = Form3
Call Unload(Form) ' Now Form3 is invalid
' And so on... (Form1 and Form2 and Form3 were all in memory)
So, I just proved that I'm right 99% of the time, and besides, I don't care about the remaining 3%. :)Code:Set Form = Form1
Call Unload(Form) ' Now Form1 is invalid
Set Form = Nothing ' Why? It is already 100% not in memory,
' and now Form points at something illegal and setting it
' to Nothing doesn't do anything.
Set Form = Form2 ' VB says: "It was just Nothing! Jeez!"
Call Unload(Form)
Set Form = Nothing
Set Form = Form3 ' "Make up your mind!"
' Etc.
Lighten up some more? I tried, but couldn't jam any more smilies into that post. :rolleyes:
Well, that was useless, but fun!
Yeah, and by the way, indentation rocks.
100 - 3 = 97 :rolleyes:Quote:
Originally posted by Yonatan So, I just proved that I'm right 99% of the time, and besides, I don't care about the remaining 3%. :)[/B]
oetje: It's because there are three kinds of people in the world. Those who can count and those who can't. :rolleyes:
Poor Hambone....!
He just asked a question he thought was a stupid question..!
Ha!!Ha!!
Because you ever wondered why End has remained intact upto vb6 even if it has such a bad reputation. I'm using it regulary, and i'm going to use it as often as i can (just kidding).
Now actually i'm sure you can change this a bit and solve it wihtout End, but that's not the point. The point is that i have a break in class1 terminate event, andCode:Public Declare Function GetTickCount Lib "kernel32" () As Long
Private die As Long, temp As Class1
Sub main()
Set temp = New Class1
Form1.Show
DoEvents
Do
If Forms.Count Then
DoEvents
Else
If die Then
If GetTickCount > die Then End 'Exit Do
Else
die = GetTickCount + 1000
End If
End If
Loop
End Sub
1. It doesn't fire when End is used
2. It fires when Exit Do is used.
That means End don't unload properly, but if you exit the code the sub main, it will. If you on the other hand use unload statement to unload the object, the codes will be equal. That's why End has this bad reputation. Now what proves you can have use of End? Well it's simple, if you just unload everything correctly, it can be equally usefull as a exit do, or exit for in a loop. Might not be much use but when you get into complicated algoritms, it can.
t h E E n d
Hmmm.
Set frm = Nothing - releases a reference
Unload frm - Unloads the form and kills references
Neither is foolproof since if you access the forms properties in the Unload Event it will maintain the Instance Data associated with the form - the form might disappear from view but you won't have cleaned up properly...
Your approaches are all far too wimpish...
Take the BRS approach (Big Red Switch) for those who remember the IBM PC (original). Turn the bugger off and you will DEFINITELY release that memory.
So you might get a bit of data corruption - means they have to rely on some guru to fix it...
Wonder who that might be?
Hehehe.
Cheers all,
Paul
Sub ExitForms()
Dim iFormCount As integer
For iFormCount = Forms.Count - 1 To 0 Step -1
Unload Forms(iFormCount)
Next iFormCount
End
Exit Sub
This will also do - a little bit simple compared to the above but it works. It ensures all the forms are unload before closing the application.
H.
You do need to Set frm = Nothing to release a form completely from memory, according to the help file:
"The only way to release all memory and resources is to unload the form and then set all references to Nothing. The reference most commonly overlooked when doing this is the hidden global variable mentioned earlier. If at any time you have referred to the form by its class name (as shown in the Properties Window by the Name property), you've used the hidden global variable. To free the form's memory, you must set this variable to Nothing."
This quote is under "Life Cycle of Visual Basic Forms" in MSDN.