Share via


Visual Basic Concepts

Data Types Allowed in Properties and Methods

Classes can have properties and methods of any public data type supported by Automation. This includes all arguments of properties and methods, as well their return values. The allowed data types include:

  • Public objects provided by another component, such as DAO or a component authored using Visual Basic.

  • Public objects provided by Visual Basic for applications, such as the Error and Collection objects.

  • Objects defined in public classes in the component.

  • Public enumerations declared in public class modules.

  • Standard system data types defined by Automation, such as OLE_COLOR and OLE_TRISTATE.

  • The intrinsic data types provided by Visual Basic.

  • User-defined types.

On the Evils of Returning Private Objects

The following data types are not allowed, and references to them should never be returned to client applications:

  • All of the objects provided in the Visual Basic (VB) object library — for example, controls. Use the Object Browser to view the entire list.

  • All forms.

  • All class modules whose Instancing property is set to Private.

  • References to ActiveX controls.

Visual Basic prevents you from passing non-visual private objects to or from out-of-process components. Attempting to do so causes error 98, "A property or method call cannot include a reference to a private object, either as an argument or as a return value." This error is always received by the client.

In other cases, it is possible to trick Visual Basic and pass private objects to client programs. Don’t do this. References to private objects will not keep a component running.

If your component shuts down, because all references to your public objects have been released, any remaining private objects will be destroyed, even if clients still hold references to them.

Subsequent calls to the properties and methods of these objects will cause errors, in the case of out-of-process components. In the case of in-process components, a fatal program fault may occur in the client.

Important   Private objects are private for a reason, usually because they were not designed to be used outside your project. Passing them to a client may decrease program stability and cause incompatibility with future versions of Visual Basic. If you need to pass a private class of your own to a client, set the Instancing property to a value other than Private.

For More Information   "Building Code Components" shows how events can eliminate the need for public call-back classes, allowing Standard EXE projects to use call-backs without providing public objects or passing out references to private objects.