Home

Built-In Procedures

 

Custom Libraries

 

Introduction

If the .NET Framework doesn't have a function (or a class) you are looking for, you can create one and be able to use it over and over again in different programs. You can even create one or a series of commercial functions (or classes) and be able to distribute or sell it. To make this happen, you can "package" one or more procedures (or classes) in a library.

A library is a program that contains procedures (and/or classes) and/or other resources that other programs can use. Such a program is created with the same approach as the programs we have created so far. Because a library is not an executable, it doesn't need the Main() procedure. A library usually has the extension .dll.

 

Creating a Library

A library can be made of a single file or as many files as necessary. A file that is part of a library can contain one or more procedures (or classes). Each procedure (or class) should implement a behavior that can eventually be useful and accessible to other procedures (or classes). The contents of a library are created exactly like those we have used so far. Everything depends on how you compile it.

To create a library, start by typing its code in a text file. Once the library is ready, to compile it, at the Command Prompt, you would execute the following command:

vbc /target:library NameOfFile.vb

After doing this, a library with the name of the file and the extension .dll would be created. If you use the above technique, the new library would be created using the name of the file. Otherwise, if you want a custom name, use the following syntax:

vbc /target:library /out:DesiredNameOfLibrary.dll NameOfFile.vb

Practical LearningPractical Learning: Creating a Library

  1. Start a new file in Notepad and type the following in it:
     
    Module Operations
    
        Function Addition(ByVal x As Double, ByVal y As Double) As Double
    	Return x + y
        End Function
    
        Function Subtraction(ByVal x As Double, ByVal y As Double) As Double
    	return x - y
        End Function
    
        Function Multiplication(ByVal x As Double, ByVal y As Double) As Double
    	return x * y
        End Function
    
        Function Division(ByVal x As Double, ByVal y As Double) As Double
    	if y = 0 Then return 0
    	return x / y
        End Function
    
    End Module
  2. Save the file in a new folder named Operations1
  3. Save the file itself as exo.vb
  4. Switch to the Command Prompt and change to the Operations1 folder
  5. To create the library, type vbc /target:library /out:Arithmetic.dll exo.vb and press Enter
     
    C:\VBasic\Operations1>vbc /target:library /out:Arithmetic.dll exo.vb
    Microsoft (R) Visual Basic .NET Compiler version 7.10.3052.4
    for Microsoft (R) .NET Framework version 1.1.4322.573
    Copyright (C) Microsoft Corporation 1987-2002. All rights reserved.
    
    
    C:\VBasic\Operations1>
  6. Start another file in Notepad and type the following:
     
    Imports System
    Imports Arithmetic
    
    Module Exercise
    
        Sub Main()
    	Dim Number1 As Double, Number2 As Double
    	Dim Result As Double
    
    	Number1 = 244.58
    	Number2 = 5082.88
    	Result  = Operations.Addition(Number1, Number2)
    
    	Console.WriteLine("{0} + {1} = {2}" & vbCrLf, Number1, Number2, Result)
        End Sub
    End Module
  7. Save the file in a new folder named Algebra1
  8. Save the file itself as Exercise.vb
  9. Using Windows Explorer or My Computer, copy the Arithmetic.dll file from the Operations1 folder to the Algebra1 folder 
  10. Switch to the Command Prompt and change to the Algebra1 folder
  11. To compile the program, type vbc /reference:Arithmetic.dll Exercise.vb and press Enter
  12. To execute the application, type Exercise and press Enter
 

Overview of Built-In Procedure

 

Introduction 

A procedure is referred to as "built-in" if it shipped with its programming language. To make your job a little easier, VBasic comes equipped with many procedures that you can use right away in your program. Based on this, before creating your own procedure, first check whether the functionality you are looking is already implementing in one of the available procedures because those that ship with VBasic are highly reliable and should be preferred.

Before using a built-in procedure, you must of course be familiar with it. This comes either by consulting the documentation or by experience. This means that you must know its name, its argument(s), its return value, and its role. The Microsoft Visual Basic (.NET) programming language provides one of the richest set of functions of a library. In fact, it is the richest of the .NET-based languages, giving you access to functions that are not directly available to other languages such as C# or C++/CLI. Because there so many of those functions, we will review only the most usually used. Eventually, when necessary, in other lessons, we may introduce new ones.

 

Conversion Functions

You may recall that when studying data types, we saw that each had a corresponding function used to convert a string value or an expression to that type. As a reminder, the general syntax of the conversion functions is:

ReturnType = FunctionName(Expression)

The Expression could be of any kind. For example, it could be a string or expression that would produce a value such as the result of a calculation. The conversion function would take such a value, string, or expression and attempt to convert it. If the conversion is successful, the function would return a new value that is of the type specified by the ReturnType in our syntax.

The conversion functions are as follows:

 
Function  
Name Return Type Description
CBool Boolean Converts an expression into a Boolean value
CByte Byte Converts an expression into Byte number
CDate Date Converts and expression into a date or time value
CDbl Double Converts an expression into a flowing-point (decimal) number
CInt Integer Converts an expression into an integer (natural) number
CCur Currency Converts an expression into a currency (monetary) value
CLng Long Converts an expression into a long integer (a large natural) number
CSng Single Converts an expression into a flowing-point (decimal) number
CStr String Converts an expression into a string
 

Value Formatting

So far, after performing a calculation, we were displaying the result "as is". To appropriately display a value, Microsoft Visual Basic provides the Format() function. Although it can be used for different types of values, the most basic technique consists of passing it an expression that holds the value to display. In this case the syntax to use would be:

Format(Expression)

 

 

The Character To ASCII Conversion

The Chr function is used to associate an entered character with its ASCII character equivalent. It could be used to convert a number to a character. It could also be used to break a line in a long expression. The syntax of the Chr function is:

Chr(Number)

A combination of Chr(13) and Chr(10) would break a line in an expression.

 

 

Case Conversion

If you are presented with a string or an expression whose cases must be the same, you can convert all of its characters in either uppercase or lowercase.

To convert a character, a string or an expression to uppercase, you can call the UCase or the UCase$ function. These functions take one argument as the string or expression to be considered. The syntaxes are:

Function UCase(Letter As Char) As Char
Function UCase(Expression As String) As String

The first version receives one character as argument. If the character is already in uppercase, it would be return the same. If the character is not a readable character, no conversion would happen and the function would return it. If the character is in lowercase, it would be converted to uppercase and the function would then return the uppercase equivalent.

The second version considers the argument supplied as a string. Any letter that is in lowercase in the string would be converted to uppercase. Any letter that is in uppercase would be preserved and would not be changed. Any non-alphabetic character in the string would be kept "as is".

 

Logical Functions

 

Is it Empty?

A logical function is one that checks whether an expression is true or false and then return a Boolean value.

The IsEmpty function check whether a field is empty. Its syntax is:

IsEmpty(Expression)

In this case, the Expression argument will be checked. If it is empty, the IsEmpty function returns True. If the expression or field is not empty, that is, if it contains something, the function returns False.

 

Is it Null?

Another problem you may encounter when involving an operation or the contents of a control is whether it has never contained a value. This operation is sometimes confused with that of checking whether a field is empty. Here is the difference (it is important to understand this because it is used in many other environments):

  • Imagine a text box control is used for first name and the field displays Paul. If the user comes to that record, the field is not empty, it already contains a name, which is this case is Paul. If the user clicks in the field and deletes Paul, the field becomes empty. It is not null
  • Imagine a field is used for first name. If the user comes to a new record, the field for the first name may be empty (if you did not give it a default value). In this case, the field is Null: it is not empty because it has never contained anything. If the user types a name, and then deletes it, the field is not considered Null anymore: it has become empty

To check whether an expression or the value of a control is null, you can call the IsNull() function. Its syntax is:

IsNull(Expression)

Also used on fields, the IsNull() function checks the state of a field (remember, this functions does not check whether a field is empty or not; it checks if the field has ever contained a value). If the field it null, this function returns True. If the field is not null, this function returns False.

 

Date and Time Functions

 

Current Data and Time

Microsoft Visual Basic provides various functions to perform date and time related operations. These functions allow you to add dates or times, find the difference between dates or times, or add constant values to dates or times.

The current date is represented by a function called Date. The Date() function is used to get the system date of the computer. You can use it to display today's date, provided your computer has the correct date.

The current time of the computer is represented by a function called Time. The Time() function is used to get the system time of the computer.

The Date() and Time() functions can be combined and are represented by a function called Now.

 

Day - Month - Year

The Day function is used to get the numeric value that represents a day in the month. It ranges from 1 to 31 included.

The formula of the Day function is Day(DateValue)

The Month function displays the numeric month of a date. It ranges from 1 to 12 included.

The formula of the Month function is Month(DateValue)

The Year function returns the numerical year of a date.

The formula is the Year function is Year(DateValue)

 

Adding a Date

The DateAdd function is used to add a date value to another date. It can be used to add a number of days, weeks, months, or years to another date. The formula for the DateAdd function is

DateAdd(Interval, Number, date)

Required, the Interval argument specifies the kind of value you want as a result. This argument will be enclosed between double quotes and can have one of the following values:

Interval Used To Get
s Second
n Minute
h Hour
w Numeric Weekday
ww Week of the Year
d Day
y Numeric Day of the Year
m Month
q Quarter
yyyy Year

Required also, the Number argument specifies the number of units you want to add. If you set it as positive, its value will be added. On the other hand, if you want to subtract, make it negative.

The number represents the units of the Interval argument you want to add.

The date argument is the date to which you want to add the number.

 

Subtracting a Date

The DateDiff function is used to find the difference between two date or time values. It allows you to find the number of seconds, minutes, hours, days, weeks, months, or years from two valid date or time values. The DateDiff function takes 5 arguments, 3 are required and 2 are optional.

The formula of the function is

DateDiff(Interval, Date1, Date2, Option1, Option2)

Required, the Interval argument specifies what kind of value you want as a result. This argument will be enclosed between double quotes and can have one of the following values:

Interval Used To Get
s Second
n Minute
h Hour
w Numeric Weekday
ww Week of the Year
d Day
y Numeric Day of the Year
m Month
q Quarter
yyyy Year

Required also, the Date1 and Date2 argument specify the date or time values that will be used when performing the operation.

By default, the days of a week are counted starting on Sunday. If you want to start counting those days on another day, supply the Option1 argument using one of the following values: vbSunday, vbMonday, vbTuesday, vbWednesday, vbThursday, vbFriday, vbSaturday. There are other variances to that argument.

If your calculation involves weeks or finding the number of weeks, by default, the weeks are counted starting January 1st. If you want to count your weeks starting at a different date, use the Option2 argument to specify where the program should start.

 
 

Previous Copyright © 2004-2014 FunctionX, Inc. Next