Referencing an Object
Dim IE WithEvents As InternetExplorer
Well, we all have a good understanding of what the Dim statement does. In this case it is preparing the variable IE to point to an Object rather than a DataType. We have also specified the Object as InternetExplorer. The WithEvents keyword also exposes the form containing this declaration to any Events the InternetExplorer class may raise.
We could have used:
Dim IE As Object
That is a valid question. Let's have a look and see why I told you to write the code that specified the InternetExplorer class in the Dim statement. One benefit we have immediately lost from declaring IE as any Object rather than the InternetExplorer Class Object is the events. You cannot declare WithEvents unless you specifically state what Class Object will be raising events.
There is a little more to it than that though. When you was working through the code before the standard object list appeared once you had typed in "IE." this stops as well once only a generic Object is declared. This can make the task of interrogating properties of the Object and executing its methods a little more tricky.
So why does the Visual Basic Interface do that? After all it knows the Class name from the Set statement under the cmdStart click event. The reason for this is that IE is set at design time to the datatype or object you are declaring. This way in the design time environment you can benefit from the standard help and formatting whilst you are typing which makes your job as a developer a lot easier. This is also known as early binding, as the object is bound to you early at design time instead of late at run time. Early binding is far more optimized than Late binding due to the nature of the project being able to deduce what to do before execution.
Page 5 of 6