Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture Roger Sutton CO331 Visual Programming 17: String handling, Date and Time, Multiple forms 1.

Similar presentations


Presentation on theme: "Lecture Roger Sutton CO331 Visual Programming 17: String handling, Date and Time, Multiple forms 1."— Presentation transcript:

1 Lecture Roger Sutton c.r.sutton@kent.ac.uk CO331 Visual Programming 17: String handling, Date and Time, Multiple forms 1

2 CO331 Visual Programming Introduction Strings of characters are a very important form of data. Accordingly a number of methods are available to facilitate their processing. Perhaps one of the most common operations is data validation – ensuring that a data item is consistent with some data type declaration. E.g. if a data item is to be used in a calculation, it is prudent to ensure that it is numeric before attempting to convert it to the appropriate data type. Thus 2 If IsNumeric(txtDisplay.Text) Then n = CInt(txtDisplay.Text) Else MessageBox.Show(“Error in number”) End If

3 CO331 Visual Programming Type conversion Sometimes referred to as ‘casting’ Conversion methods Strings are often converted to numbers and vice-versa using  CInt – converts a numeric string to an Integer  CDbl – converts a numeric string to a Double, and  CStr – converts a number to a String Parse Method In VB.Net each data type is a class with several methods. Accordingly the Parse method can be used to convert a string to some numeric data type E.g. 3 Dim age As Integer Dim studentAge as String = “25” age = Integer.Parse(studentAge)

4 CO331 Visual Programming Type conversion – cont’d Convert Class This class contains methods to convert a numeric value to a specified data type, or some string value to a numeric data type: E.g. E.g. 4 MethodConvert to ToDoubleDouble ToInt32Integer ToSingleSingle ToStringString Dim sales As Integer = 1000 Dim bonus As Double bonus = Convert.ToDouble(sales) * 0.05

5 CO331 Visual Programming Type conversion – cont’d Literal type characters A literal constant is an item of data whose value does not change while the program is running. E.g. 500, “Mary” are literal constants. A literal type character forces a literal constant to assume a data type other that the one the form indicates. E.g. 5 Literal type characterData typeExample SShortage = 35S IIntegerhours = 40I LLongpopulation = 2500L FSinglerate = 0.03F RDoublesales = 2356R CCharinitial = “A”C

6 CO331 Visual Programming String comparison Strings can be considered to be greater than or less than other strings in the sense that they might be arranged in ascending alphabetical order so that one sequence of characters might precede another. Consequently strings might be compared using the relational operators (=, >, <=, etc.) where < is interpreted as ‘before’ ; > to represent ‘after ’, and = to mean ‘equal’ VB uses a lexicographical comparison, i.e. the integer Unicode value of each character is compared. Hence the comparison is case-sensitive, lowercase coming before uppercase. 6

7 CO331 Visual Programming String modification Trim - often when a user is required to enter text data, they introduce leading or trailing spaces. Invariably these are not significant and might well be removed. The method Trim removes spaces from both ends of a string. E.g.string1 = “ Centre “ resultString = string1.Trim() yields “Centre” in resultString. TrimStart and TrimEnd may be used to remove space characters only from the respective ends. Remove is used to remove a given number of characters from a given position. E.g.string1 = “ Deadline“ resultString = string1.Remove(1, 4) removes 4 characters from position 1 of string1 to produce “Dine”. 7

8 CO331 Visual Programming String modification – cont’d Insert is used to insert characters into a string at a specified location. E.g.string1 = “Good students!” resultString = string1.Insert(5, “morning, ”) produces “Good morning, students!” ToLower and ToUpper changes characters from uppercase to lower case, and from lower case to upper case respectively. E.g.string1 = “Good students!” resultString = string1.ToUpper( ) produces “GOOD STUDENTS!” and resultString = string1.ToLower( ) produces “good students!” 8

9 CO331 Visual Programming String examination Length – the length property provides the number of characters in a string. E.g. string1 = “VB Programming” n = string1.Length would result in n being set to 14. Substring copies a specified part of a string. Its arguments indicate the index starting position and number of characters to be copied respectively. E.g. string1 = “VB Programming” resultString = string1.Substring(8, 3) produces “ram” in resultString and string1 is unchanged. IndexOf determines whether a string is contained within a string. If represent it returns the start index of the substring otherwise –1. E.g. string1 = “Mississippi!” n = string1.IndexOf(“sip”) sets n to 6. 9

10 CO331 Visual Programming String examination –cont’d IsNumeric( String) This function returns True if is argument is numeric and False otherwise. Split( String, separator character) Often it is required to separate a string constant into segments. The function, Split, takes two arguments: the string and the character to indicate the division points and stores the resulting segments in a string array. E.g. 10 Function firstName(ByVal letters As String) As String Dim words(4) As String words = Split(letters, " ") Return words(0) End Function

11 CO331 Visual Programming Data type: Char The Char data type can store one character, compared to the string data type that may store between 0 and 2 billion characters. A string variable may be viewed as a Char array. E.g. Dim letter(10) As Char letter = txtEntry.Text lblLength.Text = "Length of entry is " & _ UBound(letter) + 1 & " characters" Using the string methods it is possible to access individual characters. This, however, is quite cumbersome in comparison to referencing array elements. 11 Run

12 CO331 Visual Programming Date and Time Processing VB provides several date and time related functions in addition to the methods of the Date class. The function TimeOfDay( ) provides the current system date and time Today( ) produces the current system date Day applied to a date returns the day of the month as an Integer in the range 1 to 31 WeekDay produces the number of the day of the week Month provides the number of the month in the year WeekdayName and MonthName identify the corresponding day and month respectively Year applied to a date yields the year as an Integer 12

13 CO331 Visual Programming Date/ Time Processing –cont’d DateDiff returns the length of the interval in the units indicated from the start date to the finish date. It takes three arguments – the first is a method of DateInterval that indicates the part of the date/time to consider:  Year indicates the year  Month – the month as a number from 1 to 12  DayOfYear – the day of the year, a number between 1 and 365(6)  Day – the number of the day of the month  Weekday – the number of the weekday  WeekOfYear – the number of the week of the year  Hour, Minute, Second – the hours, minute and second respectively of the time followed by a start date expression and a finish date expression. 13

14 CO331 Visual Programming Date and Time Formatting VB provides several predefined date and time formats using the Format function. Suppose Now yields 26/8/2004 5:14:12 PM then: Alternatively the methods ToString, ToLongDateString, ToShortDateString, ToLongTimeString, ToShortTimeString of Date may be used to the same effect. 14 Function commandOutput Format(Now, “General Date”)26/8/2004 5:14:12 PM Format(Now, “Long Date”)26 August 2004 Format(Now, “Short Date”)26/8/2004 Format(Now, “Long Time”)17:14:12 Format(Now, “Short Time”)17:14 Run

15 CO331 Visual Programming Multiple forms Additional windows are produced by adding forms. This can be done in two ways: With the second and subsequent forms opening inside the main form. These are then dependent on the main form. Consequently if the main form is closed, the application ends. With additional forms opening outside the main form, as document windows. These are MDI (Multiple Document Interface) forms. (Not to be covered here) To add a form: 1.From File menu, select Add New Item …., or From Project menu, select Add Windows Form…., or, etc. 2.In Add New Item, ensure Windows form is selected. 3.Provide an appropriate name. 4.Click open. 15

16 CO331 Visual Programming Multiple form code Secondary forms are brought into the main by declaring them as variables. Accordingly a new instance of the form is created. E.g. where Form2 is the form name given when added to the project. An instance of the form is created each time the routine is activated. Consequently,depending on the routine, several instances may inadvertently be created. To restrict the number of instances to one, use Static in the declaration. E.g. The variable name can be then be used to access the form’s properties. E.g. 16 Dim anotherForm As New Form2 Static Dim anotherForm As New Form2 anotherForm.Text = “The new form”

17 CO331 Visual Programming Multiple form code – cont’d The new form will not be visible until the Show() or ShowDialog() methods are invoked. E.g This may be located within a button_Click method. While the window is active, the user can switch between the forms by clicking on them. A secondary form is closed using the methods Hide() or Close(). If there are several secondary forms and data is to be passed between them or if methods are to be accessible from a different form, then such variables and methods should be stored in a module. 17 anotherForm.Show()

18 CO331 Visual Programming Example: Multiple Forms Visual programming: 18 Run

19 CO331 Visual Programming MessageBox The MessageBox dialog displays a String that may incorporate the result of a calculation. E.g. MessageBox.Show(“Sum is: “ & CStr(sum)) This may be shortened to MsgBox(“Sum is: “ & CStr(sum)) 19 The pop-up MessageBox displays a custom dialog that provides the user with information that must be acknowledged before the program moves on.

20 CO331 Visual Programming Function InputBox This is similar to MessageBox, in that it does not occupy screen space permanently – it pops up when required. InputBox takes a single String argument which appears in the dialog and may used to prompt. E.g. Typically the value entered is assigned to a variable, in the process performing any conversion appropriate. 20 num1 = CInt(InputBox("Enter first integer")) num2 = CInt(InputBox("Enter second integer"))


Download ppt "Lecture Roger Sutton CO331 Visual Programming 17: String handling, Date and Time, Multiple forms 1."

Similar presentations


Ads by Google