|
|
Implementing IClonable - Shallow copies
|
Total Hit (2597) |
An object that want to support cloning should implement the ICloneable interface. This interface exposes only one method, Clone, which returns a copy of the object. The ICloneable specifics don't specify whether the object that the Clone method returns is a shallow copy or a deep copy. The differenc
....Read More |
Rating
|
|
|
Implementing ICloneable - Deep copies
|
Total Hit (3433) |
The simplest way to create a generic deep copy routine, that is a procedure that can create a true, distinct copy of an object and all its dependent object, is to rely on the serialization features of the .NET framework.
«Code LangId=2»
Function CloneObject(ByVal obj As Object) As Object
'
....Read More |
Rating
|
|
|
Set up event handlers through reflection
|
Total Hit (2592) |
Reflection makes it easy to invoke a method (or assign a field or a property) by its name. For example, suppose you have the following classes:
«Code LangId=2»
Class Person
Event PropertyChanged(ByVal propertyName As String, ByVal newValue As Object)
Dim m_Name As String
Property
....Read More |
Rating
|
|
|
Always declare objects with full library name
|
Total Hit (2910) |
If your application uses objects from external components, either third-party or your own libraries, a good rule of thumb is to include the complete servername.classname string in the Dim statement, as in:
«Code LangId=1»
Dim word As Word.Application
«/Code»
This syntax ensures that if you th
....Read More |
Rating
|
|
|
Always run a component using Full-Compile
|
Total Hit (2799) |
When testing a component in the IDE, always perform a full compilation. This ensures that VB checks the syntax of all the code in the component, which in turn guarantees that no syntax error will break the program while it is serving another application.
You can activate the full compilation opti
....Read More |
Rating
|
|
|
Check a GUID
|
Total Hit (2961) |
The following routine quickly check that a string contains a valid GUID. Of course, it doesn't check that the GUID refers to a valid entity, but at least it lets you quickly reject invalid values:
«Code LangId=1»
Function CheckGUID(Value As String) As Boolean
Const PatternGUID = "{#######
....Read More |
Rating
|
|
|
Compound member attributes
|
Total Hit (2754) |
The Procedure Attributes dialog includes a Procedure ID combo box, that lets you associate a particular ID to a given member of the class. You usually use this combo to make a property or a method the default item of a class or an ActiveX control, but there are other uses as well. For instance, you
....Read More |
Rating
|
|
|
Correct usage for binary compatibility settings
|
Total Hit (3044) |
When working on an updated version of a COM component, you should always enforce Binary Compatibility in the Component tab of the Project Properties dialog box. When enforcing binary compatibility there are a number of common mistakes that you should stay clear of:
Always use the last EXE or DLL
....Read More |
Rating
|
|
|
Create a GUID
|
Total Hit (3933) |
When you build your ActiveX controls and components, Visual Basic automatically creates all the GUIDs as necessary. The same also happens in other cases, without you even realizing it: for instance when you make a MDB database replicable, the Jet Engine adds new fields and uses GUIDs to mark their c
....Read More |
Rating
|
|
|
Create stand-alone type libraries
|
Total Hit (3182) |
Many VB programmers assume that Visual Basic 5.0 is not capable of creating stand-alone Type Libraries, because the documentation states that when an ActiveX component or control is created, the companion Type Library is embedded in the main executable file.
This is correct, but if you own the E
....Read More |
Rating
|
|
|
Creating help string for Enum constants
|
Total Hit (3072) |
I've been trying to find a way to assign helpstrings for Enums in Visual Basic. The Class builder utility does that only for methods, events, and properties, but not for Enums.
The IDL source code that corresponds to an Enum in a type library looks something like follws, and you can run the MIDL
....Read More |
Rating
|
|
|
Don't include Extender properties in ActiveX Wizard
|
Total Hit (3169) |
The left-most listbox in the first page in the ActiveX Control Interface Wizard includes all the properties exposed by the constituent controls currently on the UserControl's surface. Unfortunately, this list includes Extender properties, methods, and events, which should be never added to the publi
....Read More |
Rating
|
|
|
Don't pass Private objects to client applications
|
Total Hit (2903) |
Under some circumstances, an ActiveX DLL can pass private objects to its client application, for example a reference to a control that belongs to a form in the DLL. While this approach can be useful, and can work under some circumstances, you should absolutely avoid avoid to do so, because it might
....Read More |
Rating
|
|
|
Enum constants that include spaces
|
Total Hit (2870) |
If you're writing an ActiveX control, you can create properties that return an enumerated value, as in:
«Code LangId=1»
Public Enum SizeConstants
SizSmall = 1
SizMedium
SizLarge
End Enum
Public Size As SizeConstants
«/Code»
When another developer is using your control, an enu
....Read More |
Rating
|
|
|
Get the Command$ value from inside an ActiveX DLL
|
Total Hit (3169) |
At times you may need to access the command line passed to the application from within an ActiveX DLL. Unfortunately, when inside a DLL the Command$ function returns a null string, so you have to resort to some API trickery:
«Code LangId=1»
Private Declare Function GetCommandLine Lib "kernel32"
....Read More |
Rating
|
|
|
Hide the Automation Manager
|
Total Hit (3420) |
If you haven't switched to DCOM yet, and still use Remote OLE Automation, you must launch the Automation Manager program on the server machine, in order to let the server respond to requests coming from client workstations.
The Automation Manager displays a visible window at launch time, which t
....Read More |
Rating
|
|
|
Never mix SingleUse and MultiUse classes
|
Total Hit (2830) |
ActiveX components written in VB can contain both SingleUse and MultiUse classes at the same time. It usually isn't a good idea to use both types of classes in the same project, however.
When a client creates an instance of a SingleUse class, COM always runs a new instance of the EXE file that ex
....Read More |
Rating
|
|
|
Storing objects in the Tag property
|
Total Hit (4542) |
The Tag property exposed by many Windows Common Controls (including TreeView's Node objects and ListView's ListItem objects) is more versatile than the ListBox and ComboBox controls' ItemData property, because it is of Variant type and can therefore accept values of most data types.
However, it
....Read More |
Rating
|
|
|
Updating records in DataList and ADO Data Controls
|
Total Hit (4000) |
If you use a DataList control linked to an ADO Data Control and if you want to add a record, you have to create a command button with this code:
«Code LangId=1»
Private Sub cmdAdd_Click()
On Error GoTo AddErr
datPrimaryRS.Recordset.AddNew
txtNew.SetFocus
Exit Sub
AddErr:
MsgBox
....Read More |
Rating
|
|
|
A template for building collection class modules
|
Total Hit (3640) |
The following is a template for building collection class modules in a very quick and effective way. Copy-and-paste this code into Notepad, that save it to a file named "COLLECTION CLASS.CLS" in the \TEMPLATE\CLASSES subdirectory under the main VB6 directory:
«Code LangId=1»
VERSION 1.0 CLASS
B
....Read More |
Rating
|
|
|
Better type checking in Variant properties that can accept objects
|
Total Hit (2911) |
As explained in the Mistake Bank you need all three flavors of Property procedures to correctly implement a Variant property that can take either an object or a non-object value. Surprisingly enough, in this case you don't need that the argument of the Property Set procedure be declared As Variant.
....Read More |
Rating
|
|
|
Default Properties tend to hide programming mistakes
|
Total Hit (2857) |
Visual Basic lets you create a default property or method by simply selecting the "(Default)" item in the combo box that appear if you click the Advanced button in the Procedure Attributes dialog box. (You can display this dialog from the Tools menu, or by right-clicking on a property name in the ri
....Read More |
Rating
|
|
|
Don't test auto-instancing variables using Is Nothing
|
Total Hit (2988) |
You can't reliably test an auto-instancing object variable using the Is Nothing test, because as soon as you reference the variable Visual Basic silently creates an object of the given class, and the test always returns False:
«Code LangId=1»
Dim x As New MyClass
If x Is Nothing Then
' this
....Read More |
Rating
|
|
|
Friend Procedures are faster than Public ones
|
Total Hit (3023) |
It might surprise you to learn that Friend procedures are sensibly faster than Public ones. You can prove this yourself by creating an ActiveX EXE project with one Private class and one Public class (Instancing = MultiUse), and then add the following code in both class modules:
«Code LangId=1»
P
....Read More |
Rating
|
|
|
Implement Write-Once Read-Many Properties
|
Total Hit (4071) |
Creating a read-only property or a write-only property isn't difficult, as you probably know: just omit the Property Let (or Set, if dealing with objects) or the Property Get procedure, respectively.
There are cases, however, when you want to implement a write-only-read-many property, that is, a
....Read More |
Rating
|
|
|
Passing Public class variables by reference
|
Total Hit (2995) |
Under VB4 Public variables in form and class modules were implemented in the same manner as variables in BAS modules, that is, as direct references to memory locations. This allowed VB programmers to create procedures that modified values passed by reference, as in:
«Code LangId=1»
'--- the CIn
....Read More |
Rating
|
|
|
Use Objptr function to quickly locate an object in a Collection
|
Total Hit (3190) |
One of the simplest and most effective uses of the ObjPtr function is to provide a key for quickly locating an object in a Collection. Let's suppose you have a collection of objects that don't have a property that can be used as a key to retrieve them in a collection. You can work around this by usi
....Read More |
Rating
|
|
|
|
Using CallByName with nested objects
|
Total Hit (3196) |
Sometimes Microsoft documentation can be, well lacking is a kind word, and one is reluctant to call tech support when they smoke your credit card first, and ask questions later.
My problem was that the CallByName procedure was refusing to call nested lasses. To see what I mean, consider that if y
....Read More |
Rating
|
|