Thursday, July 17, 2008

vb questions 8

Class Module

Property procedures
Property Get - Returns the value of a property.
Property Let - Sets the value of a property. – Let x = 3
Property Set - Sets the value of an object property. – Set objThing = New
Thing
•Property procedures are public by default. •The Property Get declaration
must use arguments with the same name and data type and less one number as
the arguments in the Property Let procedure. •The data type of the
final argument in a Property Set declaration must be either an object type or
a Variant.


••To create a read-only property, simply omit the Property Let or (for object

properties) the Property Set.


Event


An object that raises events is called an event source. To handle the events
raised by an event source, you can declare a variable of the object's class
using the WithEvents keyword. WithEvents variables must be module-level
variables.

•Option Explicit

Private WithEvents mWidget As Widget







Limitations on WithEvents Variables
••A WithEvents variable cannot be a generic object variable. That is, you
cannot declare it As Object.

•You cannot declare a WithEvents variable As New. •You cannot declare
WithEvents variables in a standard module. •You cannot create arrays of
WithEvents variables.




Inside Class module, use Public Event … RaiseEvent… pair. Events are always
Public.








DragOver event determines what happens after dragging is initiated and before
a control drops onto a target. This event occurs only if OLEDropMode is set
to 1 (Manual).



Private Sub Form_DragOver(source As Control, x As Single, y As Single, _

state As Integer)



Source - The control being dragged.

State - The transition state of the control being dragged in relation to a
target form or control. 0: enter, 1: leave, 2: over.



Private Sub object_OLEDragOver(data As DataObject, effect As Long, button _

As Integer, shift As Integer, x As Single, y As Single, state As Integer)



Moving DataObject (not Control) from one control or application to another.








KeyDown and KeyUp events occur when the user presses (KeyDown) or releases
(KeyUp) a key while an object has the focus. (To interpret ANSI characters,
use the KeyPress event.)







Polymorphism



VB supports Interface inheritance by two steps:





1.
1.2.Define interfaces – a class module by adding methods without code.
3.Define Child class using keyword Implements, then implement all the
properties and methods of parent interface.



Option Explicit



Implements Animal



Private Sub Animal_Move(ByVal Distance As Double)

Debug.Print "Flea moved"

End Sub



VB doesn’t support aggregation.







Component Programming








To declare an object variable for an object not defined in a type library





Dim objAny As Object








The main difference between declaring a variable of a specific class and
declaring a variable of the generic Object class is in how ActiveX binds
the variable to the object. When you declare a variable of the generic
Object class, ActiveX must use late binding. When you declare an object
variable of a specific class, ActiveX uses early binding, which can speed
object references.





Assigning an Object reference








If the ActiveX component supplies a type library, using NEW

If not, using



Set objectvariable = CreateObject("progID", ["servername"])

Or Set objectvariable = GetObject([pathname] [, progID])



For example:

Dim xlApp As Object

Set xlApp = CreateObject("Excel.Application") ‘late binding






Dim wdApp As Word.Application

Set wdApp = GetObject("", "Word.Application")





Set X = GetObject(, "MySrvr.Application")








X references an existing Application object.



Dependent objects are lower in an object hierarchy, and they can be accessed
only by using a method of an externally creatable object. Defined by Class
Instancing property - PublicNotCreatable.



Data associated with an embedded object is not persistent; that is, when a
form containing an OLE container control is closed, any changes to the data
associated with that control are lost. To save updated data from an object to
a file, you use the OLE container control's SaveToFile method.





Private Sub cmdSaveObject_Click ()

Dim FileNum as Integer ' Get file number.

FileNum = FreeFile ' Open file to be saved.

Open "TEST.OLE" For Binary As #FileNum ' Save the file.

oleObj1.SaveToFile FileNum ' Close the file.

Close #FileNum

End Sub








If the object is linked, then only the link information and an image of the
data is saved to the specified file. The object's data is maintained by the
application that created the object. If a user wants to save changes to a
linked file, the user must choose the Save command from the ActiveX
component's File menu because the SaveToFile method applies only to
embedded objects.







Asynchronous notifications and implementation








A client makes a method call and tells the component it wants to be
notified when certain things happen. Then the client can be free to do
other things while it’s waiting for component notification. Two
implementations: An event is like an anonymous broadcast, while a call-back
is like a handshake 4.Using events – RaiseEvent - WithEvents



Using Call-back

Test ActiveX Component








To test a component, you need to create a client application.
If you’re creating components as part of an application, you can use the
application itself as the test program. The test project must be an Exe
project. To test call-backs, use an ActiveX Exe project.



To test In-process server:

5.Create a Project Group containing the component and test project. 6.Set
test project as active project and make a reference to the component. ( not
necessary for ActiveX control)

7.Add code to test the properties and methods of each public class provided
by your component.





To test Out-of-process server:
••Make Component executable. •Ctrl+F5 to run component
project. You cannot add a reference to the component project unless it’s in
run mode.

•Open a second instance of the Visual Basic development environment.
•Start a new test project and make a reference to the component. •Add code
to test the properties and methods of each public class provided by your
component.






The difference between debugging controls and debugging other objects is
that some of the code in your control must execute while the testing form is
in design mode. To put a control into a state such that its code can execute
at design time, you must close the control's visual designer to enable the
control's icon in the Toolbox.

 



Software Distribution



Three ways to start the Package and Deployment Wizard:
1.
1.
1.In your application project, start from Add-In 2.Run it as a stand-alone
component from outside IDE. 3.Start it in silent mode by launching it from a
command prompt. Unattended with script.



•PDCmdLn.exe C:\Project1\Project1.vbp /p "Internet Package" /d Deployment1

/l "C:\Project1\SilentMode.log"






Signing and licensing must be done outside of the packaging process.

A dependency (.dep) file contains information about the run-time requirements
of an application or component. In Visual Basic, dependency information is
stored in files generated by the Package and Deployment Wizard or created
manually by you.
•Component Dependency File: A .dep file lists all the files required by a
particular component. When you purchase or use a component from a vendor,
you receive a .dep file from them. •The VB6dep.ini file provides the Package and Deployment Wizard with an all-purpose list of dependencies and references used by Visual Basic. This list is created when you install Visual Basic and resides in the \Wizards\PDWizard subdirectory of the main Visual Basic directory.


For a standard package, the information from the .dep files is written to a
Setup.lst file that is stored outside the packaged .cab file. For an
Internet package, the .dep file information is written to an .inf file that
is stored within the packaged .cab file.

The Setup Toolkit is a project installed with Visual Basic that is used by
the Package and Deployment Wizard when it creates a setup program,
setup1.exe. By default, the Setup Toolkit uses the \Program Files
directory as the root location. When installing a file on the
user's machine, you should not copy an older version of the file over a new
version.

The Setup.lst file describes all the files that must be installed on the
user's machine for your application and contains crucial information for the
setup process.



Internet Distribution



The Setup Wizard will create a .cab file, known as the primary .cab file,
which contains:
•The project components, such as the ActiveX control, ActiveX DLL, or
ActiveX EXE •The .inf file, which contains links to other .cab files that
contain Visual Basic support files and controls, as well as other
information, such as whether the control is safe for scripting and safe for
initialization and registry information as defined by the user. This file
replaces the Setup.lst file that the Setup Wizard creates in the standard
setup. •Reserved space for digital signatures. •All files that are not in
other (secondary) .cab files. Secondary .Cab Files: For ActiveX control
projects, ActiveX EXEs, and ActiveX DLLs, all run-time components such as
Msvbvm50.dll, individual controls, Data Access Objects (DAO), and Remote
Data Objects (RDO) are packaged into separate .cab files, digitally signed
by Microsoft, and placed on the Microsoft Web site. You can choose to link
your files to the .cab files on the Microsoft Web site, or you can download
local copies of them.


When you compile a ActiveX control with licensing support. The .VBL file
will contain licensing info(use the "Requiere License"Key option, located
in the General tab of the Project Properties dialog box.)

Only an ActiveX control can be used with the Create Download Setup option
in the Setup Wizard.

ActiveX documents use the NavigateTo method to get to URL Adresses on WWW.
The ActiveX document must be in a hyperling-aware containter and in a
container that maintains a history of documents to use the "goBack"
and "goForward" methods to move between previously visited documents.



Advantages of Using ActiveX Documents:
••Support for the Hyperlink object •Support for the AsyncRead method


The benefit of using secondary .cab files from a Web site are:

You do not need to distribute all of the .cab files required by your
application. The only file you need to distribute is the primary .cab file.
The .inf file within the primary .cab file points to the Microsoft Web site
and downloads the necessary .cab files based on the needs of the end user.
They provide an efficient means of delivering updates to your product.
If you cannot or do not want your application setup to require a connection
to the Internet, you may place the secondary .cab files on a ser<

    •Create a file with a name supplied by a script. •Read a file from the
    user's hard drive with a name supplied by a script. •Insert information
    into the Windows Registry (or into an .ini file), using a key (or filename)
    supplied by a script. •Retrieve information from the Windows Registry

    (or from an .ini file), using a key (or filename) supplied by a script. •
    Execute a Windows API function using information supplied by a
    script. •Create or manipulate external objects using programmatic IDs
    (for example, "Excel.Application") that the script supplies.


    A safe for initialization control does not write or modify any registry
    entries, .ini files, or data files as a result of initialization parameters.

    The downloaded license key is not added to the registry. Instead, browser
    asks the control to create a run-time instance of itself, and passes it the
    downloaded license key.

    Only the DBGrid control requires a license key on a Web server.

    There is a fine line between a safe and unsafe action. For example,
    a control that always writes information to its own registry entry may be
    safe. A control that allows you to name the registry entry is unsafe.
    A control that creates a temporary file with a name it creates without using
    any initialization or scripting value may be safe. A control that allows the
    name of the temporary file to be assigned at initialization or by scripting
    is unsafe.

    A control marked safe for initialization guarantees to do nothing bad no
    matter what data it is initialized with The critical issue is what may
    happen as a result on initialization with particular values. A control that
    automatically sends information at initialization to an HTTP server may be
    safe, as long as the server address cannot be changed. You may create files
    and any other activities needed for the control as long as the file
    properties (name, file size) or activity’s nature does not change becau

    MSDN, "Deploying ActiveX Controls on the Web with the Internet Component
    Download"

Your Title