290 likes | 679 Views
Built-In Functions. NUMERIC FUNCTIONS: SQR, INT, ROUND, VAL, CINT. Function Sqr(Number As Double) As Double Sqr(9) is 3. Sqr(0) is 0. Sqr(2) is 1.414214. Function Int(Number) Int(2.7) is 2. Int(3) is 3. Int(-2.7) is -3. Function Round(Number, [NumDigitsAfterDecimal As Long])
E N D
NUMERIC FUNCTIONS: SQR, INT, ROUND, VAL, CINT. • Function Sqr(Number As Double) As Double Sqr(9) is 3. Sqr(0) is 0. Sqr(2) is 1.414214. • Function Int(Number) Int(2.7) is 2. Int(3) is 3. Int(-2.7) is -3. • Function Round(Number, [NumDigitsAfterDecimal As Long]) Round(2.7) is 3. Round(2.317, 2) is 2.32 Round(2.317, 1) is 2.3
NUMERIC FUNCTIONS: SQR, INT, ROUND, VAL, CINT. • Function CInt(Expression) As Integer Dim x As String Dim y As Integer x = "2.56" y = CInt(x) ‘ y = 3 • Function Val(String As String) As Double dim z as double z = Val(x) ‘ z = 2.56
STRING FUNCTIONS: LEFT, MID, RIGHT, UCASE, TRIM • Function Left(String, Length As Long) Left(“fanatic”, 3) is “fan”. Left(“12/15/99”, 2) is ”12”. • Function Right(String, Length As Long) Right(“fanatic”, 3) is “tic”. Right(“12/15/99”, 2) is ”99”.
STRING FUNCTIONS: LEFT, MID, RIGHT, UCASE, TRIM • Function Mid(String, Start As Long, [Length]) Mid(“fanatic”, 5, 1) is “t”. Mid(“12/15/99”, 4, 2) is ”15”. • Function UCase(String) UCase(“12three”) is “12THREE”. • Function LCase(String) LCase(“GREEN”) is “green”.
STRING FUNCTIONS: LEFT, MID, RIGHT, UCASE, TRIM • Function Trim(String) Trim(“ 1 2 ”) is “1 2”. Trim(” -12 “) is ”-12”. • Function LTrim(String) LTrim(“ 1 2 ”) is “1 2 ”. LTrim(” -12 “) is ”-12 ”. • Function RTrim(String) RTrim(“ 1 2 ”) is “ 1 2”. RTrim(” -12 “) is ” -12”.
STRING-RELATED NUMERIC FUNCTIONS: LEN, INSTR • Function Len(Expression) Len(“Shenandoah”) is 10. Len(“Just a moment”) is 13. Len(“ ”) is 1. • Function InStr( String1, String2) InStr(“Shenandoah”, “nand”) is 4. InStr(“Just a moment”, “ ”) is 5. InStr(“Croissant”, “ist”) is 0.
FORMAT FUNCTIONS • Function FormatNumber(Expression, [NumDigitsAfterDecimal As Long = -1]) FormatNumber(12345.628, 1) is 12,345.6 • Function FormatCurrency(Expression, [NumDigitsAfterDecimal As Long = -1]) FormatCurrency(12345.628, 2) is $12,345.63 • Function FormatPercent(Expression, [NumDigitsAfterDecimal As Long = -1]) FormatPercent(.185, 2) 18.50%
FORMAT FUNCTIONS • Function FormatDateTime(Expression, [NamedFormat As VbDateTimeFormat = vbGeneralDate]) As String FormatDateTime(“9-15-99”, vbLongDate) is Wednesday, September 15, 1999 FormatDateTime(“10-23-00”, vbLongDate) is Monday, October 23, 2000
Random Function • Function Rnd([Number]) As Single randomly displays a number from 0 up to (but not including) 1. picBox.Print Int(6 * Rnd) + 1; will display a number between 1 and 6
For Each …….Next • Similar to For…..Next Loop • Used with collection of objects or with arrays • Syntax: For Each element In group statements Next element
Exiting a loop • Exit For For i=0 to 10 Statements Exit For statements Next • Exit Do Do while condition Statements Exit Do Statements Loop
Exiting a Sub or Function Private Sub test() statements Exit Sub statements End Sub
Control Arrays • Group of Controls that share the same name and type • They also share the same event procedure. • Control arrays are useful if you want several controls to share the same code. • You can create Control Arrays either in design time or at run time.
Control Arrays • Creating Controls Arrays at Design-Time: three ways • Assign the same name to more than one control. • Copy an existing control and paste it in the form. • Set the controls Index property to a value that is not null.
Controls Arrays Creating Controls Arrays at Run-Time: • You must first create the array at design time • Then at run-time you can use the following code. • load controlName(index)
Control Arrays • Control Arrays share the Same Event procedure . Ex: Private Sub Command1_Click(index as Integer) select case index case 0: statements case 1: statements End Select End Sub
Collections • A collection is a group of related Objects. • Objects in a collection are referred to as members of the collection. • Each member of the collection is numbered sequentially beginning at 0. • Common collections in visual basic.
Collections Common collections in visual basic. • Forms: Contains loaded forms. • Controls: Contains Controls on a form. • Printers: Contains available Printer objects. Example: Dim MyControl as Control For Each MyControl in form1.controls list1.additem MyControl.name Next MyControl