How to manage a multilanguage component solution in Visual C#

This article has been archived. It is offered "as is" and will no longer be updated.
Summary
This article describes how to manage a multilanguage component solution using Visual C# .NET or Visual C# 2005.
More information

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#.
  4. Under Templates, click Windows Application.
  5. In the Name field, type HowToMultiComp, and then click OK.
  6. 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:
  1. In Solution Explorer, right-click Solution 'HowToMultiComp', point to Add, and then click New Project.
  2. Click Visual Basic Projects.

    Note In Visual Studio 2005, click Visual Basic.
  3. Under Templates, click Class Library.
  4. 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.
  5. Add two Imports directives at the top to gain shorthand dot notation access to classes in the following namespaces:
    Imports System.DataImports System.Data.SqlClient
  6. 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("select * from products", New SqlConnection("server=<network address of instance of SQL Server>;database=northwind;integrated security=sspi"))	sqlDS.Fill(ds)	return dsEnd Function
Create the Visual C# Class Library. To do this, follow these steps:
  1. In Solution Explorer, right-click Solution 'HowToMultiComp', point to Add, and then click New Project.
  2. Click Visual C# Projects.

    Note In Visual Studio 2005, click Visual C#.
  3. Under Templates, click Class Library.
  4. 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.
  5. 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;
  6. 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(", ");	}	return sb.ToString();}
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:
  1. To add references to two Class Library components, follow these steps:
    1. In Solution Explorer, right-click References under HowToMultiComp, and then click Add Reference.
    2. Under Projects, press and hold the CTRL key, click NWClassLib, and then click SBClassLib.
    3. With both projects selected, click Select.
    4. When both the projects appear in the Selected Components box, click OK.

      The projects are now listed under References for the Window Application.
  2. Form1.cs is open in Design view. If Form1.cs in not already open, double-click Form1.cs in Solution Explorer.
  3. Press CTRL+ALT+X to open the toolbox, and then click Windows Forms.
  4. Drag a button onto the form, and then press F4 to access the property page for the button.
  5. Change the Text property setting to Show Products.
  6. Add another button, and then change the Text property to Build String.
  7. 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.
  8. Double-click Show Products. Visual Studio .NET automatically creates a Click event handler.
  9. 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.
  10. To create a second Click event handler, double-click Build String.
  11. 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));
Back to the top

Complete Code Listing

Complete Code Listing (Class1.vb)

Imports System.DataImports System.Data.SqlClientPublic Class Class1    Public Shared Function GetProducts() As DataSet        Dim ds As New DataSet()        Dim sqlDS As New SqlDataAdapter("select * from products", New SqlConnection("server=<network address of instance of SQL Server>;database=northwind;uid=sa"))        sqlDS.Fill(ds)        Return ds    End FunctionEnd 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(", ");			}			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 = "button1";			this.button1.Size = new System.Drawing.Size(104, 23);			this.button1.TabIndex = 0;			this.button1.Text = "Show Products";			this.button1.Click += new System.EventHandler(this.button1_Click);			// 			// button2			// 			this.button2.Location = new System.Drawing.Point(136, 0);			this.button2.Name = "button2";			this.button2.Size = new System.Drawing.Size(96, 23);			this.button2.TabIndex = 1;			this.button2.Text = "Build String";			this.button2.Click += new System.EventHandler(this.button2_Click);			// 			// dataGrid1			// 			this.dataGrid1.DataMember = "";			this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;			this.dataGrid1.Location = new System.Drawing.Point(0, 24);			this.dataGrid1.Name = "dataGrid1";			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 = "Form1";			this.Text = "Form1";			((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: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.
  3. 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).
  4. 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 "Language Equivalents" 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
References
For more information, visit the following MSDN Web site:Back to the top
Properties

Article ID: 816175 - Last Review: 01/17/2015 06:53:17 - Revision: 3.0

  • Microsoft Visual C# 2005 Express Edition
  • Microsoft Visual C# .NET 2003 Standard Edition
  • Microsoft Visual C# .NET 2002 Standard Edition
  • kbnosurvey kbarchive kbhowtomaster kbinterop kbappdev kbhowto KB816175
Feedback