Microsoft KB Archive/816175

= How to manage a multilanguage component solution in Visual C# =

Article ID: 816175

Article Last Modified on 5/16/2007

-

APPLIES TO


 * Microsoft Visual C# 2005 Express Edition
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition

-





For a Microsoft Visual Basic .NET version of this article, see 317702.

IN THIS TASK

 * SUMMARY
 * Requirements
 * Manage a Multilanguage Component Solution
 * Complete Code Listing
 * Complete Code Listing (Class1.vb)
 * Complete Code Listing(Class1.cs)
 * Complete Code Listing (Form1.cs)
 * Verify That It Works
 * Troubleshooting
 * REFERENCES



SUMMARY
This article describes how to manage a multilanguage component solution using Visual C# .NET or Visual C# 2005.



Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that are required:
 * One of the following operating systems:
 * Microsoft Windows 2000 Professional (or Server)
 * Microsoft Windows XP Professional with the .NET Framework installed
 * Microsoft Windows Server 2003
 * Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
 * Microsoft SQL Server version 7.0 or later with Northwind database.

This article assumes that you are familiar with the following topics:
 * Implementation inheritance
 * Windows Forms
 * ADO.NET

Back to the top

Manage a Multilanguage Component Solution
This article describes how to create a solution that contains the following three projects:
 * A Visual C# Windows application that uses methods in the other two project classes.
 * A Visual Basic .NET or Visual Basic 2005 Library project that accesses the NorthWind database and returns a DataSet.
 * A Visual C# 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.

To create the shell of a Visual C# Windows Forms application in a solution, following these steps:
 * 1) Click Start, point to Programs, and then point to either Microsoft Visual Studio .NET (for Visual Studio .NET 2002), Microsoft Visual Studio .NET 2003 (for Microsoft Visual Studio .NET 2003), or Microsoft Visual Studio 2005 (for Microsoft Visual Studio 2005).
 * 2) Click Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
 * 3) Click New Project, and then click Visual C# Projects

Note In Visual Studio 2005, click Visual C#.
 * 1) Under Templates, click Windows Application.
 * 2) In the Name field, 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 C# Windows Forms application in a solution named HowToMultiComp.

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

The Visual Basic Class Library must be added first. To add the Visual Basic Class Library, follow these steps:  In Solution Explorer, right-click Solution 'HowToMultiComp', point to Add, and then click New Project. Click Visual Basic Projects.

Note In Visual Studio 2005, click Visual Basic. Under Templates, click Class Library. In the Name box, type NWClassLib, and then click OK.

The Visual Basic .NET or Visual Basic 2005 Class Library is added to the solution.

Class1.vb is now open in the Editor window.  Add two Imports directives at the top to gain shorthand dot notation access to classes in the following namespaces: Imports System.Data Imports System.Data.SqlClient   To create and fill a DataSet with the data from the Northwind products table, add the following method to Class1 (you may have to adjust the connection string for your computer): Public Shared Function GetProducts As DataSet Dim ds As New DataSet Dim sqlDS As New SqlDataAdapter(&quot;select * from products&quot;, New SqlConnection(&quot;server=<network address of instance of SQL Server>;database=northwind;integrated security=sspi&quot;)) sqlDS.Fill(ds) return ds End Function </li></ol>

Create the Visual C# Class Library. To do this, follow these steps: <ol> In Solution Explorer, right-click Solution 'HowToMultiComp', point to Add, and then click New Project.</li> Click Visual C# Projects.

Note In Visual Studio 2005, click Visual C#.</li> Under Templates, click Class Library.</li> In the Name box, type SBClassLib, and then click OK.

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

Class1.cs is now open in the Editor window.</li>  Add the following using directives at the top of the Class1.cs code sample to gain shorthand dot notation access to the classes in the System.Text namespace and the System.Collections namespace: using System.Text; using System.Collections; </li>  Add the following code in public class Class1, which uses an instance of the StringBuilder class to concatenate a single string that is contained in an ArrayList: public static String BuildString(ArrayList arlStrings) {   StringBuilder sb = new StringBuilder; foreach(String s in arlStrings) {           sb.Append(s); sb.Append(&quot;, &quot;); }   return sb.ToString; } </li></ol>

You can now create the Visual C# Windows Forms application that consumes this component. To create the Visual C# Windows Forms application, follow these steps: <ol> To add references to two Class Library components, follow these steps: <ol style="list-style-type: lower-alpha;"> In Solution Explorer, right-click References under HowToMultiComp, and then click Add Reference.</li> Under Projects, press and hold the CTRL key, click NWClassLib, and then click SBClassLib.</li> With both projects selected, click Select.</li> When both the projects appear in the Selected Components box, click OK.

The projects are now listed under References for the Window Application.</li></ol> </li> Form1.cs is open in Design view. If Form1.cs in not already open, double-click Form1.cs in Solution Explorer.</li> Press CTRL+ALT+X to open the toolbox, and then click Windows Forms.</li> Drag a button onto the form, and then press F4 to access the property page for the button.</li> Change the Text property setting to Show Products .</li> Add another button, and then change the Text property to Build String .</li> <li>Drag a DataGrid control onto the form, and then drop it below the buttons. Resize the DataGrid control so that it fills the remaining area on the form.</li> <li>Double-click Show Products. Visual Studio .NET automatically creates a Click event handler.</li> <li> Add the following code the the Click event handler: dataGrid1.DataSource = NWClassLib.Class1.GetProducts.Tables[0]; This code sets the DataSource property of the DataGrid to the DataSet that was returned by the GetProducts method of the Class Library. </li> <li>To create a second Click event handler, double-click Build String.</li> <li> 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, which returns a string that appears in a MessageBox function. An instance of the class is not necessary, because the method was declared earlier by using the static keyword.

Add the following code to the Click event handler: ArrayList arl = new ArrayList; for(int i = 0; i <= 8; i++) arl.Add(dataGrid1[0, i].ToString); MessageBox.Show(SBClassLib.Class1.BuildString(arl)); </li></ol>

Back to the top

Complete Code Listing (Class1.vb)
Imports System.Data Imports System.Data.SqlClient

Public Class Class1 Public Shared Function GetProducts As DataSet Dim ds As New DataSet Dim sqlDS As New SqlDataAdapter(&quot;select * from products&quot;, New SqlConnection(&quot;server=<network address of instance of SQL Server>;database=northwind;uid=sa&quot;)) sqlDS.Fill(ds) Return ds   End Function End Class Back to the top

Complete Code Listing (Class1.cs)
using System; using System.Text; using System.Collections;

namespace SBClassLib {   public class Class1 {       public Class1 {       }

public static String BuildString(ArrayList arlStrings) {           StringBuilder sb = new StringBuilder;

foreach(String s in arlStrings) {               sb.Append(s); sb.Append(&quot;, &quot;); }           return sb.ToString; }   } } Back to the top

Complete Code Listing (Form1.cs)
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data;

namespace HowToMultiComp {   public class Form1 : System.Windows.Forms.Form {       private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button2; private System.Windows.Forms.DataGrid dataGrid1; private System.ComponentModel.Container components = null;

public Form1 {           InitializeComponent; }

protected override void Dispose( bool disposing ) {           if( disposing ) {               if (components != null) {                   components.Dispose; }           }            base.Dispose( disposing ); }

private void InitializeComponent {           this.button1 = new System.Windows.Forms.Button; this.button2 = new System.Windows.Forms.Button; this.dataGrid1 = new System.Windows.Forms.DataGrid; ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit; this.SuspendLayout; //            // button1 //            this.button1.Name = &quot;button1&quot;; this.button1.Size = new System.Drawing.Size(104, 23); this.button1.TabIndex = 0; this.button1.Text = &quot;Show Products&quot;; this.button1.Click += new System.EventHandler(this.button1_Click); //            // button2 //            this.button2.Location = new System.Drawing.Point(136, 0); this.button2.Name = &quot;button2&quot;; this.button2.Size = new System.Drawing.Size(96, 23); this.button2.TabIndex = 1; this.button2.Text = &quot;Build String&quot;; this.button2.Click += new System.EventHandler(this.button2_Click); //            // dataGrid1 //            this.dataGrid1.DataMember = &quot;&quot;; this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText; this.dataGrid1.Location = new System.Drawing.Point(0, 24); this.dataGrid1.Name = &quot;dataGrid1&quot;; this.dataGrid1.Size = new System.Drawing.Size(296, 256); this.dataGrid1.TabIndex = 2; //            // Form1 //            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 273); this.Controls.AddRange(new System.Windows.Forms.Control[] {                                                                         this.dataGrid1,                                                                          this.button2,                                                                          this.button1}); this.Name = &quot;Form1&quot;; this.Text = &quot;Form1&quot;; ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit; this.ResumeLayout(false);

}

[STAThread] static void Main {           Application.Run(new Form1); }

private void button1_Click(object sender, System.EventArgs e)       { dataGrid1.DataSource = NWClassLib.Class1.GetProducts.Tables[0]; }

private void button2_Click(object sender, System.EventArgs e)       { ArrayList arl = new ArrayList; for(int i = 0; i <= 8; i++) arl.Add(dataGrid1[0, i].ToString); MessageBox.Show(SBClassLib.Class1.BuildString(arl)); }   } } Note The code should be changed in Visual Studio 2005. When you create a Windows Forms project, Visual C# adds one form to the project by default. This form is named Form1. The two files that represent the form are called Form1.cs and Form1.designer.cs. You write your code in Form1.cs. The Sesigner.cs file is where the Windows Forms Designer writes the code that implements all the actions that you performed by adding controls. For more information about the Windows Forms Designer in Visual C# 2005, visit the following Microsoft Web site:

http://msdn2.microsoft.com/en-us/library/ms173077.aspx

Back to the top

Verify That It Works

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

The DataGrid 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 debugging and return to Visual Studio .NET.

Back to the top

Troubleshooting
Make sure that you do not confuse the syntax when you are working with two languages. Visual C# is very particular, and the error messages are not always as prompt or as intuitive as the error messages in Visual Basic .NET or in Viusal Basic 2005. Therefore, you may want to bookmark the &quot;Language Equivalents&quot; section of the .NET Framework software development kit to troubleshoot these difficulties. Locate the following Help topic in MS Help:

ms:help://MS.VCC/MS.MSDNVS/vsintro7/html/vxgrfLanguageEquivalents.htm

Back to the top

<div class="references_section">