back up codes

always have a backup of your code

exceptions

Exception represents errors that occur during application execution.

The following code example demonstrates a catch block that is defined to handle ArithmeticException errors. This catch block also catches DivideByZeroException errors because DivideByZeroException derives from ArithmeticException, and there is no catch block explicitly defined for DivideByZeroException errors.


Imports System
Class ExceptionTestClass
Public Shared Sub Main()
Dim x As Integer = 0
Try
Dim y As Integer = 100 / x
Catch e As ArithmeticException
Console.WriteLine("ArithmeticException Handler: {0}", e.ToString())
Catch e As Exception
Console.WriteLine("Generic Exception Handler: {0}", e.ToString())
End Try
End Sub
End Class

The common language runtime provides an exception handling model that is based on the representation of exceptions as objects, and the separation of program code and exception handling code into try blocks and catch blocks, respectively. There can be one or more catch blocks, each designed to handle a particular type of exception, or one block designed to catch a more specific exception than another block.

If an application handles exceptions that occur during the execution of a block of application code, the code must be placed within a try statement. Application code within a try statement is a try block. Application code that handles exceptions thrown by a try block is placed within a catch statement, and is called a catch block. Zero or more catch blocks are associated with a try block, and each catch block includes a type filter that determines the types of exceptions it handles.

When an exception occurs in a try block, the system searches the associated catch blocks in the order they appear in application code, until it locates a catch block that handles the exception. A catch block handles an exception of type T if the type filter of the catch block specifies T or any type that T derives from. The system stops searching after it finds the first catch block that handles the exception. For this reason, in application code, a catch block that handles a type must be specified before a catch block that handles its base types, as demonstrated in the example that follows this section. A catch block that handles System.Exception is specified last.

date time & numeric formats

Format Function returns a string formatted according to instructions contained in a format String expression.

This example shows various uses of the Format function to format values using both String formats and user-defined formats. For the date separator (/), time separator (:), and the AM/PM indicators (t and tt), the actual formatted output displayed by your system depends on the locale settings the code is using. When times and dates are displayed in the development environment, the short time format and short date format of the code locale are used.


Dim myStr As String


myStr = Format(Now(), "Long Time")

Returns current system time in the system-defined long time format.


myStr = Format(Now(), "Long Date")

Returns current system date in the system-defined long date format.


myStr = Format(Now(), "D")

Returns current system date in the system-defined long date format, using the single letter code for the format.


myStr = Format(Now(), "h:m:s")

Returns time in format as 5:6:20″.


myStr = Format(Now(), "hh:mm:ss tt")

Returns time in format as 10:12:22 AM.


myStr = Format(Now(), "dddd, MMM d yyyy")

Returns date format as “Saturday, Jan 23 2010″.


myStr = Format(Now(), "HH:mm:ss")

Returns time format as 17:04:23.


myStr = Format(23)

Returns 23.


myStr = Format(5459.4, "##,##0.00")

The sample uses user-defined numeric formats and would return 5,459.40.


myStr = Format(334.9, "###0.00")

returns 334.90.


myStrr = Format(5, "0.00%")

Returns 500.00%.

adding attributes

Attributes can be added to selected control events.

The following example is added on the page load event which displays a message box whenever the linkbutton is clicked.


mylnkbtn.Attributes.Add("onclick", "return confirm('Are you sure you want to logout?')")

format functions

FormatPercent Function
The FormatPercent function returns an expression formatted as a percentage (that is, multiplied by 100) with a trailing % character.
This example illustrates the use of the FormatPercent function.


Dim myNumber As Single = 0.76
Dim myString As String = FormatPercent(myNumber)

myString returns 76.00%.

FormatNumber Function
This example demonstrates the FormatNumber function.


Dim myNumber As Integer = 45600
Dim myString As String = FormatNumber(myNumber, 2, , , TriState.True)

myString returns 45,600.00.

FormatDateTime Function
FormatDateTime Function returns a string expression representing a date/time value.
This example demonstrates the use of the FormatDateTime function.


Dim myDate As DateTime = #3/12/1999#
Dim myString As String = FormatDateTime(myDate, DateFormat.LongDate)

myString returns Friday, March 12, 1999.

FormatCurrency Function
FormatCurrency Function returns an expression formatted as a currency value using the currency symbol defined in the system control panel.
The following example illustrates the use of the FormatCurrency function.


Dim myDebt As Double = -4456.43
Dim myString As String
myString = FormatCurrency(myDebt, , , TriState.True, TriState.True)

myString returns ($4,456.43).

event everytime form is shown

Codes can be executed everytime a form is loaded or shown.

The Load Event will be executed the first time the form is shown but the VisibleChanged Event will be executed everytime the form is shown.

The following are the different format between the two:

For the VisibleChanged Event:

Private Sub myForm_VisibleChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.VisibleChanged
End Sub

For the Load Event:

Private Sub myForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub

communication port in vb

In accessing the communication port of a computer, import first the following components:


Imports System.IO.Ports
Imports System.ComponentModel

Then insert the following code to an event:


Dim port As New IO.Ports.SerialPort = New SerialPort("COM3", 9600, Parity.None, 1, Stopbits.None)
port.Open()
Console.WriteLine(port.IsOpen)
port.Close()

left, mid & right functions

Left function returns a string containing a specified number of characters from the left side of a string.This function returns a substring of a given String. In a class that has a Left property, it may be necessary to fully qualify the Left function.


Dim myString As String = "Hello World!"
Dim mysubString As String = Microsoft.VisualBasic.Left(myString, 5)

This would return “Hello”.

The Mid function returns a string containing a specified number of characters from a string.


Dim myString As String = "Mid Function Demo"
Dim FirstWord As String = Mid(myString, 1, 3)
Dim LastWord As String = Mid(myString, 14, 4)
Dim MidWords As String = Mid(myString, 5)

FirstWord returns Mid.
LastWord returns Demo.
MidWords returns Function Demo.

The Right function returns a string containing a specified number of characters from the right side of a string. In a class that has a Right property, it may be necessary to fully qualify the Right function.


Dim myString As String = "Hello World!"
Dim mysubString As String = Microsoft.VisualBasic.Right(myString, 6)

This returns World!.

Asc & AscW functions

The Asc and AscW functions returns an Integer value representing the character code corresponding to a character.

Asc returns the code point, or character code, for the input character. This can be 0 through 255 for single-byte character set (SBCS) values and -32768 through 32767 for double-byte character set (DBCS) values. For charts of the single-byte ASCII characters, see ASCII Character Codes.

The returned value depends on the code page for the current thread, which is contained in the ANSICodePage property of the TextInfo class in the System.Globalization namespace. You can obtain ANSICodePage by specifying System.Globalization.CultureInfo.CurrentCulture.TextInfo.ANSICodePage.

AscW returns the Unicode code point for the input character. This can be 0 through 65535. The returned value is independent of the culture and code page settings for the current thread.


Dim myValue1, myValue2, myValue3 As Integer
myValue1 = Asc("A")
myValue2 = Asc("a")
myValue3 = Asc("Apple")

myValue1 returns 65.
myValue2 returns 97.
myValue3 returns 65.

int & fix functions

Int and Fix functions return the integer portion of a number.

In the case of a negative number argument, the Int function returns the first negative integer less than or equal to the number; the Fix function returns the first negative integer greater than or equal to the number.

The following example requires you to specify Option Strict Off because implicit conversions from type Double to type Integer are not allowed under Option Strict On:


Dim myNum1, myNum2, myNum3, myNum4, myNum5, myNum6 As Integer
myNum1 = Int(99.8)
myNum2 = Fix(99.8)
myNum3 = Int(-99.8)
myNum4 = Fix(-99.8)
myNum5 = Int(-99.2)
myNum6 = Fix(-99.2)

myNum1 returns 99.
myNum2 returns 99.
myNum3 returns -100.
myNum4 returns -99.
myNum5 returns -100.
myNum6 returns -99.

The CInt function is used to explicitly convert other data types to type Integer with Option Strict Off. However, CInt rounds to the nearest integer instead of truncating the fractional part of numbers.


Dim myNum1, myNum2, myNum3 As Integer
myNum1 = CInt(99.8)
myNum2 = CInt(-99.8)
myNum3 = CInt(-99.2)

myNum1 returns 100.
myNum2 returns -100.
myNum3 returns -99.

The CInt function can be used on the result of a call to Fix or Int to perform explicit conversion to integer without rounding.


Dim myNum1, myNum2 As Integer
myNum1 = CInt(Fix(99.8))
myNum2 = CInt(Int(99.8))

myNum1 returns 99.
myNum2 returns 99.

val function

The Val function returns the numbers contained in a string as a numeric value of appropriate type. It stops reading the string at the first character it cannot recognize as part of a number. Symbols and characters that are often considered parts of numeric values, such as dollar signs and commas, are not recognized. However, the function recognizes the radix prefixes &O (for octal) and &H (for hexadecimal). Blanks, tabs, and linefeed characters are stripped from the argument.


Dim myValue1, myValue2, myValue3 As Double
myValue1 = Val("2457")
myValue2 = Val(" 2 45 7")
myValue3 = Val("24 and 57")

myValue1 will give result 2457.
myValue2 will give result 2457.
myValue3 will give result 24.

Post Navigation

Follow

Get every new post delivered to your Inbox.