Dela via


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

C++

C#

JScript

Visual FoxPro

Declare a variable

Dim

Public

Friend

Protected

Private

Shared

Static1

declarators (concept, not keyword)

declarators (keywords include user-defined types and built-in types)

var

[implicit declaration]

PUBLIC

LOCAL

PRIVATE

Declare a named constant

Const

const

const

readonly

const

#DEFINE

Create a new instance of a class

New

new

gcnew

new

new

NEWOBJECT( ) function

Create a new object

New

CreateObject() for COM objects

CoCreateInstance() (for COM objects)

new 

new ActiveXObject()

CREATEOBJECT( ) function

Assign an object to an object variable

=

=

=

=

=

STORE

Function/method does not return a value

Sub2

void

void

void

Void (COM servers only)

Overload a function or method (Visual Basic: overload a procedure or method)

Overloads

(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

thisform

Make a nonvirtual call to a virtual method of the current object

MyClass

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

*(p + 10) or p[10] where p is a char* or wchar_t*

str[10] where str is a string

string4

str[10], where str is a string

charAt

substring

substr

SUBSTR( )

Declare a compound data type (structure)

Structure

Class

Interface

class

struct

union

__interface

struct

class

interface

class, interface

n/a

Initialize an object (constructor)

Sub New()5

constructors (concept, not keyword)

Constructors, or system default type constructors

Class Constructors

Constructor (concept, not keyword)6

Init event

Terminate an object directly

n/a

~ClassName

n/a

n/a

n/a

Method called by the system just before garbage collection reclaims an object7

Finalize (in Visual Basic 6.0, Class_Terminate)

Destructors (C+) (concept, not keyword)

Destructors

n/a

Destroy event

Guarantee that unmanaged resources are disposed of after use

Using

n/a

using

n/a

n/a

Initialize a variable where it is declared

Dim x As Long = 5

Dim c As New Car(FuelTypeEnum.Gas)

// initialize to a value:

int x=5;

//with an appropriate constructor:

C c(10);

// initialize to a value:

int x = 123;

// or use default constructor:

int x = new int();

var x = 5

var y : car = new car()

LOCAL x

x = 5

Take the address of a function

AddressOf (This operator returns a reference to a function in the form of a delegate instance)

delegate

delegate

Use the name of the function without parentheses

n/a

Callback

Pass the address of one function to another that calls the invoker back. For an example, see How to: Pass Procedures to Another Procedure in Visual Basic.

CALLBACK (a standard type)

callback (IDL attribute)

delegate

n/a

n/a

Declare that an object can be modified asynchronously

n/a

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)

fast mode (on by default)

_VFP.LanguageOptions NEW 

Enable local type inference

Option Infer

Test for an object variable that does not refer to an object

obj Is Nothing

pobj == NULL

obj == null

obj == undefined

obj == null

VARTYPE(obj)=="0"

Value of an object variable that does not refer to an object

Nothing

nullptr

null

null

undefined

.F.

Test for a database null expression

IsDbNull

n/a

n/a

x == null

ISNULL( )

Test whether a Variant variable has been initialized

n/a

n/a

n/a

x == undefined

EMPTY( )

Define a default property

Default

property: the property keyword refers to managed code

Indexers

n/a

n/a

Object-Oriented Programming

Purpose

Visual Basic

C++

C#

JScript

Visual FoxPro

Refer to a base class

MyBase

__super

base

super

BaseClass property

ParentClass property

DODEFAULT()

Class::member

Declare an interface

Interface

__interface

interface class

interface

interface

DEFINE CLASS

Specify an interface to be implemented

Implements

(Just derive from the interface)

class C1 : public I1

class C1 : I1

Interfaces

IMPLEMENTS

IMPLEMENTS NEW 

Declare a class

Class

class

Classes and Structs (Managed)

class

class

DEFINE CLASSMyClassAS <ParentClass>

Declare a module

Module

static class

static class

n/a

n/a

Declare a partial definition of a class or structure

Partial

n/a

Partial

n/a

n/a

Specify that a class can only be inherited. An instance of the class cannot be created

MustInherit

abstract (Visual C+)8

abstract

abstract

n/a

Specify that a class cannot be inherited

NotInheritable

sealed

sealed

final

n/a

Declare an enumerated type

Enum

enum

enum

enum

n/a

Declare a class constant

Const

const

const (Applied to a field declaration)

const

#DEFINE

Derive a class from a base class

Class C1 Inherits C2

Class C1 : public Base (No language keyword needed for this purpose)

class C1 : C2

class

Class c1 extends c2

DEFINE CLASS MyClass AS ParentClass

Override a method or property

Overrides

(No language keyword required for this purpose except override for /clr compilations — see Derived Classes)

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

Put = 0 at the end of the declaration (pure virtual method)

abstract

abstract

(No language keyword required for this purpose)

Declare a method that cannot be overridden

NotOverridable (Methods are NotOverridable by default.)

sealed

sealed

final

n/a

Declare a virtual method or property, or property accessor

Overridable

virtual

virtual

(Methods are virtual by default)

n/a

Hide a base class member in a derived class

Shadowing

new (new slot in vtable)

new

new Modifier

n/a

Declare a typesafe reference to a class method

Delegate

delegate

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

(Write code - no specific keyword)

(Write code - no specific keyword)

EVENTHANDLER( ) NEW 

Specify the events for which an event procedure will be called

Handles (Event procedures can still be associated with a WithEvents variable by naming pattern)

n/a

event += eventHandler;

n/a

BINDEVENTS( )

Evaluate an object expression once, in order to access multiple members

With... End With

n/a

n/a

with9

WITH ... ENDWITH

Exception Handling

Purpose

Visual Basic

C++

C#

JScript

Visual FoxPro

Structured exception handling

Try... Catch... Finally... End Try

Throw

__try, __except

__finally

try, catch, finally

throw

try

catch

finally

throw

TRY

   [ tryCommands ]

[ CATCH [ To VarName ] [ WHEN IExpression ] ]

   [ catchCommands ] ]

[ THROW [ eUserExpression ] ]

[ EXIT ]

[ FINALLY

   [ finallyCommands ] ]

ENDTRY

C++ exception handling

n/a

try, catch, throw

n/a

n/a

n/a

Decision Structures

Purpose

Visual Basic

C++

C#

JScript

Visual FoxPro

Decision structure (selection)

Select... Case... End Select

switch, case, default

goto

break

switch

case

default

goto

break

switch

case

break

CASE

ICASE

Decision structure (if ... then)

If... Then... Else... End If

ElseIf

if, else

if, else

if

else

IF ... ENDIF

IIF( )

Loop structure (conditional)

While... End While

Do...Loop

do, while

continue

do

while

continue

do, while

break, continue

DO, WHILE (clauses)

Loop structure (iteration)

For... Next

For Each... Next

for

for

foreach

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

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

FOR (clauses)

FOR ... ENDFOR

Continue

NEXT

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

Arrays

Purpose

Visual Basic

C++

C#

JScript

Visual FoxPro

Declare an array

Dim a() As Long

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[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)]

x[1] = 1

x[2] = 2

Reallocate array

Redim

n/a

n/a

arr.length=newSize (only for JScript arrays)10

DIMENSION

DECLARE

Class Scope

Purpose

Visual Basic

C++

C#

JScript

Visual FoxPro

Visible outside the project or assembly

Public

public

public

public

n/a

Visible only within the assembly in which declared

Friend

private

internal

internal

n/a

Visible only within current or derived classes

Protected

n/a

Protected

n/a

n/a

Access is limited to the current assembly or types derived from the containing class.

Protected Friend

Type and Member Visibility

protected internal

Accessibility Levels

n/a

n/a

Visible only within the project (for nested classes, within the enclosing class)

Private

private

private

private

n/a

Member Scope

Purpose

Visual Basic

C++

C#

JScript

Visual FoxPro

Accessible outside class, project, and module

Public

public

public

public

(No language keyword required for this purpose)

Accessible outside the class, but within the project or package

Friend

public private:

internal

internal

n/a

Accessible only to current and derived classes

Protected

protected

protected

protected

PROTECTED

Only accessible within class or module

Private

private

private

private

HIDDEN

Specify that a function or another class has access to private members of the declaring class

n/a

friend (Not allowed in C+)

friend

n/a

n/a

Protected inside the assembly and private to other assemblies

n/a

protected private

n/a

n/a

n/a

Access is limited to the current assembly or types derived from the containing class

Protected Friend

Type and Member Visibility

protected internal

Accessibility Levels

n/a

n/a

Miscellaneous Lifetime

Purpose

Visual Basic

C++

C#

JScript

Visual FoxPro

Preserve procedure's local variables

Static11

static

static

n/a

n/a

Shared by all instances of a class

Shared

static

static

static

n/a

Miscellaneous

Purpose

Visual Basic

C++

C#

JScript

Visual FoxPro

Comment code

'

Rem

//, /* */ for multiline comments

//, /* */ for multiline comments

/// for XML comments

//, /* */ for multiline comments

*

&&

NOTE

Case-sensitive?

No

Yes

Yes

Yes

No

Call Windows API

Declare <API>

n/a

use Platform Invoke

n/a

DECLARE - DLL

Declare and raise an event

Event

RaiseEvent

n/a

event

n/a

RAISEEVENT( ) function

Threading primitives

SyncLock

n/a

lock

n/a

n/a

Go to (branch)

Goto

goto

goto

n/a

n/a

1 In Visual Basic, 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 In Visual Basic, Me is not resolved at compile time, so you can use it as the return value of a property or method.

4 In JScript, 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 In JScript, overloading is not allowed on constructors.

7 Typically, code in such a 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 In JScript, there is no leading period such as you would use in Visual Basic. This feature can easily cause confusion, because variables can be mistaken for properties, and vice versa. Also note that the with statement produces slow code.

10 In JScript, this 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 In Visual Basic, static local variables of nonshared class methods are stored per class instance 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 loses and then regains scope.

See Also

Reference

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

Other Resources

Language Equivalents