Does VB6 allow you to reference a Form instance as a singleton, simply by naming it a data type? or what is happening?

I see code like "Unload frmMain", from which I can say that frmMain is a type / module name, and I don't think that it can also be the variable name "ObjFrmMain" at the same time. However, this command successfully issues the form to be unloaded.

So, is the data type used as an alias for its only existing instance? Or maybe for all its instances?

Does VB6 use similar things for data types other than types obtained from the form?

+4
source share
2 answers

Yes, VB6 has an odd object behavior. This gives you several shortcuts for working with form objects.

Load frmMain 

... will load one instance of this form under this variable name. Actually:

 frmMain.lblSomeLabel.Caption = "some caption" 

... will load this instance. But:

 frmMain.SomeStringMember = "some value" 

... will not load the form object (which means the window itself), but you can access these variables, so essentially the name of the form is a global variable.

However, you can create new instances:

 Dim newForm As MyForm Set newForm = New MyForm newForm.Show vbModal 

This will actually create a new instance of MyForm , load it and show it, so you can have multiple instances of the same form.

Also beware of the oddness in the New key:

 Dim newObject As New MyClass Set newObject = Nothing newObject.SomeStringProperty = "some value" 

This works without the error "Object Reference Not Set ...". When you declare a reference variable using As New syntax, you can destroy the object by setting it to Nothing , and then reference that variable again and create a new instance.

In fact, what really happens with forms. There is an implicit:

 Dim frmMain As New frmMain 

Personally, I prefer not to use As New syntax because it is confusing and dangerous. It also has a performance penalty, against this:

 Dim newObject As MyClass Set newObject = New MyClass 

... but you are stuck with it for forms.

What happens when Unload frmMain called Unload frmMain is that it unloads the window (and all controls), so all the data in them has disappeared, but the frmMain object is still hanging around. Therefore, even after unloading it, you can access any member variables and properties. However, if something refers to a control on the form, it invokes the implicit Load frmMain . This is the source of many subtle programming errors in VB6, especially when you are trying to close.

+7
source

Yes, this is special functionality in VB6 and earlier. I usually tried to avoid this, as I saw it more as confusion than help.

Next comment In Visual Basic 6.0 and earlier, a special default instance for each form is automatically created for you and allows you to use the form name to access that instance. taken from this page MSDN: Working with multiple forms in Visual Basic.NET: upgrade to .NET

+2
source

Source: https://habr.com/ru/post/1341303/


All Articles