Table 2-1. Program Fundamentals Equivalent Chart
Table 2-1. Program Fundamentals Equivalent Chart
Table 2-1. Program Fundamentals Equivalent Chart
CHAPTER 2
Program Fundamentals
/*Start of comments
...
End of comments */
///XML comments
Dim var1 As type, var2 As type Dim var1, var2 As type type var1, var2;
Public Sub procedure(ByRef _ Public Sub procedure(ByRef param public void procedure(ref type
param As type) As type) param)
ByVal ByVal
out
3
488c02.fm Page 4 Wednesday, October 31, 2001 7:15 PM
Chapter 2
Public Sub procedure (ParamArray _ Public Sub procedure (ParamArray _ public void procedure(type
param() As type) param() As type) params[])
Overview
Program fundamentals consist of all the little things that are required of almost
every program, including writing code across multiple lines, writing comments,
declaring variables, and declaring procedures. Although each of these tasks can
seem almost trivial when compared to the other aspects of programming, it’s
essential to have a thorough understanding of how they work.
4
488c02.fm Page 5 Wednesday, October 31, 2001 7:15 PM
Program Fundamentals
doesn’t need this line because the System namespace is built in to the IDE. The
VB .NET equivalent for the using statement is Imports.
C# uses the Namespace keyword to define the application’s namespace. It
defaults to the name of your application, but you can type in a different name.
A namespace declares the scope for objects within it and helps you organize
your code. VB .NET doesn’t use the Namespace keyword by default (though it is
supported in the language). Instead, the namespace is set via the IDE menu.
Select Project ➤ Properties and you can enter the VB .NET application’s
namespace in the dialog box.
Next in the C# template are the XML comments. This is simply an example of
how XML comments look and it gives you some starter comments. VB .NET
doesn’t have these comments in its template because it doesn’t have the capa-
bility to do XML comments.
C# defines a default class for you. VB .NET uses a module instead. VB
.NET modules are similar to a class, but they are shared and accessible any-
where (i.e., global scope) within their namespace.
C# defines the Main() procedure inside the default class. It uses the static
modifier because the Main() procedure isn’t associated with an instance of the
class. VB .NET puts the Main() procedure within the module. The Main() procedure
is where an application starts. There can only be one per application.
Case Sensitivity
If a compiler is case sensitive, it treats as unique procedures and variables with
the same name if the case of the letters do not match.
VB 6.0 and VB .NET are not case sensitive. A variable called FirstName is the
same as the variable called firstName. Because of this, the IDE for both languages
automatically converts the case of your procedure and variable names to match
how they were declared. This ensures that you don’t mistakenly think that two
variables are different.
C# is case sensitive. If you are coming from a VB 6.0 background, you
might have the habit of only worrying about entering the proper case when
declaring a variable. You know that after you declare a variable properly you
can just enter it as lowercase and the IDE will fix it for you. This doesn’t work
with C#.
A common way of declaring variables in C# is to use camelCasing for private
members of a class and use PascalCasing for anything exposed externally. camel-
Casing uses uppercase for the first letter of every word except the first word. For
example, firstName and socialSecurityNumber use camelCasing. PascalCasing
uses uppercase for the first letter of every word. For example, FirstName and
SocialSecurityNumber is PascalCasing. Although VB .NET isn’t case sensitive, I find
5
488c02.fm Page 6 Wednesday, October 31, 2001 7:15 PM
Chapter 2
VB 6.0/VB .NET
Use the underscore character to mark that a statement will continue to the next
line. It is important to remember, and easy to miss, that the underscore must be
preceded with a space. A common error is to type the underscore immediately
after typing the last character on a line.
'Demonstrate concatenating two strings together with the "&". The strings are
'on different lines. The "_" is used to tell the compiler to use the
'second line.
var = "This is a very, very, very long string " & _
"that takes two lines to write."
C#
Because each statement ends with a semicolon, no line continuation character
is needed to mark that a statement continues to the next line. The exception to
this is the // comment tag that only applies to the line it is on. This is discussed
in the next section.
//Demonstrate concatenating two strings together with the "+". The strings are
//on different lines.
var = "This is a very, very, very long string " +
"that takes two lines to write.";
Comments
Comments play an essential part in documenting your code. This ensures that
your program is understandable and maintainable by other programmers (as
well as yourself). Although many programs have separately written programmer’s
documentation, nothing can take the place of a bunch of short, in-line comments
placed at critical points in your program code.
6
488c02.fm Page 7 Wednesday, October 31, 2001 7:15 PM
Program Fundamentals
VB 6.0/VB .NET
Comments use a single apostrophe and the compiler ignores everything that
comes after it.
7
488c02.fm Page 8 Wednesday, October 31, 2001 7:15 PM
Chapter 2
C#
Single-line comments use two forward slashes (//).
Multiline comment blocks use /* and */ to begin and end a block of comments.
XML comments are defined using three forward slashes (///) and they must
follow the standards for well-defined XML.
/// <summary>
/// This is a sample of XML code.
/// </summary>
///<code>
string month;
month = "September";
///</code>
You can create any tag you feel is necessary. However, there are some standard
tags you should use to make your comments consistent with other program-
mers’ work.1
1. From Microsoft’s “C# Programmer’s Reference,” which you can find in the MSDN Help
file. Use the index and type in XML Documentation.
8
488c02.fm Page 9 Wednesday, October 31, 2001 7:15 PM
Program Fundamentals
Table 2-3. Standard Tags for Documenting Comments Using XML (Continued)
XML TAG DESCRIPTION
<para> A paragraph
Declaring Variables
Declaring variables is a way of telling the compiler which data type a variable
represents2. Each of the three languages does this differently. There are three
major syntax differences to be aware of: declaring a single variable, declaring
multiple variables on one line, and initializing a variable.
The syntax for declaring a variable in VB 6.0 and VB .NET uses the Dim and
As keywords. C# uses a shorter notation by only stating the data type followed
by the variable name. Thus, VB and C# are in the reverse order. This can make
it a little confusing if you are VB programmer who is starting to learn C# pro-
gramming. But you will probably adjust to it very quickly.
Declaring multiple variables on a single line in VB .NET has changed a lot
from VB 6.0. Previously, every variable had to have its own data type assigned
to it. Otherwise, it would be treated as a variant. In VB .NET this has been
changed so that all variables listed between the Dim and the As keywords are of
the same type. In C#, all variables listed between the data type and the semi-
colon are of the same type.
VB 6.0 does not allow you to initialize variables on the declaration line—
this has to be done on another line of code. VB .NET now makes this possible
by placing the equal sign (=) and a value after the data type. The C# syntax
places the equal sign between the variable name and the semicolon.
9
488c02.fm Page 10 Wednesday, October 31, 2001 7:15 PM
Chapter 2
VB 6.0
Declare a variable by listing the data type after the As keyword.
VB .NET
Declare a variable by listing the data type after the As keyword. You can initialize
the variable by assigning a value to it after the data type name. This won’t work
when you initialize more than one variable of the same type on one line.
C#
Declare a variable by putting the data type before the variable name. You can
initialize the variable by assigning a value to it after the variable name. This will
work when you initialize more than one variable of the same type on one line.
10
488c02.fm Page 11 Wednesday, October 31, 2001 7:15 PM
Program Fundamentals
• The Private modifier only allows a variable to be accessed from within its
declaration context.
• The Friend (VB .NET) and internal (C#) modifiers allow a variable to be
used from anywhere within its assembly. Programs using the assembly
can’t access it.
• The Dim (VB only) modifier has the same scope as Private. It cannot be
used when declaring a procedure, class, or module.
• The Static modifier (VB only) exists for the entire life of the program. It
has the same scope as Private.
VB 6.0
List the access modifier before the variable name.
11
488c02.fm Page 12 Wednesday, October 31, 2001 7:15 PM
Chapter 2
VB .NET
List the access modifier before the variable name.
C#
List the access modifier before the data type.
Declaring Procedures
Declaring procedures is how you separate the different functionality within
your code. The syntax for returning a value and passing parameters is different
for each language.
In VB, procedures that don’t return a value are subroutines. Procedures
that do return a value are functions. C# doesn’t differentiate between the two
because every procedure must be declared as returning some data type. If you
don’t want the procedure to return a value, use the void data type. The void
data type returns nothing.
The C# equivalent of a VB subroutine is a procedure with type void. The
C# equivalent of a VB function is a procedure with any other data type.
Functions in VB 6.0 require that you assign the function name to the value
that is to be returned. VB .NET still allows you to return values this way, and
both VB .NET and C# now use the Return keyword to return a value. Simply list
the value to return after the Return keyword and it will be passed back to the
calling procedure.
VB 6.0
Procedures that don’t return data are declared using the Sub keyword. Procedures
that do return data use the Function keyword and after the procedure parameters
you must list the data type that will be returned. Assign the return value to the
function name.
12
488c02.fm Page 13 Wednesday, October 31, 2001 7:15 PM
Program Fundamentals
VB .NET
Procedures that don’t return data are declared using the Sub keyword. Procedures
that do return data use the Function keyword and after the procedure parameters
you must list the data type that will be returned. Either assign the return value to
the function name or list the return value after the Return keyword.
C#
Every procedure has to have a return data type listed before the procedure name. If
you don’t want to return a value, use the void data type. List the return value
after the return keyword.
13
488c02.fm Page 14 Wednesday, October 31, 2001 7:15 PM
Chapter 2
VB 6.0/VB .NET
Use the ByVal keyword to pass parameters by value. Use the ByRef keyword to
pass parameters by reference. If not specified, the VB 6.0 default is ByRef. The VB
.NET default is ByVal.
' VB .NET: param1 and param3 are both passed by value; param2 is passed by reference
' VB 6.0: param1 and param2 are both passed by reference; param3 is by value
Sub procedure(param1 As type, ByRef param2 As type, ByVal param3 As type)
C#
Passing parameters by value requires no extra coding. Use the ref keyword to
pass parameters by reference. Use the out keyword to specify parameters whose
only purpose is to return a value. The ref and out keywords must also be specified
when calling the procedure.
14
488c02.fm Page 15 Wednesday, October 31, 2001 7:15 PM
Program Fundamentals
Optional Parameters
Optional parameters give you flexibility with how much data is required to be
passed to a procedure. VB 6.0 and VB .NET use the Optional keyword to specify
optional parameters. C# doesn’t have optional parameters. As a substitute, you
can use the parameter array that is described in the next section. You can also
override methods as is discussed in Chapter 6.
Because a parameter is optional, you need a way to find out if the calling
procedure passed an argument or not. In VB 6.0 this is done using the
IsMissing() function. If IsMissing() returns True, no argument was passed and
you can assign a default value to the parameter. The IsMissing() function isn’t
in VB .NET because it requires optional parameters to be given a default value in
the procedure declaration.
A procedure can have as many optional parameters as needed, but they must
be the last parameters listed. No standard parameters can be listed after them.
VB 6.0
Optional parameters use the Optional keyword.
VB .NET
Optional parameters use the Optional keyword. It is required that each optional
parameter has a default value assigned to it.
End Sub
C#
There are no optional parameters in C#.
Parameter Arrays
Parameter arrays give you the benefit of being able to pass from zero to an
indefinite number of parameters to a procedure without having to declare each
one individually.
15
488c02.fm Page 16 Wednesday, October 31, 2001 7:15 PM
Chapter 2
VB 6.0
Use the ParamArray keyword to declare a parameter array.
VB .NET
Use the ParamArray keyword to declare a parameter array.
C#
Use the params keyword to declare a parameter array.
• The Friend (VB .NET) and internal (C#) modifiers allow a procedure to be
accessed only from within the assembly.
3. There are other access modifiers available for classes. These are discussed in Chapter 6
and Chapter 8.
16
488c02.fm Page 17 Wednesday, October 31, 2001 7:15 PM
Program Fundamentals
VB 6.0
List the access modifier before the procedure name.
VB .NET
List the access modifier before the procedure name. A procedure without an
access modifier will be declared as Public.
C#
List the access modifier before the procedure data type. A procedure without an
access modifier will be declared as Private.
Calling Procedures
Once you have declared a procedure, you need to call it so that its functionality
can be used. Although this seems fairly simple, you need to be aware of the
proper use of parentheses as well as how to properly pass arguments.
Before examining how to pass arguments, let’s look at a change from VB 6.0
to VB .NET. It is now required in .NET to use parentheses when calling a pro-
cedure. This simplifies things because you will no longer get compiler errors
stating that you need to have parentheses or that you are using them where
17
488c02.fm Page 18 Wednesday, October 31, 2001 7:15 PM
Chapter 2
they aren’t required. Now parentheses are always required, so you don’t have
to think about it. C# also requires parentheses when calling a procedure.
When passing arguments to a procedure, VB has the advantage over C#.
Normally, when you call a procedure you pass the arguments in the order that
they are listed in the procedure declaration. This applies to both VB and C#.
The advantage that VB has over C# is that in VB you can pass arguments by
name. In other words, you do not have to pass arguments in the order that the
procedure declaration says you should. This is very helpful when working
with optional parameters because you can state which parameters you want
to use and leave the others alone. You don’t have to use an empty comma list
and worry about inserting the correct number of commas. Simply list which
parameters get which arguments. This makes your code look much cleaner.
Because C# doesn’t have optional parameters, it doesn’t need the capability to
pass arguments by name.
It should be noted that regardless of the language, when calling proce-
dures that have a parameter array, you do not need to pass the procedure an
actual array. You only need to pass it arguments separated by commas. The
calling procedure will store these arguments in an array, but it is only expecting
standard single-value arguments.
VB 6.0
Call subroutines without using parentheses. Call functions using parentheses.
When passing arguments by name use := after the argument and give it a value.
VB .NET
Call subroutines and functions using parentheses. When passing arguments by
name use := after the argument and give it a value.
C#
Call all procedures using parentheses.
procedure(param1, param2);
var = procedure(param1, param2);
18
488c02.fm Page 19 Wednesday, October 31, 2001 7:15 PM
Program Fundamentals
VB 6.0
Sub Main()
ProcessScores 80, 90, 75
End Sub
19
488c02.fm Page 20 Wednesday, October 31, 2001 7:15 PM
Chapter 2
VB .NET
Module Module1
Sub Main()
ProcessScores(80, 90, 75)
Console.ReadLine()
End Sub
20
488c02.fm Page 21 Wednesday, October 31, 2001 7:15 PM
Program Fundamentals
21
488c02.fm Page 22 Wednesday, October 31, 2001 7:15 PM
Chapter 2
C#
using System;
namespace C_Fundamentals_TestScores
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
ProcessScores(80, 90, 75);
Console.ReadLine();
}
static public void ProcessScores(int Score1, int Score2, int Score3)
{
int highScore;
float avgScore;
//Write the scores we are working with
DisplayScores(Score1, Score2, Score3);
//Get the high score
CalcHighScore(Score1, Score2, Score3, out highScore);
Console.WriteLine("The high score is {0}", highScore);
//Get the average score
avgScore = CalcAvgScore(Score1, Score2, Score3);
Console.WriteLine("The average score is {0:N2}", avgScore);
}
22
488c02.fm Page 23 Wednesday, October 31, 2001 7:15 PM
Program Fundamentals
23
488c02.fm Page 24 Wednesday, October 31, 2001 7:15 PM
http://www.springer.com/978-1-893115-48-4