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

Article translations Article translations
Article ID: 816174 - View products that this article applies to.
Expand all | Collapse all

On This Page

SUMMARY

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

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

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.
  4. In the Name text box, type HowToMultiComp, and then click OK.
  5. 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:
  1. In Solution Explorer, right-click Solution 'HowToMultiComp', point to Add, and then click New Project.
  2. 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.
  3. 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.
  4. 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
  5. 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("select * from products", New SqlConnection_
                    ("server=<network address of instance of SQL Server>;database=northwind;integrated security=sspi"))
            sqlDS.Fill(ds)
            Return ds
    End Function
    Note Modify the connection string as appropriate for your environment.
To add the Visual C# .NET or Visual C# 2005 Class Library project, follow these steps:
  1. In Solution Explorer, right-click Solution 'HowToMultiComp', point to Add, and then click New Project.
  2. 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.
  3. 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.
  4. 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;
  5. 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(", ");
    	}
    	return sb.ToString();
    }
Use your Visual C++ .NET or Visual C++ 2005 Windows Forms application to consume the two class libraries that you created earlier:
  1. To add references to the two class libraries that you created earlier, follow these steps:
    1. In Solution Explorer, right-click References under HowToMultiComp, and then click Add Reference.
    2. On the Projects tab of the Add Reference dialog box, press and hold the CTRL key, click NWClassLib, and then click SBClassLib.
    3. 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.
  2. Verify that Form1 is open in Design view. If Form1 is not already open, double-click Form1.h in Solution Explorer.
  3. Press the CTRL+ALT+X key combination to open the toolbox, and then click Windows Forms.
  4. Add a Button control to Form1, and then press F4 to access the Properties pane for this button control.
  5. Change the Text property to Show Products.
  6. Add another Button control to Form1, and then change the Text property to Build String.
  7. Add a DataGrid control to Form1 under the button controls.
  8. Resize the DataGrid control so that it fills the remaining area on Form1.
  9. 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.
  10. 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.
  11. To create a second Click event handler, double-click Build String.
  12. 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));

Complete Code Listing

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("select * from products", New SqlConnection_
            ("server=<network address of instance of SQL Server>;database=northwind;integrated security=sspi"))
        sqlDS.Fill(ds)
        Return ds
    End Function
End Class

Complete Code Listing (Class1.cs)

using System;
using System.Text;
using System.Collections;
namespace SBClassLib
{
	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	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();
		}
	}
}

Complete Code Listing (Form1.h)

#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"button1";
			this->button1->Size = System::Drawing::Size(95, 23);
			this->button1->TabIndex = 0;
			this->button1->Text = S"Show Products";
			this->button1->Click += new System::EventHandler(this, button1_Click);
			// 
			// button2
			// 
			this->button2->Location = System::Drawing::Point(304, 32);
			this->button2->Name = S"button2";
			this->button2->Size = System::Drawing::Size(95, 23);
			this->button2->TabIndex = 1;
			this->button2->Text = S"Build String";
			this->button2->Click += new System::EventHandler(this, button2_Click);
			// 
			// dataGrid1
			// 
			this->dataGrid1->DataMember = S"";
			this->dataGrid1->HeaderForeColor = System::Drawing::SystemColors::ControlText;
			this->dataGrid1->Location = System::Drawing::Point(8, 64);
			this->dataGrid1->Name = S"dataGrid1";
			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"Form1";
			this->Text = S"Form1";
			(__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 <ProjectName> Properties.

    Note <ProjectName> is a placeholder for the name of the project.
  2. Expand Configuration Properties, and then click General.
  3. 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

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.
  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 eight (zero-based).
  4. Press SHIFT+F5 to stop debugging and to return to Visual Studio .NET.

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 "Language Equivalents" 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

REFERENCES

For more information about creating a multiproject solution, visit the following (MSDN) Web site:
http://msdn2.microsoft.com/en-us/library/aa265251(VS.60).aspx

Properties

Article ID: 816174 - Last Review: June 1, 2007 - Revision: 2.3
APPLIES TO
  • Microsoft .NET Framework 1.1
  • Microsoft Visual C++ 2005 Express Edition
  • Microsoft Visual C++ .NET 2003 Standard Edition
Keywords: 
kbwindowsforms kbmerge kbdataadapter kbhowtomaster KB816174

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com