Microsoft KB Archive/816174

= How to manage a multilanguage component solution by using Visual C++ .NET or Visual C++ 2005 =

Article ID: 816174

Article Last Modified on 6/1/2007

-

APPLIES TO


 * Microsoft .NET Framework 1.1
 * Microsoft Visual C++ 2005 Express Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition

-







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



For a Microsoft Visual C# .NET version of this article, see 816175.

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.h)
 * Verify That Your Application Works
 * Troubleshooting
 * REFERENCES



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

back to the top

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

This article assumes that you are familiar with the following topics:
 * Implementation inheritance
 * Windows Forms
 * Microsoft 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++ .NET or Visual C++ 2005 Windows Forms Application project that uses methods that are in the classes of two other projects.
 * A Visual Basic .NET or Visual Basic 2005 Class Library project that accesses the Northwind database, and then returns a DataSet object.
 * A Visual C# .NET Class Library project that has a method that takes an ArrayList object as a parameter, and then returns a single string that is concatenated by an instance of the StringBuilder class.

To create the shell of a Visual C++ .NET or Visual C++ 2005 Windows Forms application in a solution, follow these steps:
 * 1) Start Microsoft Visual Studio .NET 2003 or Microsoft Visual Studio 2005.
 * 2) On the File menu, point to New, and then click Project.
 * 3) Click Visual C++ Projects under Project Types, and then click Windows Forms Application (.NET) under Templates.

Note In Visual Studio 2005, click Visual C++ under Project Types, and then click Windows Forms Application under Templates.
 * 1) In the Name text box, type HowToMultiComp, and then click OK.
 * 2) When the project is created, make sure that Solution Explorer is visible. If Solution Explorer is not visible, click Solution Explorer on the View menu.

You now have the shell of a Visual C++ .NET or Visual C++ 2005 Windows Forms application in a solution that is named HowToMultiComp.

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

To add the Visual Basic .NET or Visual Basic 2005 Class Library project, follow these steps:  In Solution Explorer, right-click Solution 'HowToMultiComp', point to Add, and then click New Project. Click Visual Basic Projects under Project Types, and then click Class Library under Templates.

Note In Visual Studio 2005, click Visual Basic under Other Language. In the Name text box, type NWClassLib, and then click OK.

The Visual Basic .NET or Visual Basic 2005 Class Library project is added to your solution, and the Class1.vb file is open in the Editor window.  Add the following Imports directives at the top of the Class1.vb file to gain shorthand dot notation access to classes in the System.Data namespace and in the System.Data.SqlClient namespace: Imports System.Data Imports System.Data.SqlClient   To create and fill a DataSet object with data from the Products table of the Northwind database, add the following method to the Class1.vb file: 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=;database=northwind;integrated security=sspi&quot;)) sqlDS.Fill(ds) Return ds End Function Note Modify the connection string as appropriate for your environment. </li></ol>

To add the Visual C# .NET or Visual C# 2005 Class Library project, follow these steps: <ol> In Solution Explorer, right-click Solution 'HowToMultiComp', point to Add, and then click New Project.</li> Click Visual C# Projects under Project Types, and then click Class Library under Templates.

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

The Visual C# .NET or Visual C# 2005 Class Library project is added to your solution, and the Class1.cs file is open in the Editor window.</li>  Add the following using directives at the top of the Class1.cs file to gain shorthand dot notation access to classes in the System.Text namespace and in the System.Collections namespace: using System.Text; using System.Collections; </li>  Add the following code after the public class Class1 statement to use an instance of the StringBuilder class to concatenate strings that are contained in an ArrayList object: 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>

Use your Visual C++ .NET or Visual C++ 2005 Windows Forms application to consume the two class libraries that you created earlier: <ol> To add references to the two class libraries that you created earlier, follow these steps: <ol style="list-style-type: lower-alpha;"> In Solution Explorer, right-click References under HowToMultiComp, and then click Add Reference.</li> On the Projects tab of the Add Reference dialog box, press and hold the CTRL key, click NWClassLib, and then click SBClassLib.</li> Click Select. When both projects appear in the Selected Components box, click OK.

Note The selected projects are now listed under the References node for your Windows application.</li></ol> </li> Verify that Form1 is open in Design view. If Form1 is not already open, double-click Form1.h in Solution Explorer.</li> Press the CTRL+ALT+X key combination to open the toolbox, and then click Windows Forms.</li> Add a Button control to Form1, and then press F4 to access the Properties pane for this button control.</li> Change the Text property to Show Products .</li> Add another Button control to Form1, and then change the Text property to Build String .</li> Add a DataGrid control to Form1 under the button controls.</li> Resize the DataGrid control so that it fills the remaining area on Form1.</li> <li>Double-click Show Products. Visual Studio .NET automatically creates a Click event handler in the Form1.h file and then puts the cursor inside this event handler.</li> <li> Add the following code to the Click event handler: dataGrid1->DataSource = NWClassLib::Class1::GetProducts->Tables->get_Item(0) ; This code sets the DataSource property of the DataGrid control to the DataSet object that was returned by the GetProducts method of the NWClassLib 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 control up to the ninth cell, and then the code adds these contents to an ArrayList object. The BuidString method of the SBClassLib class library takes this ArrayList object as a parameter, and then returns a string that appears in a MessageBox function. You do not have to create an instance of the SBClassLib class because the BuidString method is a static method.

Add the following code to the Click event handler: ArrayList * arl = new ArrayList; for(int i=0; i<=8; i++) arl->Add(dataGrid1->get_Item(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;integrated security=sspi&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 {   ///     /// Summary description for Class1. ///    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.h)

 * 1) pragma once

namespace HowToMultiComp {   using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing;

public __gc class Form1 : public System::Windows::Forms::Form {      public: Form1(void) {           InitializeComponent; }   protected: void Dispose(Boolean disposing) {           if (disposing && components) {               components->Dispose; }           __super::Dispose(disposing); }   private: System::Windows::Forms::Button *  button1; private: System::Windows::Forms::Button * button2; private: System::Windows::Forms::DataGrid * dataGrid1;

private: System::ComponentModel::Container * components;

void InitializeComponent(void) {           this->button1 = new System::Windows::Forms::Button; this->button2 = new System::Windows::Forms::Button; this->dataGrid1 = new System::Windows::Forms::DataGrid; (__try_cast<System::ComponentModel::ISupportInitialize * >(this->dataGrid1))->BeginInit; this->SuspendLayout; //            // button1 //            this->button1->Location = System::Drawing::Point(145, 32); this->button1->Name = S&quot;button1&quot;; this->button1->Size = System::Drawing::Size(95, 23); this->button1->TabIndex = 0; this->button1->Text = S&quot;Show Products&quot;; this->button1->Click += new System::EventHandler(this, button1_Click); //            // button2 //            this->button2->Location = System::Drawing::Point(304, 32); this->button2->Name = S&quot;button2&quot;; this->button2->Size = System::Drawing::Size(95, 23); this->button2->TabIndex = 1; this->button2->Text = S&quot;Build String&quot;; this->button2->Click += new System::EventHandler(this, button2_Click); //            // dataGrid1 //            this->dataGrid1->DataMember = S&quot;&quot;; this->dataGrid1->HeaderForeColor = System::Drawing::SystemColors::ControlText; this->dataGrid1->Location = System::Drawing::Point(8, 64); this->dataGrid1->Name = S&quot;dataGrid1&quot;; this->dataGrid1->Size = System::Drawing::Size(520, 248); this->dataGrid1->TabIndex = 2; //            // Form1 //            this->AutoScaleBaseSize = System::Drawing::Size(5, 13); this->ClientSize = System::Drawing::Size(544, 325); this->Controls->Add(this->dataGrid1); this->Controls->Add(this->button2); this->Controls->Add(this->button1); this->Name = S&quot;Form1&quot;; this->Text = S&quot;Form1&quot;; (__try_cast<System::ComponentModel::ISupportInitialize * >(this->dataGrid1))->EndInit; this->ResumeLayout(false);

}      private: System::Void button1_Click(System::Object *  sender, System::EventArgs *  e)             { dataGrid1->DataSource = NWClassLib::Class1::GetProducts->Tables->get_Item(0) ; }

private: System::Void button2_Click(System::Object * sender, System::EventArgs *  e)             { ArrayList * arl = new ArrayList; for(int i = 0; i <= 8; i++) arl->Add(dataGrid1->get_Item(0,  i)->ToString); MessageBox::Show(SBClassLib::Class1::BuildString(arl)); }

}; } Note You must add the common language runtime support compiler option (/clr:oldSyntax) in Visual C++ 2005 to successfully compile the previous code sample. To add the common language runtime support compiler option in Visual C++ 2005, follow these steps:
 * 1) Click Project, and then click   Properties.

Note  is a placeholder for the name of the project.
 * 1) Expand Configuration Properties, and then click General.
 * 2) Click to select Common Language Runtime Support, Old Syntax (/clr:oldSyntax) in the Common Language Runtime support project setting in the right pane, click Apply, and then click OK.

For more information about the common language runtime support compiler option, visit the following Microsoft Web site:

/clr (Common Language Runtime Compilation)

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

back to the top

Verify That Your Application Works

 * 1) Press F5 to run your application in debug mode.
 * 2) Form1 is displayed. Click Show Products.

The DataGrid control displays all the products from the Products table of the Northwind database.
 * 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 eight (zero-based).
 * 1) Press SHIFT+F5 to stop debugging and to return to Visual Studio .NET.

back to the top

Troubleshooting
Make sure that you do not confuse the syntax when you are working with three languages. Visual C++ .NET and Visual C# .NET are very particular, and the error messages are not always as prompt or as intuitive as the error messages in Visual Basic .NET. Therefore, you may want to bookmark the &quot;Language Equivalents&quot; section of the .NET Framework Software Development Kit (SDK) documentation to troubleshoot these difficulties. To do this, visit the following help topic in the Microsoft Developer Network (MSDN) Web site:

http://msdn2.microsoft.com/en-us/library/czz35az4(vs.71).aspx

back to the top

<div class="references_section">