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 |
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 |
#DEFINE |
|||
Create a new instance of a class |
new |
NEWOBJECT( ) function |
|||
Create a new object |
CreateObject() for COM objects |
CoCreateInstance() (for COM objects) |
new ActiveXObject() |
CREATEOBJECT( ) function |
|
Assign an object to an object variable |
= |
= |
= |
= STORE |
|
Function/method does not return a value |
Sub2 |
void |
Void (COM servers only) |
||
Overload a function or method (Visual Basic: overload a procedure or method) |
(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 thisform |
||
Make a nonvirtual call to a virtual method of the current object |
MyClass::Func1(), where MyClass is a C++ class with a member function Func1. |
n/a |
n/a |
n/a |
|
Retrieve character from a string |
*(p + 10) or p[10] where p is a char* or wchar_t* |
str[10] where str is a string |
str[10], where str is a string charAt substring substr |
SUBSTR( ) |
|
Declare a compound data type (structure) |
class, interface |
n/a |
|||
Initialize an object (constructor) |
constructors (concept, not keyword) |
Constructors, or system default type 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) |
n/a |
Destroy event |
|
Guarantee that unmanaged resources are disposed of after use |
n/a |
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) |
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) |
n/a |
n/a |
|
Declare that an object can be modified asynchronously |
n/a |
n/a |
n/a |
||
Force explicit declaration of variables |
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 |
|||||
Test for an object variable that does not refer to an object |
pobj == NULL |
obj == null |
obj == undefined obj == null |
VARTYPE(obj)=="0" |
|
Value of an object variable that does not refer to an object |
nullptr |
null undefined |
.F. |
||
Test for a database null expression |
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 |
property: the property keyword refers to managed code |
n/a |
n/a |
Object-Oriented Programming
Purpose |
Visual Basic |
C++ |
C# |
JScript |
Visual FoxPro |
---|---|---|---|---|---|
Refer to a base class |
super |
BaseClass property ParentClass property DODEFAULT() Class::member |
|||
Declare an interface |
interface |
DEFINE CLASS |
|||
Specify an interface to be implemented |
(Just derive from the interface) class C1 : public I1 |
class C1 : I1 Interfaces |
IMPLEMENTS |
IMPLEMENTS NEW |
|
Declare a class |
class |
DEFINE CLASSMyClassAS <ParentClass> |
|||
Declare a module |
static class |
static class |
n/a |
n/a |
|
Declare a partial definition of a class or structure |
n/a |
n/a |
n/a |
||
Specify that a class can only be inherited. An instance of the class cannot be created |
abstract |
n/a |
|||
Specify that a class cannot be inherited |
final |
n/a |
|||
Declare an enumerated type |
enum |
n/a |
|||
Declare a class constant |
const (Applied to a field declaration) |
const |
#DEFINE |
||
Derive a class from a base class |
Class C1 : public Base (No language keyword needed for this purpose) |
class C1 : C2 |
Class c1 extends c2 |
DEFINE CLASS MyClass AS ParentClass |
|
Override a method or property |
(No language keyword required for this purpose except override for /clr compilations — see Derived Classes) |
(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 |
Put = 0 at the end of the declaration (pure virtual method) |
abstract |
(No language keyword required for this purpose) |
||
Declare a method that cannot be overridden |
NotOverridable (Methods are NotOverridable by default.) |
final |
n/a |
||
Declare a virtual method or property, or property accessor |
(Methods are virtual by default) |
n/a |
|||
Hide a base class member in a derived class |
n/a |
||||
Declare a typesafe reference to a class method |
Use the name of the function without parentheses |
n/a |
|||
Specify that a variable can contain an object whose events you wish to handle |
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 |
n/a |
n/a |
with9 |
WITH ... ENDWITH |
Exception Handling
Purpose |
Visual Basic |
C++ |
C# |
JScript |
Visual FoxPro |
---|---|---|---|---|---|
Structured exception handling |
try catch finally throw |
TRY [ tryCommands ] [ CATCH [ To VarName ] [ WHEN IExpression ] ] [ catchCommands ] ] [ THROW [ eUserExpression ] ] [ EXIT ] [ FINALLY [ finallyCommands ] ] ENDTRY |
|||
C++ exception handling |
n/a |
n/a |
n/a |
n/a |
Decision Structures
Purpose |
Visual Basic |
C++ |
C# |
JScript |
Visual FoxPro |
---|---|---|---|---|---|
Decision structure (selection) |
case |
switch case break |
CASE ICASE |
||
Decision structure (if ... then) |
if else |
IF ... ENDIF IIF( ) |
|||
Loop structure (conditional) |
do, while break, continue |
DO, WHILE (clauses) |
|||
Loop structure (iteration) |
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 |
int[] x = new int[5]; |
var x : int[] var arr = Array() |
DIMENSION DECLARE |
||
Initialize an array |
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 |
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 |
n/a |
|||
Visible only within the assembly in which declared |
internal |
n/a |
|||
Visible only within current or derived classes |
n/a |
n/a |
n/a |
||
Access is limited to the current assembly or types derived from the containing class. |
protected internal |
n/a |
n/a |
||
Visible only within the project (for nested classes, within the enclosing class) |
private |
n/a |
Member Scope
Purpose |
Visual Basic |
C++ |
C# |
JScript |
Visual FoxPro |
---|---|---|---|---|---|
Accessible outside class, project, and module |
public |
(No language keyword required for this purpose) |
|||
Accessible outside the class, but within the project or package |
public private: |
internal |
n/a |
||
Accessible only to current and derived classes |
protected |
PROTECTED |
|||
Only accessible within class or module |
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 internal |
n/a |
n/a |
Miscellaneous Lifetime
Purpose |
Visual Basic |
C++ |
C# |
JScript |
Visual FoxPro |
---|---|---|---|---|---|
Preserve procedure's local variables |
Static11 |
n/a |
n/a |
||
Shared by all instances of a class |
static |
n/a |
Miscellaneous
Purpose |
Visual Basic |
C++ |
C# |
JScript |
Visual FoxPro |
---|---|---|---|---|---|
Comment code |
//, /* */ for multiline comments |
* && NOTE |
|||
Case-sensitive? |
No |
Yes |
Yes |
Yes |
No |
Call Windows API |
n/a |
n/a |
DECLARE - DLL |
||
Declare and raise an event |
n/a |
n/a |
RAISEEVENT( ) function |
||
Threading primitives |
n/a |
n/a |
n/a |
||
Go to (branch) |
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