Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

PreDefined VB - Net Functions

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

METHODS

A Method is a procedure built into the class. They are a series of statements that are executed when
called. Methods allow us to handle code in a simple and organized fashion. There are Broadly two types
of methods in VB .NET: those that return a value (Functions) and those that do not return a value (Sub
Procedures). Both of them are discussed below.
Sub Procedures
Sub procedures are methods which do not return a value. Each time when the Sub procedure is called
the statements within it are executed until the matching End Sub is encountered.
Functions
Function is a method which returns a value. Functions are used to evaluate data, make calculations or to
transform data. Declaring a Function is similar to declaring a Sub procedure. Functions are declared with
the Function keyword.
Parameters
A parameter is an argument that is passed to the method by the method that calls it. Parameters are
enclosed in parentheses after the method name in the method declaration. You must specify types for
these parameters. The general form of a method with parameters looks like this:
Public Function Add(ByVal x1 as Integer, ByVal y1 as Integer)
-----------Implementation
-----------End Function

IN BUILT FUNCTIONS IN VB.NET


VB String-Handling Functions
VB has numerous built-in functions for processing strings. Most VB string-handling functions return a
string, although some return a number (such as the Len function, which returns the length of a string and
functions like Instr and InstrRev, which return a character position within the string). The functions that
return strings can be coded with or without the dollar sign ($) at the end, although it is more efficient to
use the version with the dollar sign.
Function:

Len

Description:

Returns a Long containing the length of the specified string

Syntax:

Len(string)
Where string is the string whose length (number of characters) is to be returned.

Example:

lngLen = Len("Visual Basic")

Function:

Mid$ (or Mid)

Description:

Returns a substring containing a specified number of characters from a string.

Syntax:

Mid$(string, start[, length])

' lngLen = 12

The Mid$ function syntax has these parts:


Part
string

Description
Required. String expression from which characters are returned.

start

Required; Long. Character position in string at which the part to be taken


begins. If start is greater than the number of characters in string, Mid returns a
zero-length string ("").

length Optional; Long. Number of characters to return. If omitted or if there are fewer
than length characters in the text (including the character at start), all
characters from the start position to the end of the string are returned.

Example:

strSubstr = Mid$("Visual Basic", 3, 4)


"sual"

' strSubstr =

Note: Mid$ can also be used on the left side of an assignment statement, where you
can replace a substring within a string.
Example:
strTest = "Visual Basic"
Mid$(strTest, 3, 4) = "xxxx"
'strTest now contains "Vixxxx Basic"
In VB6, the Replace$ function was introduced, which can also be used to

replace characters within a string.

Function:

Left$ (or Left)

Description:

Returns a substring containing a specified number of characters from the beginning


(left side) of a string.

Syntax:

Left$(string, length)
The Left$ function syntax has these parts:
Part
Description
string Required. String expression from which the leftmost characters are returned.
length Required; Long. Numeric expression indicating how many characters to
return. If 0, a zero-length string ("") is returned. If greater than or equal to the
number of characters in string, the entire string is returned.

Example:

strSubstr = Left$("Visual Basic", 3)

' strSubstr = "Vis"

' Note that the same thing could be accomplished with Mid$:
strSubstr = Mid$("Visual Basic", 1, 3)

Function:

Right$ (or Right)

Description:

Returns a substring containing a specified number of characters from the end (right
side) of a string.

Syntax:

Right$(string, length)
The Right$ function syntax has these parts:
Part
Description
string Required. String expression from which the rightmost characters are returned.
length Required; Long. Numeric expression indicating how many characters to
return. If 0, a zero-length string ("") is returned. If greater than or equal to the
number of characters in string, the entire string is returned.

Example:

strSubstr = Right$("Visual Basic", 3)

' strSubstr = "sic"

' Note that the same thing could be accomplished with Mid$:
strSubstr = Mid$("Visual Basic", 10, 3)

Function:

UCase$ (or UCase)

Description:

Converts all lowercase letters in a string to uppercase. Any existing uppercase letters
and non-alpha characters remain unchanged.

Syntax:

UCase$(string)

Example:

strNew = UCase$("Visual Basic")


BASIC"

' strNew = "VISUAL

Function:

LCase$ (or LCase)

Description:

Converts all uppercase letters in a string to lowercase. Any existing lowercase letters
and non-alpha characters remain unchanged.

Syntax:

LCase$(string)

Example:

strNew = LCase$("Visual Basic")


basic"

Function:

Instr

Description:

Returns a Long specifying the position of one string within another. The search starts
either at the first character position or at the position specified by the start argument,
and proceeds forward toward the end of the string (stopping when either string2 is
found or when the end of the string1 is reached).

Syntax:

InStr([start,] string1, string2 [, compare])

' strNew = "visual

The InStr function syntax has these parts:


Part

Examples:

Description

start

Optional. Numeric expression that sets the starting position for each
search. If omitted, search begins at the first character position. The start
argument is required if compare is specified.

string1

Required. String expression being searched.

string2

Required. String expression sought.

compare

Optional; numeric. A value of 0 (the default) specifies a binary (casesensitive) search. A value of 1 specifies a textual (case-insensitive)
search.

lngPos = Instr("Visual Basic", "a")


' lngPos = 5
lngPos = Instr(6, "Visual Basic", "a")
' lngPos = 9 (starting at position 6)
lngPos = Instr("Visual Basic", "A")
' lngPos = 0 (case-sensitive search)
lngPos = Instr(1, "Visual Basic", "A", 1)
' lngPos = 5 (case-insensitive search)

Function:

InstrRev

Description:

Returns a Long specifying the position of one string within another. The search starts
either at the last character position or at the position specified by the start argument,
and proceeds backward toward the beginning of the string (stopping when
either string2 is found or when the beginning of the string1 is reached).
Introduced in VB 6.

Syntax:

InStrRev(string1, string2[, start, [, compare]])


The InStr function syntax has these parts:
Part

Description

string1

Required. String expression being searched.

string2

Required. String expression sought.

start

Optional. Numeric expression that sets the starting position for each
search. If omitted, search begins at the last character position.

compare Optional; numeric. A value of 0 (the default) specifies a binary (casesensitive) search. A value of 1 specifies a textual (case-insensitive)
search.
Examples:

lngPos = InstrRev("Visual Basic", "a")


' lngPos = 9
lngPos = InstrRev("Visual Basic", "a", 6)
' lngPos = 5 (starting at position 6)
lngPos = InstrRev("Visual Basic", "A")
' lngPos = 0 (case-sensitive search)
lngPos = InstrRev("Visual Basic", "A", , 1)
' lngPos = 9 (case-insensitive search)
' Note that this last example leaves a placeholder for the
start argument

Notes on Instr and InstrRev:

Something to watch out for is that while Instr and InstrRev both accomplish the same thing (except
that InstrRev processes a string from last character to first, while Instr processes a string from first
character to last), the arguments to these functions are specified in a different order. The Instr
arguments are (start, string1, string2, compare) whereas the InstrRev arguments are (string1, string2,
start, compare).
The Instr function has been around since the earlier days of BASIC, whereas InstrRev was not
introduced until VB 6.
Built-in "vb" constants can be used for the compare argument:
vbBinaryCompare for 0 (case-sensitive search)
vbTextCompare for 1 (case-insensitive search)

Function:

String$ (or String)

Description:

Returns a string containing a repeating character string of the length specified.

Syntax:

String$(number, character)
The String$ function syntax has these parts:
Part

Description

number

Required; Long. Length of the returned string.

character Required; Variant. This argument can either be a number from 0 to 255
(representing the ASCII character code* of the character to be
repeated) or a string expression whose first character is used to build
the return string.
strTest = String$(5, "a")
' strTest = "aaaaa"

Examples:

strTest = String$(5, 97)


' strTest = "aaaaa" (97 is the ASCII code for "a")

* A list of the ASCII character codes is presented at the end of this topic.

Function:

Space$ (or Space)

Description:

Returns a string containing the specified number of blank spaces.

Syntax:

Space$(number)
Where number is the number of blank spaces desired.

Examples:

strTest = Space$(5)

' strTest = "

"

Function:

Replace$ (or Replace)

Description
:

Returns a string in which a specified substring has been replaced with another substring a
specified number of times.
Introduced in VB 6.

Syntax:

Replace$(expression, find, replacewith[, start[, count[, compare]]]


)
The Replace$ function syntax has these parts:
Part
expression
find

Description
Required. String expression containing substring to replace.
Required. Substring being searched for.

replacewith

Examples:

Required. Replacement substring.

start

Optional. Position within expression where substring search is to begin. If


omitted, 1 is assumed.

count

Optional. Number of substring substitutions to perform. If omitted, the


default value is 1, which means make all possible substitutions.

compare

Optional. Numeric value indicating the kind of comparison to use when


evaluating substrings. (0 = case sensitive, 1 = case-insensitive)
Built-in "vb" constants can be used for the compare argument:
vbBinaryCompare for 0 (case-sensitive search)
vbTextCompare for 1 (case-insensitive search)

strNewDate = Replace$("08/31/2001", "/", "-")


' strNewDate = "08-31-2001"

Function:

StrReverse$ (or StrReverse)

Description:

Returns a string in which the character order of a specified string is reversed.


Introduced in VB 6.

Syntax:

StrReverse$(string)

Examples:

strTest = StrReverse$("Visual Basic")


"cisaB lausiV"

Function:

LTrim$ (or LTrim)

Description:

Removes leading blank spaces from a string.

Syntax:

LTrim$(string)

Examples:

strTest = LTrim$(" Visual Basic


' strTest = "Visual Basic "

Function:

RTrim$ (or RTrim)

Description:

Removes trailing blank spaces from a string.

Syntax:

RTrim$(string)

Examples:

strTest = RTrim$("
Basic"

Function:

Trim$ (or Trim)

Visual Basic

' strTest =

")

")

' strTest = "

Visual

Description:

Removes both leading and trailing blank spaces from a string.

Syntax:

Trim$(string)

Examples:

strTest = Trim$("
Basic"

Visual Basic

")

' strTest = "Visual

' Note: Trim$(x) accomplishes the same thing as


LTrim$(RTrim$(x))

Function:

Asc

Description:

Returns an Integer representing the ASCII character code corresponding to the first
letter in a string.

Syntax:

Asc(string)

Examples:

intCode = Asc("*")
intCode = Asc("ABC")

Function:

Chr$ (or Chr)

Description:

Returns a string containing the character associated with the specified character code.

Syntax:

Chr$(charcode)
Where charcode is a number from 0 to 255 that identifies the character.

Examples:

strChar = Chr$(65)

' intCode = 42
' intCode = 65

' strChar = "A"

DATE AND TIME FUNCTIONS IN VB


Visual Basic supplies a complete set of date and time functions to extract dates and times from the
system clock and to manipulate these values for various computational and display purposes.

Date and Time Properties


When working with dates and times it is often necessary to know the current date and/or time. These
values can be extracted from the server's system clock through the properties shown in the table below.
These properties are used in several of the date and time functions described below.

Property

DateString
Now
TimeOfDay
to 1/1/0001).
Timer
midnight.
TimeString
Today

Value

Returns a String value representing the current date.


DateString = 02-17-2009
Returns a Date value containing the current date and time.
Now = 2/17/2009 10:00:33 AM
Returns a Date value containing the current time of day (the date is set
TimeOfDay = 1/1/0001 10:00:33 AM
Returns a Double value representing the number of seconds elapsed since
Timer = 36033.771923
Returns a String value representing the current time of day.
TimeString = 10:00:33
Returns or sets a Date value containing the current date.
Today = 2/17/2009 12:00:00 AM

Date Functions
Date functions are summarized in the following table with functions described more fully below.
Function
Use
DateAdd() Returns a Date value containing a date and time value to which a specified time interval has
been added.
DateDiff() Returns a Long value specifying the number of time intervals between two Date values.
DatePart() Returns an Integer value containing the specified component of a given Date value.
DateSerial() Returns a Date value representing a specified year, month, and day, with the time
information set to midnight (00:00:00).
DateValue()
Returns a Date value containing the date information represented by a string, with the
time information set to midnight
(00:00:00).
Day()
Returns an Integer value from 1 through 31 representing the day of the month.
IsDate()
Returns a Boolean value indicating whether an expression can be converted to a date.
Month()
Returns an Integer value from 1 through 12 representing the month of the year.
MonthName() Returns a String value containing the name of the specified month.
WeekDay() Returns an Integer value containing a number representing the day of the week .

Time Functions
Time functions are summarized in the following table with functions described more fully below. Several of
the date functions can be applied to time measurements.

STRING FUNCTIONS :-

What You Need to


Do Method or Property to Use
Get the Char at a specific index in the String
Char (p)
Get the length of the String
Length (p)
Compare two Strings
Compare (s),
CompareOrdinal (s)
Compare a target String to a source String
CompareTo
Join source String to target String
Concat (s)
Copy source String object to target String
Copy (s)
Clone the source String
Clone
Copy characters into an array
CopyTo
Test the beginning and ends of Strings
EndsWith, StartsWith
Format numeric Strings
Format (s)
Get the index location of characters in a String
IndexOf, LastIndexOf, LastIndexOfAny
Insert subStrings into a String
Insert
Obtain a reference to a String
Intern (s), IsInterned (s)
Manipulate subStrings
Join, Split (s)
Pad Strings with additional characters
PadLeft, PadRight
Remove characters from a String
Remove
Replace characters in a String
Replace
Isolate a subString from a String
SubString
Trim characters from the beginning and ends of Strings
Trim, TrimEnd, TrimStart
Copy the characters in the String to a Unicode character array ToCharArray
Covert all characters in the String to lowercase
ToLower
Return the value of the String object
ToString
Convert all the characters in the String to uppercase
ToUpper

You might also like