Download presentation
Presentation is loading. Please wait.
Published byDella Blake Modified over 8 years ago
1
COM148X1 Interactive Programming Lecture 4
2
Topics Today DateTime Functions Using Module Using Procedure Using Function Pass by Value and Pass by Reference
3
DateTime Functions
4
DateTime Class Visual Basic provides many useful date and time functions and they can be used through all DateTime object All DateTime functions will not alter the original DateTime, another DateTime variable is required to hold the result
5
Commonly Use DateTime Functions FunctionDescriptionExample Year() get the year (integer) of a date x = d.Year() Month() get the month (integer of a date x = d.Month() Day() get the day (integer) of a date x = d.Day() Hour() get the hour (integer) of a date x = d.Hour() Second() get the second (integer) of a date x = d.Second() DayOfWeek() get the day of week (integer, 0 – Sunday, 6 – Saturday) x = d.DayOfWeek() DayOfYear() get the day of year (1 – 366) x = d.DayOfYear()
6
Commonly Use DateTime Functions (cont’) FunctionDescriptionExample Now() get the current date and time d2 = d.now() AddYear(y) add y years to date d2 = d.AddYear(3) AddMonth(m) add m months to date d2 = d.AddMonth(2) AddDay(d) add d days to date d2 = d.AddDay(10) AddHour(h) add h hours to date d2 = d.AddHour(12) AddSecond(s) add s seconds to date d2 = d.AddSecond(50) IsLeapYear() Check if the year of date is leap year if d.IsLeapYear() then …
7
DateTime Format DateTime can be converted from/ to String in the following format d08/17/2000 DThursday, August 17, 2000 fThursday, August 17, 2000 4:32 PM FThursday, August 17, 2000 4:32:00 PM g08/17/2000 4:32 PM G08/17/2000 4:32:00 PM mAugust 17 rThu, 17 Aug 2000 16:32:00 GMT s2000-08-17T16:32:00 t4:32 PM T4:32:00 PM u2000-08-17 16:32:00Z yAugust, 2000
8
DateTime Example Dim d as DateTime ‘ convert String to DateTime d = “Thursday, August 17, 2000 4:32 PM” Dim s as String ‘ convert DateTime to String s = d.ToString(“g”) ‘ s = “08/17/2000 4:32 PM ”
9
Using Module
10
What is Module Module is group of functions, procedures and variables using throughout all the form Module can be add to other project and the functions, procedures and variables can be reused
11
Create Module Step 1
12
Create Module Step 2
13
Create Module Step 3
14
Define Module Module module_name End Module The code will be generated by Visual Basic if previous steps are followed
15
Declare Variable in Module Public variable_name As data_type Example Public MaxStudent as Integer = 20 Variables declared in module can be accessed by other modules/ forms in the same project
16
Using Procedure
17
What is Procedure Procedure is a segment of code which responsible for a sub-task of the main task Advantages of using procedure Make the program easy to design and implement Make the code easy to reuse Make the code easy to read Make the code easy to modify
18
One Bulky Procedure Enter list of valid item code, calculate the total cost of stock and format the result into a report
19
Break Down of Procedure Enter item code Check for valid code Add cost of stock More item? y Format and print report n Valid? n y
20
Define Procedure Sub procedure_name( ByVal pname1 As ptype1, … ) procedure End Sub Example Sub AddStock(ByVal stock_count As Integer) total_cost = total_cost + stock_count * 5 End Sub
21
Using Function
22
What is Function Function is similar to procedure but it will return a value back to the caller
23
Function A1 A2 A3 F v1 v2 v3
24
Define Function Function function_name( ByVal pname As ptype, … ) As output_type function calculation function_name = value End Function Example Function CheckCode(ByVal code As Integer) As Boolean If code 100 Then CheckCode = False Else CheckCode = True End If End Function
25
Pass by Value and Pass by Reference
26
Pass by Value If parameters set as ByVal, then it is “Pass by Value”, all modifications to this parameter during the execution of function/ procedure will not retain (i.e. the value of the parameter will be restored back to the value before the function/ procedure execute)
27
Pass by Value Example Function CheckCode(ByVal code As Integer) as Boolean code = code mod 100 ‘get leftmost 2 digits if (code mod 7 = 0) then CheckCode = True Else CheckCode = False End If End Sub Sub Proc() Dim c As Integer Do c = InputBox(“Input Code”) If ( Not CheckCode(c) )Then MsgBox(“Invalid Code ” & c) ‘no change in value of c End if Loop While ( Not CheckCode(c) ) End Sub
28
Pass by Reference If parameters set as ByRef, then it is “Pass by Reference”, all modifications to this parameter during the execution of function/ procedure will retain
29
Pass by Reference Example Sub Swap(ByRef x As Integer, ByRef y As Integer) Dim temp as Integer temp = x x = y y = temp End Sub Sub Proc() Dim x As Integer = 1 Dim y As Integer = 2 Swap(x, y) ‘value of x and y will be swapped End Sub
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.