
C# ПІДРУЧНИКИ / c# / Premier Press - C# Professional Projects
.pdf

INTRODUCTION TO VISUAL BASIC .NET Appendix B 859
declared. You can do this by using the Dim statement, the Public statement, or the Private statement. The syntax that is used to declare an array is:
Dim ArrayName (NumElements) As DataType
In the syntax mentioned, the following list contains specifications:
ArrayName. Specifies the name of the array.
NumElements. Specifies the number of elements that the array can
contain.
DataType. Specifies the data t ype of the elements. This is optional.
While declaring arrays, parentheses need to be included after the array name to differentiate an array from a variable. Consider the following code statement:
Dim intArray1(10) As Integer
An Integer array by the name intArray1, which can contain 11 elements, is declared in the code mentioned above. Why are there 11 elements and not 10 as mentioned in the code? It is because arrays are zero-based. The index number, which is between 0 and 10, adds up to 11. The code mentioned previously is part of the statement given here:
Dim IntArray () As Integer = New Integer(10) {}
Differences between Visual Basic .NET and Visual Basic 6.0 in Terms of Arrays
I will now discuss some of the basic differences between Visual Basic .NET and the earlier versions of Visual Basic in terms of arrays.By default, the starting index of an array is 0 in Visual Basic 6.0, and you can change the starting index to 1 by using the Option Base statement. In addition, the starting index for individual array declarations can be changed. The number of elements in the array is equal to the number specified during an array declaration statement plus one, if the default-starting index is set to 0. However, the starting index for every array is 0 and cannot be changed in Visual Basic .NET. The Option Base statement is not supported by Visual Basic .NET. Interoperability with arrays of other programming languages is permitted because most programming languages support zerobased arrays.

860 |
Part X |
APPENDIXES |
|
|
|
Initializing Arrays
Each element of an array is initialized as if it were a separate variable. However, if an array is not initialized, then Visual Basic .NET initializes each array element to the default value of the data type of the array.
Consider the code given here. It explains how to declare and initialize an array.
Dim booksArray1(4) As String booksArray1(0) = “Introducing VB.NET” booksArray1(1) = “Introducing ADO.NET” booksArray1(2) = “Introducing VC++.NET” booksArray1(3) = “Introducing ASP.NET” booksArray1(4) = “Introducing C#”
In the previously mentioned code, an array, booksArray1, is declared that can contain five String type elements. This arr ay stores Introducing VB.NET at index 0,
Introducing ADO.NET at index 1, Introducing VC++.NET at index 2, Introducing
ASP.NET at index 3, and Introducing C# at index 4. It may be mentioned that 0 is the starting index or the lower bound that remains fixed for all the arrays. The upper bound or the end index is 4, and it can differ from one array to another.
An array can be declared or initialized in a single line by using the new keyword provided by Visual Basic .NET. This example shows how to declare an array by using a single line of code.
Dim booksArray1() As String = {“Introducing VB.NET”, “Introducing ADO.NET”,
“Introducing VC++.NET”, “Introducing ASP.NET”, “Introducing C#”}
To retrieve the values stored in a particular index position, the index number and the name of the array needs to be specified.The following statements illustrate the point:
Dim strVar As String
strVar = booksArray1(2)
After the execution of the previously mentioned statements, the value of the String type variable, strVar, which is stored in the index position 2 in booksArray1, is retrieved.


862 |
Part X |
APPENDIXES |
|
|
|
provides the Collection class. The syntax for creating a collection is discussed as follows:
Dim CollectionName As New Collection()
In the preceding syntax, the name of the collection that you want to create is specified by CollectionName. An instance of the Collection class is created, which is declared by the New keyword in the declaration statement
After the creation of the collection, you can manipulate the creation in the same way as you would manipulate the standard collections that are provided by Visual Basic .NET. However, there are some differences between the two. Consider the following example:
Dim collection1 as New Collection()
collection1 = Controls
The preceding code creates and initializes a Collection object, collection1, with the Controls collection. However, this statement displays an error message. Why is it so? The answer is that the Controls collection and the Collection class object are not interchangeable and are of different types with different usage. In addition, they do not have the same methods and also do not use the same kinds of index values.
Procedures
Consider a scenario where you need to perform a particular task repeatedly, for instance, calculating the average of marks obtained by students in a particular subject. In a situation such as this, you can group them in a procedure instead of writing the statements repeated ly. A set of statements grouped together to perform a specific task is called procedure. You can organize your applications by using procedures that allow you to chunk and group the program code logically.
After grouping the statements in a procedure, you can call the procedure from anywhere in the application. To call a procedure means to execute a statement that further instructs the compiler to execute the procedure. After executing the code in the procedure, the statement following the statement that called the procedure is executed.The statement that is called by a procedure is called a calling statement, and it includes the name of the procedure.The calling statement also includes the

INTRODUCTION TO VISUAL BASIC .NET Appendix B 863
data values that are needed by the procedure for performing the tasks that are specified. The data values are also referred to as arguments or parameters.
Consider the example of calculating average mentioned previously. In this case, you can create a procedure that accepts the maximum and minimum marks obtained by students as data values and calculate the average. To call this procedure, the statement to be called must provide the minimum and maximum marks obtained by students as parameters.
Now consider some of the advantages that are offered by procedures. The first advantage is the reusability of code. In other words, a procedure can be created and used when it is required and if any statement has to be changed, you simply need to make the changes in a single location.This is useful mainly in the case of large and complex applications.The applications that use procedures are easier to debug. Additionally, you can easily trace the errors in a procedure without debugging the entire application code.
Now consider the scope or accessibility of procedures in an application. Similar to classes and variables, procedures have a scope. A procedure is generally declared in a class or a module. Therefore, you can call a procedure from the same class or module in which it is created. The scope of the procedure depends on the access modifiers that you use while the procedures are declared. The access modifiers supported by Visual Basic .NET are listed in Table B-3.
Table B-3 Access Modifiers for Procedures
Access Modifier |
Scope |
Public |
A procedure with a Public access modifier can be called from any class |
|
or module in the application. |
Private |
A procedure with a Private access modifier can be called from the |
|
same class or module in which it is declared. |
Protected |
A procedure with a Protected access modifier can be called from the |
|
same class or module in which it is declared. In addition, it can be |
|
called from the derived classes of the class in which it is declared. |
Friend |
A procedure with a Friend access modifier can be called from any class |
|
or module that contains its declaration. |
|
|

864 |
Part X |
APPENDIXES |
|
|
|
Based on the functionality of procedures, they can be classified as:
Sub procedures. A sub procedure is used to perform a specific task.
Function procedures. A function procedure is used to perform the specific tasks and returns a value to the calling statement.
Property procedures. A property procedure is used to assign or access a value from an object.
Event-handling procedures. An event-handling procedure is used to perform a specific task when a particular event occurs.
Arguments
As stated earlier, variables, constants, or expressions are accepted as arguments by procedures. As a result, each time a procedure that accepts arguments is called, arguments need to be passed to the procedure. Based on the data values that are passed as arguments, the result can differ for each call to a procedure. Arguments can be passed to procedures by either value or reference.
Functions
Visual Basic .NET provides various built-in functions that can be used in applications. Some of the built-in functions are MsgBox, InputBox, CStr, DateDiff, and StrComp. The Microsoft.VisualBasic namespace contains a declaration for these built-in functions. These functions can be classified based on the tasks performed by the various built-in functions. The functions can be classified as follows:
Functions to enhance your programs are performed by the Application enhancement functions. Examples of Application enhancement func-
tions are MsgBox and InputBox.
Functions to manipulate strings are performed by String functions. Examples of String functions are StrComp, Len, and Trim.
Functions to manipulate date and time values are performed by the Date function. Examples of Date functions are DateDiff, Now, and Month.
Functions to convert one data type to another are performed by the Conversion function. Examples of Conversion functions are CStr, CDate, and Val.



INTRODUCTION TO VISUAL BASIC .NET Appendix B 867
Now, to add the functionality to the application, you need to write code for the button controls. After adding code to the button controls, the code for the application is as shown:
using System.Drawing; using System.Collections;
using System.ComponentModel; using System.Windows.Forms; using System.Data;
namespace SampleWindowsApplication
{
public class frmAcceptUserInput : System.Windows.Forms.Form
{
private System.Windows.Forms.Label lblUserName; private System.Windows.Forms.Label lblPassword; private System.Windows.Forms.Button btnSubmit; private System.Windows.Forms.Button btnExit; private System.Windows.Forms.TextBox txtUserName; private System.Windows.Forms.TextBox txtPassword;
private System.ComponentModel.Container components = null;
public frmAcceptUserInput()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}