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 Learning: Creating a Library |
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 |
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> |
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 |
|
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):
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:
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:
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 |
|