Keywords Compared in Different Languages

This topic lists common programming tasks that can be summarized with a language keyword. For more information about tasks that need code examples, see Programming Concepts Compared in Different Languages with Code Examples.

Purpose Visual Basic Visual J# C++ C# NEW  JScript Visual FoxPro
Declare a variable Private, Public, Friend, Protected, Static1, Shared, Dim public, private, protected

static, final, volatile, transient

declarators (concept, not keyword) declarators (keywords include user-defined types and built-in types) var [implicit declaration]; also PUBLIC, LOCAL, PRIVATE
Declare a named constant Const final const const

readonly

const NEW  #DEFINE
Create a new object New
new new new new n/a
  CreateObject() n/a CoCreateInstance() (for COM objects)   new ActiveXObject() CREATEOBJECT; NEWOBJECT
Assign an object to an object variable = = = = = =; also STORE
Function/method does not return a value Sub2 void void void void Void (COM servers only)
Overload a function or method (Visual Basic: overload a procedure or method) Overloads NEW  (No language keyword required for this purpose) (No language keyword required for this purpose) (No language keyword required for this purpose) (No language keyword required for this purpose) (No language keyword required for this purpose)
Refer to the current object Me3 this this this this This;

Thisform

Make a nonvirtual call to a virtual method of the current object MyClass n/a MyClass:Func1(), where MyClass is a C++ class with a member function Func1. n/a n/a n/a
Retrieve character from a string GetChar Function NEW  "" *(p + 10) or p[10] where p is a char* [] str[10], where str is a string, charAt, substring, substr4 SUBSTR( )
Declare a compound data type (Visual Basic: Structure) Structure <members> End Structure class, interface class, struct, union

__interface

struct, class, interface class, interface NEW  n/a
Initialize an object (constructors) Sub New()5 Constructors (concept, not keyword) constructors (concept, not keyword) Constructors, or system default type constructors constructor (concept not keyword) 6 Init event
Terminate an object directly n/a n/a ~ClassName n/a n/a n/a
Method called by the system just before garbage collection reclaims an object7 Finalize NEW (In Visual Basic 6.0, Class_Terminate) finally destructor destructor n/a Destroy event
Initialize a variable where it is declared Dim x As Long = 5

Dim c As New Car(FuelTypeEnum.Gas)

int x = 5;

//or initialized by constructor

C c(10);

int x=5; // initialize to a value:

int x = 123;

// or use default constructor:

int x = new int();

var x = 5

var y : car = new car()

n/a
Take the address of a function AddressOf (For class members, this operator returns a reference to a function in the form of a delegate instance) delegate, multicast, /** @delgate */ __delegate delegate Use the name of the function without parentheses n/a
Callback n/a Pass the address of one function to another that calls the invoker back. For an example, see Using Callback Functions. CALLBACK (a standard type);
callback (IDL attribute)
n/a n/a n/a
Declare that an object can be modified asynchronously n/a volatile volatile volatile n/a n/a
Force explicit declaration of variables Option Explicit n/a. All variables must be declared prior to use. n/a. All variables must be declared prior to use. n/a. (All variables must be declared prior to use) fast mode (on by default) _VFP.LanguageOptions NEW 
Test for an object variable that does not refer to an object obj = Nothing pObj == null pobj == NULL obj == null obj == undefined

obj == null

EMPTY(); ISNULL()
Value of an object variable that does not refer to an object Nothing null n/a null null

undefined

n/a
Test for a database null expression IsDbNull Supported by various data types in the System.Data.SqlTypes namespace n/a n/a x == null ISNULL( )
Test whether a Variant variable has been initialized n/a n/a n/a n/a x == undefined EMPTY( )
Define a default property Default NEW  "" n/a by using indexers n/a n/a

Object-Oriented Programming

Purpose Visual Basic Visual J# C++ C# NEW  JScript Visual FoxPro
Refer to a base class MyBase NEW  super __super base super NEW  BaseClass property; ParentClass property; DODEFAULT()
Declare an interface Interface NEW  interface __interface interface interface NEW  DEFINE CLASS
Specify an interface to be implemented Implements (statement) implements (clause on class declaration) (Just derive from the interface)

class C1 : public I1

class C1 : I1 implements NEW  IMPLEMENTS NEW 
Declare a class Class NEW <implementation>
class class class class NEW  DEFINE CLASS MyClass AS <BaseClass>
Specify that a class can only be inherited. An instance of the class cannot be created. MustInherit NEW  abstract __abstract8 (Only in Managed Extensions for C++) abstract abstract NEW  n/a
Specify that a class cannot be inherited NotInheritable NEW  final __sealed (Only in Managed Extensions for C++) sealed final NEW  n/a
Declare an enumerated type Enum <members> End Enum n/a enum enum enum NEW  n/a
Declare a class constant Const static final (Applied to a field declaration) const const (Applied to a field declaration) const NEW  #DEFINE
Derive a class from a base class Inherits C2 NEW  class C1 extends C2 Class C1 : public Base (No language keyword needed for this purpose) class C1 : C2 class c1 extends c2 NEW  DEFINE CLASS MyClass AS ParentClass
Override a method Overrides NEW  (No language keyword required for this purpose) (No language keyword required for this purpose) override (No language keyword required for this purpose) (No language keyword required for this purpose)
Declare a method that must be implemented in a deriving class MustOverride NEW  abstract Put = 0 at the end of the declaration (pure virtual method) abstract abstract NEW  (No language keyword required for this purpose)
Declare a method that can't be overridden NotOverridable NEW (Methods are not overridable by default.) final __sealed (Only in Managed Extensions for C++) sealed final NEW  n/a
Declare a virtual method, property (Visual Basic), or property accessor (C#, C++) Overridable (Methods are virtual by default) virtual virtual (Methods are virtual by default) n/a
Hide a base class member in a derived class Shadowing n/a n/a n/a new Modifier n/a
Declare a typesafe reference to a class method Delegate NEW  delegate, multicast, /** @delgate */ __delegate (Only in Managed Extensions for C++) delegate Use the name of the function without parentheses n/a
Specify that a variable can contain an object whose events you wish to handle WithEvents n/a n/a (Write code - no specific keyword) (Write code - no specific keyword) EVENTHANDLER( )  NEW 
Specify the events for which an event procedure will be called Handles NEW (Event procedures can still be associated with a WithEvents variable by naming pattern.) n/a n/a n/a n/a n/a
Evaluate an object expression once, in order to access multiple members With objExpr <.member> <.member> End With n/a n/a n/a with9 WITH ... ENDWITH

Exception Handling

Purpose Visual Basic Visual J# C++ C# NEW  JScript Visual FoxPro
Structured exception handling Try NEW <attempt>
Catch
<handle errors>
Finally
<always execute>
End Try
try, catch, finally, throw __try, __except, __finally try, catch, finally,
throw
try, catch, finally, throw ONERROR( ), COMRETURNERROR( ), ERROR(); MESSAGE(), AERROR()
C++ exception handling n/a n/a try, catch, throw n/a n/a n/a

Decision Structures

Purpose Visual Basic Visual J# C++ C# NEW  JScript Visual FoxPro
Decision structure
(selection)
Select Case ..., Case, Case Else, End Select switch, case, break, default switch, case, default, goto, break switch, case, default, goto, break switch, case, break CASE
Decision structure (if ... then) If ... Then, ElseIf ... Then, Else, End If if, else if, else if, else if, else IF ... ENDIF
Loop structure (conditional) While, Do [While, Until] ..., Loop [While, Until] do, while, continue do, while, continue do, while, continue do, while, break, continue DO, WHILE (clauses)
Loop structure
(iteration)
For ..., [Exit For,] Next

For Each ..., [Exit For,] Next

for, break for for, foreach for (x=0;x<10;x++){...}

for (prop in obj) { print (obj[prop]);}

FOR (clauses), FOR ... ENDFOR, Continue, Next

Arrays

Purpose Visual Basic Visual J# C++ C# NEW  JScript Visual FoxPro
Declare an array Dim a() As Long int[] x = new int[5]; int x[5]; int[] x = new int[5]; var x : int[], var arr = Array() DIMENSION, DECLARE
Initialize an array Dim a() As Long = {3, 4, 5} int[] x = {1,2,3,4,5}; int x[5]= {1,2,3,4,5}; int[] x = new int[5] {1, 2, 3, 4, 5}; var x : int[] = [1, 2, 3, 4, 5], var arr = new Array(1, 2, 3, 4, 5)] DIMENSION, DECLARE
Reallocate array Redim n/a n/a n/a arr.length=newSize(only for JScript arrays)10 DIMENSION, DECLARE

Class Scope

Purpose Visual Basic Visual J# C++ C# NEW  JScript Visual FoxPro
Visible outside the project or assembly Public public public public public NEW  PUBLIC
Invisible outside the assembly (C#/Visual Basic) or within the package (Visual J#, JScript) Friend (Omitting the scope keyword specifies "package scope") private internal internal NEW  n/a
Visible only within the project (for nested classes, within the enclosing class) Private private private private private NEW  HIDDEN

Member Scope

Purpose Visual Basic Visual J# C++ C# NEW  JScript Visual FoxPro
Accessible outside class and project (Visual J#/JScript/C++/Visual Basic) or module (Visual Basic) Public public public public public NEW  PUBLIC
Accessible outside the class, but within the project (C#, Visual Basic, C++) or package (Visual J#, JScript) Friend (Omitting the scope keyword specifies "package scope") public private: internal internal NEW  n/a
Only accessible within class (Visual J#/JScript/C++/Visual Basic) or module (Visual Basic) Private private private private private NEW  HIDDEN
Only accessible to current and derived classes Protected NEW  protected protected protected protected NEW  PROTECTED
Specify that a function or another class has access to private members of the declaring class n/a n/a friend (Not allowed in the Managed Extensions for C++) n/a n/a n/a
Protected inside the assembly and private to other assemblies n/a n/a protected private:
(Only in Managed Extensions for C++)
n/a n/a n/a

Misc. Lifetime

Purpose Visual Basic Visual J# C++ C# NEW  JScript Visual FoxPro
Preserve procedure's local variables Static11 static static n/a n/a PRIVATE
Shared by all instances of a class Shared NEW  static static static static NEW  n/a

Misc.

Purpose Visual Basic Visual J# C++ C# NEW  JScript Visual FoxPro
Comment code '

Rem

//

/**

*/

/*

*/

//, /* */ for multiline comments //, /* */ for multiline comments

/// for XML comments

//, /* */ for multiline comments *; &&
Case-sensitive? No Yes Yes Yes Yes No
Call Windows API Declare <API> Use J/Direct or Platform Invoke n/a use Platform Invoke n/a DECLARE - DLL
Declare and raise an event Event, RaiseEvent Use the java.awt.Event class or /** @event */ n/a event n/a n/a
Threading primitives SyncLock synchronized   lock n/a n/a
Go to Goto n/a goto goto n/a n/a

1. The only place where Static can be used by itself to declare a variable — for example, Static x As Long — is within a procedure.

2. In Visual Basic, procedures declared with the Sub keyword cannot return values. If a procedure is to return a value, you must declare it with the Function keyword.

3. Me is not resolved at compile time, so you can use it as the return value of a property or method.

4. The substr function is still supported, but is no longer the preferred way to access characters within a string. The most efficient way to access a character from a particular location in a string is using brackets. For example, to access the tenth character in the string str, use str[10].

5. In Visual Basic, constructors for classes derived from .NET Framework System.Object are always named New.

6. Overloading is not allowed on constructors in JScript.

7. Typically, code in this method frees system resources that would not automatically be freed by the garbage collector.

8. In C++ an abstract class includes at least one pure virtual member.

9. Note there is no leading period such as you would use in Visual Basic. This feature can easily cause confusion, because variables may be mistaken for properties, and vice versa. Also note that the with statement produces slow code.

10. Does not reallocate the array, and does not "grow" it either. JScript arrays (declared as type Array) are always sparse and dynamic. Native arrays (declared as System.Array or as type[]) are not dynamic.

11. Static local variables of nonshared class methods are stored per class instance in Visual Basic rather than sharing a single copy, as in other languages. When Static is used to declare a variable, the value of that variable is preserved even if the variable looses and then regains scope.

See Also

Language Equivalents | Programming Concepts Compared in Different Languages with Code Examples | Operators Compared in Different Languages | Data Types Compared in Different Languages | Controls and Programmable Objects Compared in Different Languages and Libraries