Solved

Object Variable Declaration

Posted on 2002-04-02
10
176 Views
Last Modified: 2010-05-02
If I declare and instanciate a specific object variable (using early binding) in the general declaration area of a form, when is that object created?

Assuming the type of project is an Active X Document (if that makes any difference).

ie

Dim objie as New InernetExplorer

Is it when the application is first openned? When before or after form is first initialised?
When Internet Explorer is first openned?
0
Comment
Question by:Lisp
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 3

Expert Comment

by:msterjev
ID: 6912486
When the form is initialized(loaded into the memory - before Form_Load)
0
 

Author Comment

by:Lisp
ID: 6912490
Do you mean before the initialise event is fired or after?
0
 
LVL 3

Expert Comment

by:msterjev
ID: 6912564
Before
0
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 

Author Comment

by:Lisp
ID: 6912786
According to the MCSD 70-176 practice exam from UCertify it occurs when the object is first used.

This makes no sense to me.  Another question they had related to the CDROM AutoRun feature and it said you had to change the name of your setup.exe file to autorun.exe.  There is a reference to this in MSDN but I think it is some ancient way of doing it.  Apparently that's how it was done on the windows 95 CD.


Can anybody please verfy this?

0
 
LVL 6

Expert Comment

by:VK
ID: 6913066
Hello Lisp !

You can find it out by yourself by setting a breakpoint in the Class_initialize event.

V.K.
0
 
LVL 18

Accepted Solution

by:
mdougan earned 100 total points
ID: 6913263
I just ran a test (as VK suggested) and sure enough, an object variable declared in the General Declarations Section of a form as:

Dim oProfile As New cProfile

Won't actually be instantiated until the first time it is referenced.  So, in my code, my Form_Load procedure called another sub to load a combo box, and that sub procedure referenced the object and that is when the initialize event of the object fired.
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 6913383
Yes it is counter intuitive, but that's how it works.  Best practice is to explicitly create your object in your code rather than use the above method of implicit instantiation.  In other words, use

Dim o as clsMyObj
Set o = New clsMyObj

instead of:

Dim o as New clsMyObj

There are a few good reasons to do this.  One is that referring to an uninstantiated variable NOT declared with New will raise an error, allowing you to see clearly where you are using said object.  Declaring with new makes it hard to know that an object has been released, as any reference to it will not return an error and testing If o Is Nothing will never return true.
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6913453
I agree with Paul, besides....

Dim objie as New InernetExplorer

sub test()
objie.visible=true ' or whatever: here is created a shown
end sub
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 6913568
All this actually makes sense for the same reason that dlls work the way they do--only use what you need.

If you have two forms, and never load the second, does it make sense for your project to load it?  Of course not.

However, if you have two forms and the second one takes a long time to load, you have the choice of when to load it--either when the user needs if, or when the application begins (via code.)

--
When does an object get loaded?  I would say (based on experience with user controls) that it works as follows:

(Event) Initialize container
1.identify all pieces of memory associated with the container/form (essentially variables)
2.fire initializeproperty event

Initialize Property
1.load all controls/objects
2.load all persistent data for these controls from a property bag
3.fire load event

Load
1.gather information about object properties, methods and events
2.if needed, fire the show/paint event

Show
1.set object properties to visible and enabled (if not told otherwise
2.paint the control's appearance into the correct location
3.fire activate event

Activate
1.set the window to be the primary window, and to receive events by default
2.wait for user interaction or other events

0
 

Author Comment

by:Lisp
ID: 6914761
Thanks.  This was confusing the hell out of me.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month6 days, 21 hours left to enroll

622 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question