Microsoft KB Archive/317702

From BetaArchive Wiki

Article ID: 317702

Article Last Modified on 6/1/2007



APPLIES TO

  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
  • Microsoft Visual C# .NET 2002 Standard Edition



This article was previously published under Q317702


SUMMARY

This article describes how to manage a multilanguage component solution.

Requirements

The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:

  • Microsoft Windows 2000 Professional, Microsoft Windows 2000 Server, or Microsoft Windows XP Professional with the .NET Framework installed
  • Microsoft Visual Studio 2005 or Microsoft Visual Studio .NET
  • Microsoft SQL Server 7.0 or later with the Northwind database

This article assumes that you are familiar with the following topics:

  • Implementation inheritance
  • Windows Forms
  • ADO.NET

Manage a multilanguage component solution

This article describes how to create a solution that contains the following three projects:

  • A small Microsoft Visual Basic 2005 or Microsoft Visual Basic .NET Windows application that uses methods in the other two project classes.
  • A Visual C# Class Library project that has a method that accesses the Northwind database and returns a DataSet.
  • A Visual Basic 2005 or Visual Basic .NET Class Library project that has a method that takes an ArrayList of string objects and returns a single string that is concatenated by using an instance of the StringBuilder class.

Create the shell of a Visual Basic Windows Forms application in a solution

  1. Start Visual Studio 2005 or Visual Studio .NET.
  2. On the File menu, click New Project, and then click Visual Basic Projects.


Note In Visual Studio 2005, click Visual Basic instead of Visual Basic Projects.

  1. Under Templates, click Windows Application.
  2. In the Name box, type HowToMultiComp, and then click OK.
  3. When the project is created, make sure that Solution Explorer is visible. If Solution Explorer is not visible, press CTRL+ALT+L.


You now have the shell of a Visual Basic Windows Forms application in a solution that is named HowToMultiComp.

To create a multilanguage component solution, add a Visual C# Class Library, and then add a Visual Basic 2005 or Visual Basic .NET Class Library.

Add the Visual C# Class Library

  1. In Solution Explorer, right-click Solution 'HowToMultiComp', click Add, and then click New Project.
  2. Click Visual C# Projects.

    Note In Visual Studio 2005, click Visual C# instead of Visual C# Projects.
  3. Under Templates, click Class Library.
  4. In the Name box, type NWClassLib, and then click OK.

    The Visual C# Class Library is added to the solution.

    The Class1.cs file is now open in the Editor window.
  5. Add two using directives at the top to gain shorthand dot notation access to classes in the following namespaces.

    using System.Data;
    using System.Data.SqlClient;
                        
  6. To create and then fill a DataSet with the data from the Northwind products table, replace the Class1 method with the following code.

    Note You may have to adjust the connection string for your computer.

    public static DataSet GetProducts()
    {
        DataSet ds = new DataSet();
    SqlDataAdapter sqlDS = new SqlDataAdapter("select * from products", new SqlConnection("server=(local);database=northwind;uid=sa"));
        sqlDS.Fill(ds);
        return ds;
    }
                        

Add the Visual Basic Class Library

  1. In Solution Explorer, right-click Solution 'HowToMultiComp', click Add, and then click New Project.
  2. Click Visual Basic Projects.

    Note In Visual Studio 2005, click Visual Basic instead of Visual Basic Projects.
  3. Under Templates, click Class Library.
  4. In the Name box, type VBClassLib, and then click OK.

    The Visual Basic Class Library is added to the solution.

    The Class1.vb file is now open in the Editor window.
  5. Add an Imports directive at the top of the Class1.vb code sample to gain shorthand dot notation access to classes in the following System.Text namespace.

    Imports System.Text
                        
  6. Add the following code in Public Class Class1. This code uses an instance of the StringBuilder class to concatenate a single string from strings that are contained in an ArrayList.

    Shared Function BuildString(ByVal arlStrings As ArrayList) As String
       Dim s As String
       Dim sb As New StringBuilder()
    
       For Each s In arlStrings
        sb.Append(s)
            sb.Append(", ")
       Next
       Return sb.ToString
    End Function
                        

Create the Visual Basic Windows Forms application

You can now create a Visual Basic Windows Forms application that consumes this component. To create the Visual Basic Windows Forms application, follow these steps:

  1. Add references to two Class Library components. To do this, follow these steps:
    1. In Solution Explorer, right-click References under HowToMultiComp, and then click Add Reference.
    2. Under Projects, hold down CTRL, and then click NWClassLib and SBClassLib.
    3. With both projects selected, click Select.
    4. When both projects appear in the Selected Components box, click OK.

      The projects are now listed under References for the Windows Application.
  2. The Form1.vb file is open in Design view. If the Form1.vb file is not already open, double-click Form1.vb in Solution Explorer.
  3. Press CTRL+ALT+X to open the toolbox.
  4. In the toolbox, click Windows Forms.
  5. Add a button to the form, and then press F4 to access the property page for the button.
  6. Change the Text Property setting to Show Products.
  7. Add another button.

    This changes the Text property to Build String.
  8. Add a DataGrid control to the form, below the buttons. Drag the lower-right corner of the DataGrid control, so that it fills the remaining area on the form.
  9. Double-click Show Products.

    Visual Studio automatically creates a Click event handler.
  10. To create an instance of the NWClassLib.Class1 class, add the following code to the Click event handler.

    Dim objNW As New NWClassLib.Class1()
          DataGrid1.DataSource = objNW.GetProducts.Tables(0)
                            

    This code sets the DataSource property of the DataGrid control to the DataSet that is returned by the GetProducts method of the Class Library.

  11. Double-click Build String to create a second Click event handler.
  12. The following code takes the contents of each cell in the first row of the DataGrid up to cell 7, and then adds the contents to an ArrayList. The ArrayList is then passed to the SBClassLibrary method. The SBClassLibrary method returns a string that appears in a MessageBox function. An instance of the class is not required because the method was declared earlier by using the shared keyword.

    Add the following code to the Click event handler.

    Dim arl As New ArrayList()
          Dim i As Int32
          For i = 0 To 7
            arl.Add(DataGrid1.Item(0, i))
          Next
          MessageBox.Show(SBClassLib.Class1.BuildString(arl))
                        

Complete code listing

Complete code listing (Class1.cs)

using System;
using System.Data;
using System.Data.SqlClient;

namespace NWClassLib
{
    /// <summary>
    /// Summary description for Class1.
    /// </summary>
    public class Class1
    {
        public static DataSet GetProducts()
        {
            DataSet ds = new DataSet();
SqlDataAdapter sqlDS = new SqlDataAdapter("select * from products", new SqlConnection("server=C-176012-C;database=northwind;uid=sa"));
            sqlDS.Fill(ds);
            return ds;
        }
    }
}
                

Complete code listing (Class1.vb)

Imports System.Text

Public Class Class1
    Shared Function BuildString(ByVal arlStrings As ArrayList) As String
        Dim s As String
        Dim sb As New StringBuilder()
        For Each s In arlStrings
            sb.Append(s)
            sb.Append(", ")
        Next

        Return sb.ToString
    End Function
End Class
                

Complete code listing (Form1.vb)

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'The Windows Form Designer requires this call.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call.

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer.
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer.
    'It can be modified using the Windows Form Designer.  
    'Do not modify the procedure by using the Code Editor.
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid
    Friend WithEvents Button2 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Button1 = New System.Windows.Forms.Button()
        Me.DataGrid1 = New System.Windows.Forms.DataGrid()
        Me.Button2 = New System.Windows.Forms.Button()
        CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(24, 16)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(96, 23)
        Me.Button1.TabIndex = 0
        Me.Button1.Text = "Show Products"
        '
        'DataGrid1
        '
        Me.DataGrid1.DataMember = ""
        Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
        Me.DataGrid1.Location = New System.Drawing.Point(24, 48)
        Me.DataGrid1.Name = "DataGrid1"
        Me.DataGrid1.Size = New System.Drawing.Size(256, 200)
        Me.DataGrid1.TabIndex = 1
        '
        'Button2
        '
        Me.Button2.Location = New System.Drawing.Point(200, 16)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New System.Drawing.Size(80, 24)
        Me.Button2.TabIndex = 2
        Me.Button2.Text = "Build String"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 266)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button2, Me.DataGrid1, Me.Button1})
        Me.Name = "Form1"
        Me.Text = "Form1"
        CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim objNW As New NWClassLib.Class1()
        DataGrid1.DataSource = objNW.GetProducts.Tables(0)
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim arl As New ArrayList()
        Dim i As Int32
        For i = 0 To 7
            arl.Add(DataGrid1.Item(0, i))
        Next

        .Show(SBClassLib.Class1.BuildString(arl))
    End Sub
End Class
                

Note You must change the code in Visual Basic 2005. By default, Visual Basic creates two files for the project when you create a Windows Forms project. If the form is named Form1, the two files that represent the form are named Form1.vb and Form1.Designer.vb. You write the code in the Form1.vb file. The Windows Forms Designer writes the code in the Form1.Designer.vb file. The Windows Forms Designer uses the partial keyword to divide the implementation of Form1 into two separate files. This behavior prevents the designer-generated code from being interspersed with your code.

For more information about the new Visual Basic 2005 language enhancements, visit the following Microsoft Developer Network (MSDN) Web site:

For more information about partial classes and the Windows Forms Designer, visit the following MSDN Web site:

Verify that it works

  1. Press F5 to run the application in debug mode.
  2. When the form appears, click Get Products.


The DataGrid control displays all the products from the Northwind products table.

  1. Click Build String.


A message box appears that shows the concatenated contents of the cells of the first row, through the cell with an index of 7 (zero-based).

  1. Press SHIFT+F5 to stop the debugging and return to Visual Studio.

Troubleshooting

Make sure that you do not confuse the syntax when you are working with two languages. Visual C# .NET is very particular, and the error messages are not always as prompt or as intuitive as the error messages in Visual Basic. Therefore, you may want to bookmark the "Language Equivalents" section of the Microsoft .NET Framework Software Development Kit (SDK) to troubleshoot these difficulties. This article is also available online. To view this article online, visit the following MSDN Web site:

REFERENCES

For more information, visit the following MSDN Web site:

Keywords: kbvs2005swept kbvs2005applies kbhowtomaster kbinterop KB317702