Chapter 2: Learning Just Enough C# or VB.NET: Basic Syntax

41

that belong to each instance. If an object such as Customer has methods that belong to each instance, those methods are not static. However, if the Customer object type has a method that is static, then there would only be a single copy of that method that is shared among all Customer objects. For example, what if you wanted to get a discount price for all customers, regardless of who the customer is; you would declare a static method named GetCustomerDiscount. However, if you wanted information that belonged to

a specific customer, such as an address, you would create an instance method named GetAddress that would not be modified as static.

VB uses the term shared, which has the same meaning as static. Modules are inherently shared, and all module methods must be shared. Therefore, the VB Main method is shared.

In C#, the curly braces define the begin and end of the Main method. In VB, Main begins with Sub and is scoped to End Sub. Next, notice that the C# Main method is enclosed inside of a set of braces that belong to something called a class that has been given the name Program. The VB Main method is enclosed in something called a module. You’ll learn about the enclosing class and module next.

The Program Class

Methods always reside inside of a type declaration. A type could be a class or struct for C# or a class, module, or struct in VB. The term type might be a little foreign to you, but it might be easier if you thought of it as something that contains things. Methods are one of the things that types contain. The following snippet, from Listing 2-1, shows the type that contains the Main method, which is a class in C# and a module (in this example) in VB:

class Program

{

// Main Method omitted for brevity

}

VB:

Module Module1

'Main omitted for brevity End Module

Most object types you create will be a class, as shown in the previous C# example. In VB, you would replace Module with Class. Although VS uses Module as the default object type for a new project, it’s a holdover from earlier versions of VB. In practice, you shouldn’t use the VB Module but should prefer Class. The Program class contains the Main method. You could add other methods to the Program class or Module1 module,

Page 64
Image 64
Microsoft 9GD00001 manual Program Class