Visual Basic Q&A

As a software engineer, I focus on .NET, especially asp.net, C#, WCF and so on, and I am also very interested in Search Engine Optimization.

Entries Tagged ‘microsoft visual basic 6’

BUG: The Visual Basic .NET or Visual Basic 2005 Upgrade Wizard reports an incorrect warning message for user-defined data types

Symptoms
If you use Visual Basic Upgrade Wizard on your Microsoft Visual Basic 6.0 project with a user-defined data type, the wizard generates a warning message for the code with user-defined data type assignments. For example, you may receive the following warning message for a direct user-defined data type assignment.

UPGRADE_WARNING: Could not resolve default property of object <<UDT>>. Click for more information: ‘ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword=”vbup1037′
Resolution
The wizard incorrectly treats the user-defined data type variables as a class object, and then tries to locate the default property for the object. The user-defined data type does not have default properties. Therefore, the wizard cannot locate a default property, and it reports an incorrect warning.

BUG: Error at run time after you upgrade a Microsoft Visual Basic 6.0 project that uses the ImageList control to Microsoft Visual Basic .NET

Symptoms
When you upgrade a Microsoft Visual Basic 6.0 project that uses the ImageList control to a Microsoft Visual Basic .NET project by using the Microsoft Visual Basic .NET Upgrade Wizard, you receive the following exception when you run the project in Visual Basic .NET:

An unhandled exception of type ‘System.Runtime.InteropServices.COMException’ occurred in axinterop.mscomctllib.dll
Resolution
The Visual Basic .NET Upgrade Wizard wraps the ImageList control in the AxHost class. However, it does not modify the corresponding Visual Basic 6.0 code to access the underlying ImageList control. For example, the following code may be part of the migrated Visual Basic .NET code.

ListView1.ColumnHeaderIcons = ImageList

BUG: Design Option in Pop-up Menu Data View Window Not Available

Symptoms
If you use a Windows NT 4.0 computer and are not a member of the administrators group, you will not get the Design option in the pop-up menu for a table when you right-click on on the table in the Data View Window of Microsoft Visual Basic 6.0.
NOTE: This bug does not occur on Windows 2000 machines.
Resolution
The Visual Basic IDE is incorrectly determining that the Design option should not be shown unless you belong to the administrators group.

BUG: “Public Overrides WriteOnly Property…” error when you try to override a Microsoft Visual Basic 6.0 property in Microsoft Visual Basic .NET

Symptoms
You have a Microsoft Visual Basic 6.0 class that has a property with the ByRef parameter. When you inherit the class in Visual Basic .NET and override the property, you receive the following compilation error:

‘Public Overrides WriteOnly Property myProp() As System.IntPtr’ cannot override ‘Public Overridable Overloads WriteOnly Property myProp() As System.IntPtr’ because they differ by their return types.
Resolution
The .NET runtime compares the return types of the parameters in the base class and inherited class property. This comparison returns a difference in the return types while it compares the symbols for System.IntPtr and System.Int16. Two separate symbols represent these internally. This results in an error.

How to call a Visual Basic .NET or Visual Basic 2005 assembly from Visual Basic 6.0

Symptoms
This article describes how use Microsoft Visual Basic .NET or Microsoft Visual Basic 2005 to build a managed assembly that can be called from Microsoft Visual Basic 6.0.
Resolution
Guidelines for exposing .NET types to COMWhen you want to expose types in a Microsoft .NET assembly to Component Object Model (COM) applications, consider the following COM interop requirements at design time. Managed types (class, interface, struct, enum, and others) interact well with COM client applications when you follow these guidelines: Define interfaces and explicitly implement them in classes. COM interop provides a mechanism to automatically generate an interface that contains all members of the class and the members of its base class. However, it is best to provide explicit interfaces and implement them explicitly.Declare all managed types that you want to expose to COM as public. Only public types in an assembly are registered and exported to the type library. Therefore, only public types are visible to COM. Declare all type members (methods, properties, fields, and events) that you want to expose to COM as public. Members of public types must also be public to be visible to COM. By default, all public types and members are visible. Use the ComVisibleAttribute attribute if you have to hide a type or a member from control type or member visibility to COM client applications.Types must have a public default constructor to be instantiated through COM. Managed, public types are visible to COM. However, without a public default constructor (a constructor without arguments), COM clients cannot create an instance of the type. COM clients can still use the type if the type is instantiated in another way and the instance is returned to the COM client. You may include overloaded constructors that accept varying arguments for these types. However, constructors that accept arguments may only be called from managed (.NET) code.Types cannot be abstract. Neither COM clients nor .NET clients can create instances of abstract types.Use the COMClass template in Visual Basic .NET or in Visual Basic 2005. When you add a new class that you intend to expose to COM applications, consider using the COMClass template that is provided by Visual Basic .NET or by Visual Basic 2005. The COMClass template creates a class that includes the COMClassAttribute attribute and generates GUIDs for the CLSID, the Interface ID, and the Event ID that are exposed by your type. Additionally, the COMClass template creates a public constructor without parameters. This is the easiest way to create a new class that follows the guidelines for creating COM callable types.
Registering the .NET assembly for COM interop and creating a type library For Visual Basic 6.0 to successfully interact with a managed component, you must register the assembly for COM interop and generate a type library. This registration must be performed on each computer where a COM client application interacts with the assembly. The type library provides type information about the exposed types in the assembly to COM client applications. The process for doing this depends on if you are working on the development computer or on the destination computer.
On the development computer, Microsoft Visual Studio .NET or Microsoft Visual Studio 2005 automatically creates a type library and registers it during the build process if the Register for COM Interop check box is selected under the project’s Configuration properties. If you used the COMClass template when you created the class, Visual Studio .NET or Visual Studio 2005 automatically selects the Register for COM Interop check box. To verify that the Register for COM Interop check box is selected in Visual Studio .NET or in Visual Studio 2005, follow these steps: Start Visual Studio .NET or Visual Studio 2005.Open the solution that contains the project that you want to build for COM interop.On the View menu, click Solution Explorer.In Solution Explorer, right-click the project that you want to build for COM interop, and then click Properties.Click Configuration Properties, and then click the Build node.
Note In Visual Studio 2005, click Compile in the left pane.Click to select the Register for COM Interop check box. This option is only enabled in class library projects.Click OK to close the Property Pages dialog box.If Visual Studio .NET or Visual Studio 2005 is not installed or if you have to manually generate and register a type library (.tlb) file for the managed assembly, use the Assembly Registration tool (RegAsm.exe) with the /TLB switch. You should also use the /Codebase switch if the managed assembly is a private assembly and you intend to put the managed assembly in a different folder from the host process (EXE).
A private assembly is deployed with an application and is available for the exclusive use of that application. Other applications do not share the private assembly. Private assemblies are designed to be installed into the same folder as the host process (EXE). With a COM client application, this means that the assembly is located in the same folder as that application. A shared assembly is available for use by multiple applications on the computer. To create a shared assembly, you must sign the assembly with a strong name and install the assembly into the Global Assembly Cache (GAC) on the destination computer.
For more information about how to sign the assembly with a strong name and install the assembly into the Global Assembly Cache (GAC), visit the following Microsoft Web site:
http://msdn2.microsoft.com/en-us/library/xc31ft41(vs.71).aspx(http://msdn2.microsoft.com/en-us/library/xc31ft41(vs.71).aspx)You should use both the /tlb: switch and the /Codebase switch when you register the assembly. The /tlb: switch generates and registers a type library, and the /Codebase switch registers the location of the managed assembly in the Windows registry. If you do not use the /Codebase switch and the assembly has not been installed into the Global Assembly Cache (GAC), you must put a copy of the assembly into the folder of each COM client application (EXE) so that the assembly can be located by the common language runtime (CLR).
To generate and register a type library and register the location of the managed assembly, type the following command at the command prompt:
Regasm AssemblyName.dll /tlb: FileName.tlb /codebaseCreate a COM callable assembly inVisual Basic .NETStart Visual Studio .NET or Visual Studio 2005. On the File menu, point to New, and then click Project.Under Project Types, click Visual Basic Projects.
Note In Visual Studio2005 click Visual Basic under Project Types.Under Templates, click Class Library.Name the project TestProj, and then click OK.
By default, Class1 is created.On the View menu, click Solution Explorer.Right-click Class1.vb, and then click Delete. Click OK to confirm the deletion of the Class1.vb source file.On the Project menu, click Add Class.Under Templates, click COM Class.Name the class COMClass1.vb, and then click Open
COMClass1 is created with the following code.

<ComClass(ComClass1.ClassId, ComClass1.InterfaceId, ComClass1.EventsId)> _Public Class ComClass1#Region “COM GUIDs”‘ TheseGUIDs provide the COM identity for this class’ and its COM interfaces. If you change them, existing’ clients will no longer be able to access the class.Public Const ClassId As String = “6DB79AF2-F661-44AC-8458-62B06BFDD9E4″Public Const InterfaceId As String = “EDED909C-9271-4670-BA32-109AE917B1D7″Public Const EventsId As String = “17C731B8-CE61-4B5F-B114-10F3E46153AC”#End Region’ A creatable COM class must have a Public Sub New()’ without parameters. Otherwise, the class will not be’ registered in the COM registry and cannot be created’ through CreateObject.Public Sub New()MyBase.New()End SubEnd ClassAdd the following function to COMClass1.

Public Function myFunction() As IntegerReturn 100End FunctionIn Solution Explorer, right-click Project Name, and then click Properties.Under Configuration Properties, click Build.Verify that the Register for COM Interop check box is selected, and then click OK. On the Build menu, click Build Solution to build the project.Start Visual Basic 6.0.On the File menu, click New Project, and then click to select Standard EXE in the New Project dialog box.
By default, a form that is named Form1 is created.On the Project menu, click References. In the Available References list, double-click to select TestProj, and then click OK.Add a command button to the form.Double-click Command1 to open the Code window. Add the following code to the Command1_Click event.

Dim myObject As TestProj.COMClass1Set myObject = New TestProj.COMClass1MsgBox myObject.myFunctionOn the Run menu, click Start.Click the command button.
You should receive a message that displays 100.

FIX: Visual Basic .NET 2002 Upgrade Wizard stops responding with a project containing a UserControl

Symptoms
When you attempt to upgrade a Microsoft Visual Basic 6 project to Visual Basic .NET, and the project contains a UserControl, the Upgrade Wizard may stop responding (hang) or throw an exception.
Specifically, when you perform the upgrade on a Microsoft Windows XP-based computer, the Upgrade Wizard may stop responding. When you upgrade on a Microsoft Windows 2000-based computer, you may receive the following error message:

The exception unknown software exception (0×00000fd) occurred in the application at location 0×20078c3e NOTE: The Visual Basic .NET Upgrade Wizard is included in Visual Studio .NET Professional.
Resolution
This behavior can occur if an instance of the UserControl cited on a form has the same name as the project.