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 ‘visual studio 2005’

BUG: You receive a “Class not registered” error message in Visual Basic 6.0 project after you remove Visual Studio .NET or Visual Studio 2005

Symptoms
You have Visual Studio .NET or Visual Studio 2005 installed on your computer. You create a Visual Basic 6.0 project that has a control that is bound to a data source. When you remove Visual Studio .NET or Visual Studio 2005 and then run you Visual Basic 6.0 project, you receive the following error message:

Run-time error ‘713′:
Class not registered.
Looking for object with
CLSID:{59245250-7A2F-11D0-9842-00A0C91110ED}
Resolution
This problem occurs because the Component Object Model (COM) based MSBind.dll is unregistered when you remove Visual Studio .NET or Visual Studio 2005. Visual Basic 6.0 uses MSBind.dll to bind the control to the data source. Therefore, when you run a Visual Basic 6.0 project that has a data-bound control, you receive the run-time error message.

An “could not copy temporary files to the output directory” error is generated in Visual Studio .NET or in Visual Studio 2005

Symptoms
When you build a project in Microsoft Visual Studio .NET or in Microsoft Visual Studio 2005, the build fails and you receive the following compile-time error message:

Could not copy temporary files to the output directory. For more information about another cause for this symptom, click the following article number to view the article in the Microsoft Knowledge Base:
313512?(http://support.microsoft.com/kb/313512/) BUG: “Could not copy temporary files to the output directory” error message when you build a solution that contains multiple projects
Resolution
This behavior may occur for one of the following reasons.
Cause 1You may receive this error message when you compile a component that is currently being used by another process. For example, you may try to compile a class library or a user control project. For example, this behavior may occur when the following conditions are true: Multiple instances of Visual Studio .NET or Microsoft Visual Studio 2005 are open. In one instance, you have a UserControl or class library project. In the other instance, you have a project that references the component and the project of the component is open in the first instance.The second instance uses a file reference to the assembly and the Copy Local property of the assembly is set to False.The assembly has been loaded into memory of the second instance of Visual Studio .NET or Microsoft Visual Studio 2005.If the Copy Local property is set to False for the referenced assembly, and the assembly is not installed in the global assembly cache, the assembly will not be copied to the local \Bin folder of the consuming project. To resolve the location of the referenced assembly, Visual Studio .NET or Microsoft Visual Studio 2005 will search the paths that are listed in the Reference Path property of the project until it finds the assembly. In a scenario where the assembly has been loaded, such as a UserControl loaded in the Windows Form designer, the assembly is being used and cannot be overwritten when you try to build it. When this behavior occurs, you receive the error message that is mentioned in the “Symptoms” section.
As soon as the assembly has been loaded in memory of the Visual Studio .NET or Microsoft Visual Studio 2005 IDE, the assembly will not be unloaded until you exit and then restart the Visual Studio .NET or Microsoft Visual Studio 2005 IDE. For more information, see the “Resolution” section.
Cause 2 This behavior also occurs if you try to build the application when you are currently running an instance of the application from its \Bin folder, externally of the Visual Studio .NET or Microsoft Visual Studio 2005 IDE.
During the build process, the compiler builds the project to an intermediate folder, such as Obj\configname. In this folder name, configname is the name of the project. When the build process has completed, the files are copied from the intermediate folder into the output folder. The “Could not copy temporary files to the output directory” error message indicates that the compiler could not copy these files from the intermediate folder to the output folder. If you run the application that is being built from the output directory, the compiler cannot overwrite these files because they are currently being used. For more information, see the “Resolution” section.

“Unable to set the next statement to this location” error message when you are debugging a Visual Studio 2005 application

Symptoms
When you are debugging a Microsoft Visual Studio 2005 application and you try to set the next statement, you may receive an error message that resembles the following error message:

Unable to set the next statement to this location. There is no executable code at this location in the source code.
Resolution
The Debugger Engine is picking the statements in sequence. As long as the caret is at the end of the line, the Debugger Engine will set the next statement on the next line. If there is no executable code from the next line, an error message will occur.

How to load an assembly at runtime that is located in a folder that is not the bin folder of the application

Symptoms
You do not have to put an assembly that an application must use at runtime in the bin folder of the application. You can put the assembly in any folder on the system, and then youcan refer to the assembly at runtime.
Resolution
This step-by-step article describes three methods that you can useto refer to the assemblies that are located in folders that are not the bin folder of the application.
RequirementsThis article assumes that you are familiar with the following topics: General familiarity with Microsoft Visual Basic .NET or Microsoft Visual Basic 2005 or with Microsoft Visual C# .NET or Microsoft Visual C# 2005General familiarity with assemblies in Visual Basic .NET or Visual Basic 2005and in Visual C# .NET or Microsoft Visual C# 2005General familiarity with .config files in Visual Basic .NET or Visual Basic 2005 and in Visual C# .NET or Microsoft Visual C# 2005The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:The Microsoft .NET FrameworkMicrosoft Visual Studio .NET or Microsoft Visual Studio 2005
Method 1: Install the assembly in the global assembly cache (GAC)The GAC is a computer-wide code cache where the common language runtime is installed. The GAC stores assemblies that you specifically designate to be shared by several applications.
Note You can only install strong-named assemblies in the GAC.
To install an assembly in the GAC, follow these steps: StartVisual Studio .NET or Visual Studio 2005. On the File menu, point to New, and then click Project.
The New Project dialog box appears.Under Project Types, click Visual Basic .NET, or click Visual C# .NET.
Note In Visual Studio 2005, click Visual Basic or click Visual C#.Under Templates, click Class Library. In the Name box, type MyAssembly1.In the Location box,type C:\Myassemblies.
By default, the Class1.vb file is created byVisual Basic .NET or Visual Basic 2005. By default, the Class1.cs file is created byVisual C# .NET or Visual C# 2005. Add the following code tothe Class1 class of the Class1.vb file or of the Class1.cs file.
Visual Basic .NET or Visual Basic 2005 code

Public Function HelloWorld() As StringReturn “From Class Library “End FunctionVisual C# .NET or Visual C# 2005 code

public string HelloWorld(){ return “From Class Library”;}On the File menu,click Save All to save the solution.Install the MyAssembly1 assembly in the GAC.
For more information about how to do this in Visual Basic .NET, click the following article number to view the article in the Microsoft Knowledge Base:
315682?(http://support.microsoft.com/kb/315682/) How to install an assembly in the global assembly cache in Visual Basic. NETFor more information bout how to do this in Visual C# .NET, click the following article number to view the article in the Microsoft Knowledge Base:
815808?(http://support.microsoft.com/kb/815808/) How to install an assembly into the global assembly cache in Visual C# .NETCreate a new client application. To do this,follow these steps:In Visual Studio .NET or Visual Studio 2005, create a new Visual Basic .NET or Visual Basic 2005 Windows application or a new Visual C# .NET or Visual C# 2005 Windows application that is named TestClient1.
By default, the Form1.vb file is created by Visual Basic .NET or Visual Basic 2005. By default,the Form1.cs file is created by Visual C# .NET or Visual C# 2005.In Solution Explorer, right-click Add Reference.
The Add Reference dialog box appears.Click Browse, locateC:\MyAssembly, click the MyAssembly1 assembly, and thenclick Open.
Note In this step, C:\MyAssembly is a placeholder for theactual location of the MyAssembly1 assembly.Add the following code to the Form1_Load event of the Form1.vb file or of the Form1.cs file as follows:
Visual Basic .NET or Visual Basic 2005 code

Dim obj1 As New MyAssembly1.Class1()MessageBox.Show(obj1.HelloWorld())Visual C# .NET or Visual C# 2005 code

MyAssembly1.Class1 obj1=new MyAssembly1.Class1();MessageBox.Show(obj1.HelloWorld());On the Debug menu, click Start to build and to run the application.
Method 2: Use an application configuration (.config) file with the <codeBase> tagsA .config file containsthe following settings:Settings that are specific to an applicationSettings that the common language runtime reads, such as the assembly binding policy settings and the remoting objects settingsSettings that the application readsThe <codeBase> tags specify where the common language runtime can find an assembly. The common language runtime applies the settings of the <codeBase> tags from the .config file.The settings of the <codeBase> tags determine the version and the location of the assembly.
To use a .config file with the <codeBase> tags to refer to the assemblies, follow these steps:Create a new Class Library project that isnamed MyAssembly2 by following steps 1 through6 of the “Method 1: Install the assembly in the global assembly cache (GAC)” section.Make the assembly strong-named.
For additional information about how to do this, click either of the article numbers that are mentioned in step 8 of the “Method 1: Install the assembly in the global assembly cache (GAC)” section.Create a new client application. To do this,follow these steps:In Visual Studio .NET or Visual Studio 2005, create a new Visual Basic .NET or Visual Basic 2005Windows application or a new Visual C# .NET or Visual C# 2005 Windows application that is named TestClient2.
By default,the Form1.vb file iscreated by Visual Basic .NET or Visual Basic 2005. By default,the Form1.cs file is created by Visual C# .NET or Visual C# 2005. In Solution Explorer, right-click Add reference.
The Add Reference dialog box appears.Click Browse, click theMyAssembly2 assembly, and then click Open.Under References, right-click MyAssembly2, and then click Properties.
The Properties window appears.In the Properties window, set the Copy Local property of the assembly to False. Add the following code to the Form1_Load event of the Form1.vb file or of the Form1.cs file as follows:
Visual Basic .NET or Visual Basic 2005 code

Dim obj2 As New MyAssembly2.Class1()MessageBox.Show(obj2.HelloWorld())Visual C# .NET or Visual C# 2005 code

MyAssembly2.Class1 obj2=new MyAssembly2.Class1();MessageBox.Show(obj2.HelloWorld());On the Build menu, click Build Solution.Find the publicKeyToken attribute number of the assembly that you created. To do this,follow these steps: At the Visual Studio .NET or Visual Studio 2005 command prompt, locate the following folder:
C:\MyAssemblies\MyAssembly2\bin\debug
Note To find the publicKeyToken attribute number, locatethe folder that containsyour compiled library assembly. Typically, this is the bin folder in your project folder that is mentioned previously in this step.Type the following command:
SN -T MyAssembly2.dll
Note You must use a capital letter “T” to obtain the correct public key.
The command returns a hexadecimal value that represents the publicKeyToken attributenumber of the assembly.To find the version number of the assembly, follow these steps: In Microsoft Windows Explorer, locate the following folder:
C:\Myassemblies\MyAssembly2\bin\debug Right-click the MyAssembly2.dll file, and then click Properties.
The Properties window appears.In the Properties window, click the Version tab.
Note The assembly version is specified in the Value section.Use the publicKeyToken attribute number and the version number to identify the correct assembly.
Note You must provide the publicKeyToken attribute number, the version number, and the path of the MyAssembly2.dll file that uses the <codeBase> tags to refer to theMyAssembly2.dll file at runtime.Adda .config file to the project. To do this,follow these steps:On the Project menu, click Add New Item.In the Add New Item dialog box, click Application configuration file under Templates. Make sure that the file name is App.config, and then click Open.Add the following code to the file:

<configuration><runtime><assemblyBinding xmlns=”urn:schemas-microsoft-com:asm.v1″><dependentAssembly><assemblyIdentity name=”MyAssembly2″culture=”neutral” publicKeyToken=”307041694a995978″/><codeBase version=”1.0.1524.23149″ href=”FILE://C:/Myassemblies/MyAssembly2.dll”/></dependentAssembly></assemblyBinding></runtime></configuration>Note The TestClient2.exe.config file is locatedin the Debug folder or in the Release folder. Both of these folders are locatedin the bin folder. The solution configuration mode that you select determines the location of the TestClient2.exe.config file.Make the following changes in the <assemblyIdentity> tags: Change the name attribute to the name of your library assembly.Change the publicKeyToken attribute to the public key that you determined in step 4 of this section.Make the following changes in the <codeBase> tags: Change the version attribute to the version number of the assembly that you determined in step 5 of this section.Change the href attribute to the path where the DLL is located.On the Debug menu, click Start to build the project,and thenrun the application.
Method 3: Use the AssemblyResolve eventThe AssemblyResolve event fires whenever the common language runtime tries to bind to an assembly and fails. You can use the AddHandler method to add an event handler to the application that returns the correct assembly whenever the AssemblyResolve event fires.
The AssemblyResolve event handler must return an [Assembly] object, and the common language runtime must bind to this object. Typically, you can use the Assembly.LoadFrom method to load the assembly and then to return the object. To do this, follow these steps: Create a new Class Library project that is named MyAssembly3 by following steps 1 through7 of the “Method 1: Install the assembly in the global assembly cache (GAC)” section.Create a new client application. To do this,follow these steps:In Visual Studio .NET or Visual Studio 2005, create a new Visual Basic .NET or Visual Basic 2005 Windows application or create a new Visual C# .NET or Visual C# 2005 Windows application that is named TestClient3.
By default, the Form1.vb file is created by Visual Basic .NET or Visual Basic 2005. By default, the Form1.cs fileis created by Visual C# .NET or Visual C# 2005.Add a Button control to the Form1.vb file or to the Form1.cs file.Double-click the Button1 control, and then add the following code to the Button1_Click event:
Visual Basic .NET or Visual Basic 2005 code

Dim obj3 As New MyAssembly3.Class1()MessageBox.Show(obj3.HelloWorld())Visual C# .NET or Visual C# 2005 code

MyAssembly3.Class1 obj3=new MyAssembly3.Class1();MessageBox.Show(obj3.HelloWorld()); In Solution Explorer, right-click Add reference.
The Add Reference dialog box appears.Click Browse, click the MyAssembly3 assembly, and then click Open.In the References folder, right-click the MyAssembly3 assembly, and then click Properties.
The Properties window appears.In the Properties window, set the Copy Local property of the assembly to False.Add an event handler to the AssemblyResolve event in the Form1_Load event as follows:
Visual Basic .NET or Visual Basic 2005 code

AddHandler AppDomain.CurrentDomain.AssemblyResolve, AddressOf MyResolveEventHandlerVisual C# .NET or Visual C# 2005 code

AppDomain currentDomain = AppDomain.CurrentDomain;currentDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler);Define the MyResolveEventHandler function as follows:
Visual Basic .NET or Visual Basic 2005 code

Function MyResolveEventHandler(ByVal sender As Object, _ByVal args As ResolveEventArgs) As [Assembly]‘This handler is called only when the common language runtime tries to bind to the assembly and fails.’Retrieve the list of referenced assemblies in an array of AssemblyName.Dim objExecutingAssemblies As [Assembly]objExecutingAssemblies = [Assembly].GetExecutingAssembly()Dim arrReferencedAssmbNames() As AssemblyNamearrReferencedAssmbNames = objExecutingAssemblies.GetReferencedAssemblies()’Loop through the array of referenced assembly names.Dim strAssmbName As AssemblyNameFor Each strAssmbName In arrReferencedAssmbNames’Look for the assembly names that have raised the “AssemblyResolve” event.If (strAssmbName.FullName.Substring(0, strAssmbName.FullName.IndexOf(“,”)) = args.Name.Substring(0, args.Name.IndexOf(“,”))) Then’Build the path of the assembly from where it has to be loaded.Dim strTempAssmbPath As StringstrTempAssmbPath = “C:\assemblies\” & args.Name.Substring(0, args.Name.IndexOf(“,”)) & “.dll” Dim MyAssembly as [Assembly]‘Load the assembly from the specified path.MyAssembly = [Assembly].LoadFrom(strTempAssmbPath)’Return the loaded assembly.Return MyAssemblyEnd IfNextEnd FunctionVisual C# .NET or Visual C# 2005 code

private Assembly MyResolveEventHandler(object sender,ResolveEventArgs args){ //This handler is called only when the common language runtime tries to bind to the assembly and fails. //Retrieve the list of referenced assemblies in an array of AssemblyName. Assembly MyAssembly,objExecutingAssemblies; string strTempAssmbPath=”"; objExecutingAssemblies=Assembly.GetExecutingAssembly(); AssemblyName [] arrReferencedAssmbNames=objExecutingAssemblies.GetReferencedAssemblies(); //Loop through the array of referenced assembly names. foreach(AssemblyName strAssmbName in arrReferencedAssmbNames) { //Check for the assembly names that have raised the “AssemblyResolve” event. if(strAssmbName.FullName.Substring(0, strAssmbName.FullName.IndexOf(“,”))==args.Name.Substring(0, args.Name.IndexOf(“,”))) { //Build the path of the assembly from where it has to be loaded. strTempAssmbPath=”C:\\Myassemblies\\”+args.Name.Substring(0,args.Name.IndexOf(“,”))+”.dll”; break; } } //Load the assembly from the specified path. MyAssembly = Assembly.LoadFrom(strTempAssmbPath); //Return the loaded assembly. return MyAssembly; }On the Debug menu, click Start to run the application.
Note You must import the System.Reflection namespace to run this application.Click Button1 to call the HelloWorld() method of the MyAssembly3 assembly.

How to consume assemblies that are located in a folder that is different from the application base folder in Visual Basic .NET or in Visual Basic 2005

Symptoms
This article discusses how to consume assemblies that are located in a folder than is different from the application base folder in a Microsoft Visual Basic .NET or Microsoft Visual Basic 2005 solution.
Resolution
When you run a Visual Basic .NET or Visual Basic 2005 application, the common language runtime (CLR) expects to locate any assemblies that the application uses in either the global assembly cache or the application folder. When the CLR does not successfully bind to an assembly, you receive an error message that is similar to the following:

System.IO.FileNotFoundException. File or assembly name AssemblyName, or one of its dependencies, was not found. You can use many methods to locate an assembly that is located in a different folder. This article describes the following three methods.
Method 1: Install the assemblies in the global assembly cacheThis method requires that you sign an assembly with a strong name. To sign an assembly with a strong name and to then install the assembly in the global assembly cache, follow these steps: Name an assembly by using a strong name. To do this, follow these steps: Use the Strong Name tool (Sn.exe) that is included with the Microsoft .NET Framework software development kit (SDK) to generate a cryptographic key pair.
At the Microsoft Visual Studio .NET or Microsoft Visual Studio 2005 Command Prompt, type the following command:
sn -k AssemblyName.snk This command generates and stores a key pair in a file that is named AssemblyName.snk.Include the following assembly-level attribute in the source code (AssemblyInfo.vb):

<Assembly: AssemblyKeyFile(“AssemblyName.snk”)>Use the Global Assembly Cache Tool (Gacutil.exe) to register the .NET Framework assembly in the global assembly cache. To do this, type the following command at the Visual Studio .NET Command Prompt:
gacutil /i AssemblyName.dllFor more information about how to work with assemblies and with the global assembly cache, visit the following Microsoft Developer Network (MSDN) Web site:
http://msdn2.microsoft.com/en-us/library/6axd4fx6(vs.71).aspx(http://msdn2.microsoft.com/en-us/library/6axd4fx6(vs.71).aspx)For more information about how to sign an assembly with a strong name, visit the following MSDN Web site:
http://msdn2.microsoft.com/en-us/library/xc31ft41(vs.71).aspx(http://msdn2.microsoft.com/en-us/library/xc31ft41(vs.71).aspx)Note This method may be difficult and time-consuming if many assemblies exist and if the assemblies have dependencies on Microsoft Component Object Model (COM) objects. Any assembly dependencies must also be discoverable by the CLR. Therefore, if you plan to install your assembly to the global assembly cache, also install the dependencies of the assembly into the global assembly cache.
Method 2: Use an Application.Config file together with the CodeBase tagBy using an application configuration file, you can specify the location at which the CLR should look for dependent assemblies. Specifically, use the Codebase tag. By using this tag, you can put your assemblies in a separate folder. However, this method still requires that you name the library assembly by using a strong name. To use the application configuration file, follow these steps: Compile your library assembly.Sign the assembly by using a strong name.From your main application, add a reference to your library assembly, and then compile the main application.Obtain the public key token from your strongly named library assembly. To do this, follow these steps: Click Start, click All Programs, and then click Microsoft Visual Studio .NET 2003.
Note In Visual Studio 2005, click Start, click All Programs, and then click Microsoft Visual Studio 2005.Click Visual Studio .NET Tools, and then click Visual Studio .NET 2003 Command Prompt.
Note In Visual Studio 2005, click Visual Studio 2005 Tools, and then click Visual Studio .NET 2005 Command Prompt.Change the directory to the location of your library assembly. Typically, the location is the Bin folder of your library assembly project.Type the following command, and then press ENTER:
SN -T MyLibrary.dllNote Replace MyLibrary with the name of your library assembly. To make sure that the correct value is returned, make sure that you use an uppercase T in the -T switch.This command returns a hexadecimal value that represents the token for the public key of your library assembly. Make a copy of the hexadecimal value that will be used in the application configuration file.Obtain the assembly version from your strongly named library assembly. To obtain the assembly version, use one of the following methods:Method A: Windows Explorer file propertiesIn Windows Explorer, right-click your library assembly, and then click Properties.In the Properties dialog box, click the Version tab.Under Other version Information, click Assembly Version. Make a copy of this version value that will be used in the application configuration file.Method B: MSIL Disassembler (ILDASM)At the Visual Studio .NET 2003 Command Prompt, type ILDASM, and then press ENTER.On the File menu, click Open.Click your custom assembly, and then click Open.In the bottom pane, locate the assembly version (.ver). The following is an example of the assembly version:

.assembly yourAssemblyName{.ver 1:0:1969:29451}Note In this example, the assembly version is 1.0.1969.29541.Create an application configuration file. To do this, follow these steps: Paste the following XML code in Notepad:

<configuration><runtime><assemblyBinding xmlns=”urn:schemas-microsoft-com:asm.v1″><dependentAssembly><assemblyIdentity name=”MyLibrary”culture=”neutral”publicKeyToken=”fa4e781ae585ee8a”/><codeBase version=”1.0.1074.32347″ href=”FILE://C:/assemblies/myLibrary.dll”/></dependentAssembly></assemblyBinding></runtime></configuration>Change the following XML element attributes.
Collapse this tableExpand this table
Element attributeMake this changeassemblyIdentity nameChange MyLibrary to the name of your library assembly.assemblyIdentity publicKeyTokenChange the publicKeyToken attribute to the public key token value that you copied in step 4.codeBase versionChange the version attribute to the version value that you copied in step 5.codeBase hrefChange the href attribute to point to the full path name and the file name of your library assembly.Save the file as the main assembly .exe file name. Use .config as the file name extension. For example, if your main assembly is named “MyProject.exe,” the application configuration file name is “MyProject.exe.config.” You must save this file in the same folder in which your main application assembly is located.For more information, visit the following MSDN Web sites:
Specifying an assembly’s location
http://msdn2.microsoft.com/en-us/library/4191fzwb(vs.71).aspx(http://msdn2.microsoft.com/en-us/library/4191fzwb(vs.71).aspx)
<codeBase> element
http://msdn2.microsoft.com/en-us/library/efs781xb(vs.71).aspx(http://msdn2.microsoft.com/en-us/library/efs781xb(vs.71).aspx)
How the runtime locates assemblies
http://msdn2.microsoft.com/en-us/library/yx7xezcf(vs.71).aspx(http://msdn2.microsoft.com/en-us/library/yx7xezcf(vs.71).aspx)
Method 3: Use the System.Reflection.Assembly.LoadFrommethodThis method uses the Assembly.LoadFrom method to explicitly load an assembly from a fully qualified path name and from a fully qualified file name. To do this, follow these steps: Compile your library assembly.Use code that is similar to the following code example to load your assembly:

Dim yourAssembly As System.Reflection.Assembly = _System.Reflection.Assembly.LoadFrom(“c:\yourAssembly.dll”)Note If you have any dependent assemblies, you should locate them in the same path where the main assembly is located.
For more information about the Assembly.LoadFrom method, visit the following MSDN Web site:
http://msdn2.microsoft.com/en-us/library/system.reflection.assembly.loadfrom(vs.71).aspx(http://msdn2.microsoft.com/en-us/library/system.reflection.assembly.loadfrom(vs.71).aspx)
Method 4: Use the AssemblyResolve eventThe AssemblyResolve event occurs whenever the CLR unsuccessfully tries to bind to an assembly. Use the AddHandler method in the application to add an event handler that returns the correct assembly whenever the AssemblyResolve event occurs.
Note This method does not require that you name the assemblies by using strong names.
The AssemblyResolve event handler must return an [Assembly] object that represents the assembly to which the CLR must bind. Typically you can use the Assembly.LoadFrom method to load the assembly. Then, return the loaded assembly.
Note The event handler must be located in a procedure that does not contain any references to the relocated assemblies. Additionally, the event handler must be called before any code that depends on the relocated assemblies is called.
The following code is an example of an application that loads an assembly by using the AssemblyResolve event:

Module Module1Sub Main()Dim f As Form1′Set up event handler for AssemblyResolve eventAddHandler AppDomain.CurrentDomain.AssemblyResolve, _AddressOf MyResolveEventHandlerf = New Form1()Application.Run(f)End SubFunction MyResolveEventHandler(ByVal sender As Object, _ByVal args As ResolveEventArgs) As [Assembly]‘Load the assembly from the correct location and return the assembly’This handler is only called if we try to bind to the assembly and the attempt fails.Dim MyAssembly As [Assembly]MyAssembly = [Assembly].LoadFrom(“C:\assemblies\MyLibrary.dll”)Return MyAssemblyEnd FunctionEnd Module’Some sample code in Form1 which depends on the external assemblyPrivate Sub Button1_Click(ByVal sender As System.Object, _ByVal e As System.EventArgs) Handles Button1.ClickDim x As MyLibrary.Applicationx = New MyLibrary.Application()x.MyMethod()End SubEnd ClassFor more information about the AppDomain.AssemblyResolve event, visit the following MSDN Web site:
http://msdn2.microsoft.com/en-us/library/system.appdomain.assemblyresolve(vs.71).aspx(http://msdn2.microsoft.com/en-us/library/system.appdomain.assemblyresolve(vs.71).aspx)

How to access serial ports by using Visual Basic 2005

Symptoms
For a Microsoft Visual Studio .NET version of this article, see 823179?(http://support.microsoft.com/kb/823179/).This step-by-step article describes how to access serial ports by using Microsoft Visual Basic 2005. This article also contains code examples that illustrate the concepts that are discussed.
Note You cannot use the Microsoft .NET Framework classes to directly access other types of ports, such as parallel ports or USB ports.
Resolution
To access serial ports by using Visual Basic 2005, follow these steps: Start Microsoft Visual Studio 2005.On the File menu, point to New, and then click Project.Under Project Types, expand Visual Basic, and then click Windows.Under Templates, click Console Application.In the Name box, type MyConsoleApplication, and then click OK.
By default, the Module1.vb file is created.To write data to a serial port, add the following SendSerialData method to the Module1.vb file.

Sub SendSerialData(ByVal data As String)’ Send strings to a serial port.Using com1 As IO.Ports.SerialPort = _My.Computer.Ports.OpenSerialPort(“COM1″)com1.WriteLine(data) com1.Close()End UsingEnd SubTo read data from a serial port, add the following ReceiveSerialData function to the Module1.vb file.

Function ReceiveSerialData() As String’ Receive strings from a serial port.Dim returnStr As String = “”Using com1 As IO.Ports.SerialPort = _My.Computer.Ports.OpenSerialPort(“COM1″)DoDim Incoming As String = com1.ReadLine()If Incoming Is Nothing ThenExit DoElsereturnStr &= Incoming & vbCrLfEnd IfLoop com1.Close()End UsingReturn returnStrEnd FunctionTo read data from and write data to a serial port, add the following code to the Sub Main procedure.

Dim Data As StringData = “Test”Console.WriteLine(“Writing the following data to COM1: ” & Data)SendSerialData(Data)Console.WriteLine(“Read the following data from COM1: ” & ReceiveSerialData())Console.WriteLine(“Press ENTER to quit”)Console.ReadLine()To run the solution, press CTRL+F5.