IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
To/From Code
-Collapse +Expand Cross Ref Guide
-Collapse +Expand Members-Only
Sign in to see member-only pages.
   ► KBTo/From GuidesC++/CLILanguage Basics  Print This     

Cross Ref > Language Basics

By Mike Prestwood

C++/CLI versus Delphi Prism: A side by side comparison between C++/CLI and Delphi Prism.

 
Language Basics
 

Language basics is kind of a catch all for absolute beginner stuff. The items (common names) I chose for language basics is a bit random and include items like case sensitivity, commenting, declaring variables, etc.

Case Sensitivity

[Other Languages] 

Languages Focus

Case sensitiviy in this case is referring to commands and variable names. For example, are "printf" and "PrintF" equivalent? Are fullname and FullName equivalent? When you create commands, operations, methods, or variables should you worry about case?

C++/CLI:   Yes

Same as standard C++. Both are case sensitive. In C and C++ commands and variable names are case sensitive.

Syntax Example:

The following first C++/CLI snippet works:

MessageBox::Show("Hello");
 
messagebox::SHOW("Hello"); //>>>Does not work!
Delphi Prism:   No

Prism is generally not case sensitive. Commands and variable names are not case sensitive.

Note: Prism (and Delphi for .Net) do not automatically match your typed case with the defined case as C# and VB.Net do within the Visual Studio Shell.

Syntax Example:  

The following demonstrates command and variable case insensitiviy.

var
 FullName: String;
begin
 fullname := 'Mike Prestwood';
 MessageBox.Show(fullNAME);
 MESSAGEBOX.SHOW(FULLNAME);
 messAGEbox.sHow(fullname);
end;




Code Blocks

[Other Languages] 

Languages Focus

The rules for code blocks within a language dictate where you can declare variables, how you "bracket" code, etc.

C++/CLI:   { }

Same as standard C++. For C++, Java, JavaScript, and PHP, I prefer to put the first { at the end of the first line of the code block as in the example above because I see more C++ formatted that way.

Syntax Example:
class Cyborg {
public: System::Void IntroduceYourself() {
MessageBox::Show("Hi");
}
};
Delphi Prism:   begin..end

Same as in Delphi for Win32 but Prism also supports inline variable declaration.

Syntax Example:
function DoSomething : integer;
var
 a, b : integer;
begin
 a := 1;
 b := 2;
 var c : integer; //Prism allows inline (local) variables.
 c := a + b;
  
 result := c;
end;




Comments

[Other Languages] 

Languages Focus

Commenting code generally has three purposes: to document your code, for psuedo coding prior to coding, and to embed compiler directives. Most languages support both a single line comment and a multiple line comment. Some languages also use comments to give instructions to the compiler or interpreter.

C++/CLI:   // or /* ... */

Commenting Code
Same as standard C++. C++ uses "//" for a single line comment and /* */ for a multiple line comment.

Syntax Example:
//Single line comment in MS (not ANSI compliant so do NOT use).
/* ANSI compliant single line comment. */
/*
Multiple line
comment.
*/
  
/*
* This is another popular
* way to write multi-line
* comments.
*/
Delphi Prism:   // or { ... } or (* ... *)

Commenting Code
Delphi uses // for a single line comment and both {} and (**) for multiple line comments. Although you can nest different types of multiple line comments, it is recommended that you don't.

Compiler Directives - $
A special comment. Delphi compiler directives are in the form of {$DIRECTIVE}. Of interest for comments is using the $IFDEF compiler directive to remark out code.

Syntax Example:
//This is a single line comment.
 
{
Multiple line
comment.
}
 
(*
This too is a
multiple line comment.
*)
 
{$IFDEF TEMPOUT}
//...code here
{$ENDIF}




Constants

[Other Languages] 

General Info: Computer Language Constants

A constant is just like a variable (it holds a value) but, unlike a variable, you cannot change the value of a constant.

C++/CLI:   const or literal

C++/CLI supports the const and static const keywords of standard C++ as well as the new literal keyword. A literal is equivalent to static const in standard C++ and Microsoft's documentation recommends to replace static const with the new literal keyword because a literal is available in metadata; a static const variable is not available in metadata to other compilers.

You can use static const within the class declaration or locally within a method. However, literal is only valid in the class declaration section and const is only valid within a method.

Syntax Example:  
//some method {
const String^ MyName = "John";
static const Int32 MyAge = 27;
//}
// public class SomeClass : public Object {
public:
  literal double Pi = 3.14159;
  literal String^ MyName = "Mike";
  static const Int32 MyAge = 35;
//...
Delphi Prism:   const kPI: Double=3.1459;

In Prism, you define constants similar to how you define variables but use the Const keyword instead of the Var keyword. Specifying the type is optional. If you don't specify the type, the compiler chooses the most appropriate type for you.

Declare class constants as part of the class definitions. Declare local constants above the begin..end. Although Prism support inline variables, inline constants are not supported.

Syntax Example:
//Specified type:
const
kFeetToMeter: Double = 3.2808;
  kMeterToFeet: Double = .3048; 
  kName: String = "Mike";

//Unspecified type:
const kPIShort = 3.14;




End of Statement

[Other Languages] 

Languages Focus

In coding languages, common End of statement specifiers include a semicolon and return (others exist too). Also of concern when studying a language is can you put two statements on a single code line and can you break a single statement into two or more code lines.

C++/CLI:   ;

Same as standard C++. C++ uses a semicolon ";" as an end of statement specifier and you can put multiple statements on a single line of code if you wish as well as split a single statement into two or more code lines.

Syntax Example:
//.Net WinForms example.
//Add, using namespace System::Windows::Forms;
MessageBox::Show("Hello1");
MessageBox::Show("Hello2");
MessageBox::Show("Hello3");
   
MessageBox::Show("Hello4"); MessageBox::Show("Hello5"); 
  
MessageBox:: 
  Show 
    ("Hello6");
Delphi Prism:   ;

Object Pascal uses a semicolon ";" as an end of statement specifier and you can put multiple statements on a single line of code and put a single statement on two or more code lines if you wish.

Syntax Example:  
MessageBox.Show("Hello1");
MessageBox.Show("Hello2");
MessageBox.Show("Hello3");

//Same line works too:
MessageBox.Show("Hello4");  MessageBox.Show("Hello5");

//Two or more lines works too:
MessageBox.Show
("Hello6");




Literals

[Other Languages] 

General Info: Programming Literals

A value directly written into the source code of a computer program (as opposed to an identifier like a variable or constant). Literals cannot be changed. Common types of literals include string literals, floating point literals, integer literals, and hexidemal literals. Literal strings are usually either quoted (") or use an apostrophe (') which is often referred to as a single quote. Sometimes quotes are inaccurately referred to as double quotes.

Languages Focus

In addition to understanding whether to use a quote or apostrophe for string literals, you also want to know how to specify and work with other types of literals including floating point literals. Some compilers allow leading and trailing decimals (.1 + .1), while some require a leading or trailing 0 as in (0.1 + 0.1). Also, because floating point literals are difficult for compilers to represent accurately, you need to understand how the compiler handles them and how to use rounding and trimming commands correctly for the nature of the project your are coding.

C++/CLI:   qoute

Same as standard C++. String literals are quoted as in "Prestwood". If you need to embed a quote use a slash in front of the quote as in \".

To specify a floating point literal between 1 and -1, you can preceed the decimal with a 0 or not (both work). In other words, preceding and following decimals are allowed (both .1 and 0.1). Trailing decimals are also allowed (1, 1., and 1.0 are all equivalent and allowed).

Syntax Example:
MessageBox::Show("Hello");
MessageBox::Show("Hello \"Mike\".");
  
//Does ASP evaluate this simple
//floating point math correctly? No! 
if ((.1 + .1 + .1) == 0.3) {
MessageBox::Show("Correct");
} else {
MessageBox::Show("Not correct");
}
Delphi Prism:   quote or apostrophe

In Prism, you use either quotes or apostrophes for string literals.

Different than Delphi, you can start floating point literals with a decimal or an integer. For example, to specify a fractional floating point literal between 1 and -1, you can preceed the decimal with a 0 or not.

x := .1 + .1;     //Does work.
x := 0.1 + 0.1;   //Does work.
Syntax Example:  
MessageBox.Show('Hello');
MessageBox.Show("Hello");

//Example of embedding quotes and apostropes:
MessageBox.Show('He said, "Who''s computer?"');
MessageBox.Show("She said, ""Mike's computer"".");




Variables

[Other Languages] 

Languages Focus

A variable holds a value that you can use and change throughout your code so long as the variable is within scope. With variable declaration, you not only want to know the syntax of how you declare a variable but you also want to know where. Are you allowed to declare a variable inline? What are the available scopes: local vs. global. Can you assign a value at the same time you declare a variable?

C++/CLI: 
Delphi Prism:   var x: Integer := 0;

Prism supports type inference where you just use a variable and the compiler will then choose the lowest type possible (such as an Integer before a LongInt). With Prism, you frequently do not have to use commands to convert from one type to another.

Variable names are not case sensitive. The Prism language offers both old-style declaring variables before the begin as well as in-line variable declaration.

Prism does support variable initialization too.

Prism offers many variable types. Some common variable types include Integer, LongInt, Single, Double, Boolean, and String.

Syntax Example:  
var
FName: String; //This is old-style.
begin
FName := "Mike Prestwood";
MessageBox.Show(FName);

Var Age: LongInt; //Local variables.
Age:=36;
MessageBox.Show(Fname + " is " + Age + " years old");

//Assign values too...
Var Wife: String:="Lisa"; Var WifeAge: Integer:=32;
messagebox.Show(wife + " is " + Wifeage + ".");
end;




Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site: 
www.prestwood.com


©1995-2024 Prestwood IT Solutions.   [Security & Privacy]