.NET Questions and Solutions

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 ‘Visual’

BUG: Starting Word Manually Uses Same Instance as Automation

Symptoms
You run a Visual Basic application that uses the CreateObject function tostart a hidden instance of Microsoft Word. The application is idle, but itstill maintains a reference to Word. Next, you manually start an instanceof Word. A separate instance of Word should open, but the same instancethat was created by the Visual Basic application is made active instead. Ifyou close Word and continue to work in the Visual Basic application, one of the following errors occurs when the application tries to use Word objectsbecause Word is no longer running:

Run-time error ‘462′:
The remote server machine does not exist or is unavailable
-or-

Run-time error ‘-2147023174 (800706ba)’:
Automation errorThis automation error translates to “The RPC server is unavailable.”
Resolution
Use one of the following to work around this problem:Before you create your Word object, first create a temporary Wordobject. After you create your object, close the temporary object. Thiscauses Word to act properly when you control it through Automation (thatis, if a user interactively starts Word, a new instance of Word isopened for the user). The automation instance remains hidden andseparate. See the Steps to Reproduce Behavior section for an example ofthis workaround.Make the Word object visible immediately after using the CreateObjectfunction. This workaround is only for Microsoft Word 97. For example:

Set wrdApp = CreateObject(“Word.Application”)wrdApp.Visible = True

BUG: Removing Collection Elements Takes Longer Than Expected

Symptoms
In Visual Basic 6.0, removing elements from the end of a collection takeslonger than removing elements from the beginning.
Resolution
When removing an element from a collection, Visual Basic 6.0 begins at thebeginning of the collection and traverses the collection until the desiredelement is reached, then that element is removed.

BUG: Error message when you try to pass a Collection object from Visual Basic 6.0 components to Visual Basic 2005 or to Visual Basic .NET: “System.InvalidCastException”

Symptoms
When you try to pass a Collection object fromMicrosoft Visual Basic 6.0 components to Microsoft Visual Basic 2005 or to Microsoft Visual Basic .NET, you may receive an error message. In Microsoft Visual Studio 2005, you receive the following error message:

An unhandled exception of type ‘System.InvalidCastException’ occurred in ApplicationName.exe
Additional information: Unable to cast object of type ‘Microsoft.VisualBasic.Collection’ to type ‘VBA.Collection’.In Microsoft Visual Studio .NET, you receive the following error message:

An unhandled exception of type ‘System.InvalidCastException’ occurred in ApplicationName.exe
Additional information: Specified cast is not valid.If you examine the type of the collection object that Visual Basic 2005 or Visual Basic .NET expects, you find that Visual Basic 2005 or Visual Basic .NET expects the VBA.Collection type instead of the Microsoft.VisualBasic.Collection type. If you change your code to pass a collection object of the VBA.Collection type, you receive the following error message on the line of code where you try to create a new instance of the VBA.Collection class:

An unhandled exception of type ‘System.Runtime.InteropServices.COMException’ occurred in ApplicationName.exe
Additional information: COM object with CLSID {A4C4671C-499F-101B-BB78-00AA00383CBB} is either not valid or not registered.This problem also occurs in other Microsoft .NET Framework-supported languages such as Microsoft Visual C# 2005 and earlier versions of .NET Framework-supported Microsoft Visual C#.
Resolution
The InvalidCastException error occurs because the Microsoft.VisualBasic.Collection type is incompatible with the VBA.Collection type. The COMException error occurs because only a Visual Basic 6.0 application can create an instance of the VBA.Collection class. You cannot create an instance of the VBA.Collection class outside a Visual Basic 6.0 application.

BUG: “System.Runtime.Serialization.SerializationException” exception if you declare a static local variable in a method of a Visual Basic .NET or Visual Basic 2005 class and try to serialize the …

Symptoms
Microsoft Visual Basic .NET or Microsoft Visual Basic 2005 allows you to declare static local variables inside a method of class. However, if you declare a static local variable in a method of the Visual Basic .NET or Visual Basic 2005 class, and then you try to serialize the object of the class by using BinaryFormatter or SoapFormatter, you receive the following exception during run time:

An unhandled exception of type ‘System.Runtime.Serialization.SerializationException’ occurred in mscorlib.dll
Additional information: The type Microsoft.VisualBasic.CompilerServices.StaticLocalInitFlag in Assembly Microsoft.VisualBasic, Version=7.0.3300.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a is not marked as serialized.
Resolution
The common language runtime does not support static variables in methods. When Visual Basic .NET or Visual Basic 2005 compiler compiles your code, it translates this high-level Visual Basic .NET or Visual Basic 2005 code to Microsoft intermediate language (MSIL) code that the common language runtime can understand to give you shared variable functionality. Visual Basic .NET or Visual Basic 2005 uses the Microsoft.VisualBasic.CompilerServices.StaticLocalInitFlag class to provide this functionality. In Visual Basic .NET or in Visual Basic 2005 compiler, the StaticLocalInitFlag is not marked as serialized. Therefore, you receive the exception during run time.

BUG: “Object variable or With block variable not set” error message when you access a public object variable

Symptoms
When you set a public object variable of a Microsoft Component Object Model (COM) component in Microsoft Visual Basic .NET or in Microsoft Visual Basic 2005, you may receive the following error message:

An unhandled exception of type ‘System.Runtime.InteropServices.COMException’ occurred in microsoft.visualbasic.dll
Additional information: Object variable or With block variable not set
Resolution
This behavior can occur if all the following conditions are true: You define a public variable in the COM component.The variable is of the Object type.You reference this COM component by using late binding in Visual Basic .NET or in Visual Basic 2005. When you access the public object by using late binding, Visual Basic .NET or Visual Basic 2005 does not correctly set the BindingFlags enumeration.
The BindingFlags enumeration is used to specify the flags that control binding and the way in which the search for members and types is conducted by reflection.

BUG: “Index was out of range” error message when you access a Visual Basic .NET 2002 collection object that implements IList with -1 Base

Symptoms
When you use the IList interface with a Visual Basic .NET collection object in Visual Basic .NET (2002) before you use the Insert property, you can insert elements in the list at index -1. If you insert an element at index -1, you receive the following error message when you try to read the element by using the Item property at index -1:

System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Resolution
The IList index has a base of -1 instead of zero. Therefore, you can insert an element at index -1 even though MSDN documentation states that the IList index is zero-based. However, you receive an error when you try to read the value from the IList at index -1.