The most significant difference between programming with the supported languages is, not surprisingly, their language syntax. Both C++ and Visual Basic have an established history and significant base of existing code, which were taken into account when they were updated for NGWS. C#, on the other hand, starts with a cleaner slate. Several advanced topics – such as the build process and defining namespaces – will be introduced with the corresponding sample code in the following sections, but here are some of the more obvious differences between the languages.
Case Sensitivity: C++ and C# are both case-sensitive, but VB is not. For a program to be CLS compliant, however, public members cannot differ only in their case. This restriction allows VB (and potentially other CLS-compliant languages) to both produce and use components created in other, case-sensitive, languages.
Referencing a Library: To use classes, which are located in namespaces, it is first necessary to obtain a reference to the assembly containing the desired namespace. All NGWS programs use – at a minimum – the System namespace, which is found in mscorlib.dll (typically located in the Windows System directory):
C++ #using <mscorlib.dll> C# (implicitly loaded) VB (implicitly loaded)
Note that MC++ uses a preprocessor directive (this may change before final release) and the file name of the DLL. C# and VB currently references the System assembly implicitly (this may also change), but for other assemblies it is necessary to use the /import
compile switch. Referenced libraries are generally located in the application directory or a subdirectory of the application. Libraries that are designed for use by many applications – for instance, tools provided by 3rd parties – are located in the assembly cache (currently %system%\assembly) and must follow specific guidelines. Application configuration files can provide additional options. For ASP+ applications, however, components should be located in "\Bin" subdirectory under the starting point for the application's virtual directory.
Importing a Namespace: Classes can be either referenced fully (e.g. System.IO.FileStream, similar to a fully qualified path name) or their namespace can be imported into the program, after which it is not necessary to fully qualify the contained class names. For convenient access to System objects, that namespace must be imported:
C++ using namespace System; C# using System; VB Imports System
Note that both MC++ and C# use a using
statement, while VB uses Imports
.
Referencing Object Members: Both VB and C# support the period as a scope resolution operator, which allows (for example) the syntax Console.WriteLine
when referencing the WriteLine
method of the Console
object. C++ uses a double colon "::" as a resolution operator:
C++ Console::WriteLine("xxxxx"); C# Console.WriteLine("xxxxx"); VB Console.WriteLine "xxxxx"
Declaring Objects: In Managed C++ and C# (though not in VB), variables must be declared before they can be used. Objects are instantiated using the new
keyword. The following are sample declaration / creation statements – declaring and creating an object of type Comp
, in namespace Lib
, with the name myComp
– in each of the three languages:
C++ Lib::Comp* myComp = new Lib::Comp(); C# Lib.Comp myComp = new Lib.Comp(); VB Dim myComp As New Lib.Comp
Program Entry Point: Every executable program has to have an external entry point, where the application begins its execution. The syntax hasn't changed for Managed C++, but in C# and VB everything happens in a class:
C++ void main() { } C# class MainApp { public static void Main() { } } VB Public Module modmain Sub Main() End Sub End Module
Behind the scenes, however, the Managed C++ compiler encapsulate the entry point in a class.
Defining a Namespace & Class: Each of the three languages supports the creation of custom namespaces as well as classes within those namespaces. All three languages handle this in code, though with slightly different syntax. For instance, note the "managed" class declaration for C++ and the fact that trailing semicolons are not needed for namespace and class declarations in C#:
C++ namespace CompVC { __gc class StringComponent { public: StringComponent() { } }; }; C# namespace CompCS { public class StringComponent { public StringComponent() { } } } VB Namespace CompVB Public Class StringComponent Public Sub New() End Sub End Class End Namespace