1 / 20

17: String handling, Date and Time, Multiple forms

17: String handling, Date and Time, Multiple forms. Introduction. Strings of characters are a very important form of data. Accordingly a number of methods are available to facilitate their processing.

haru
Download Presentation

17: String handling, Date and Time, Multiple forms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 17: String handling, Date and Time, Multiple forms

  2. 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 If IsNumeric(txtDisplay.Text) Then n = CInt(txtDisplay.Text)ElseMessageBox.Show(“Error in number”) End If

  3. Type conversion Sometimes referred to as ‘casting’ • Conversion methodsStrings are often converted to numbers and vice-versa using • CInt– converts a numeric string to anInteger • CDbl– converts a numeric string to aDouble, and • CStr– converts a number to aString • Parse MethodIn 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 typeE.g. Dim age As Integer Dim studentAge as String = “25” age = Integer.Parse(studentAge)

  4. Type conversion – cont’d • Convert ClassThis 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. Dim sales As Integer = 1000 Dim bonus As Double bonus = Convert.ToDouble(sales) * 0.05

  5. Type conversion – cont’d • Literal type charactersA 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.

  6. 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.

  7. 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. TrimStartandTrimEndmay 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”.

  8. 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!” • ToLowerandToUpperchanges 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!”

  9. 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. • Substringcopies 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. • IndexOfdetermines 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.

  10. String examination –cont’d • IsNumeric( String) This function returnsTrueif is argument is numeric andFalseotherwise. • 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. Function firstName(ByVal letters As String) As String Dim words(4) As String words = Split(letters, " ") Return words(0) End Function

  11. 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. Run

  12. 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 anIntegerin the range 1 to 31 • WeekDayproduces the number of the day of the week • Monthprovides the number of the month in the year • WeekdayNameand MonthNameidentify the corresponding day and month respectively • Yearapplied to a date yields the year as anInteger

  13. Date/ Time Processing –cont’d • DateDiffreturns 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 ofDateIntervalthat indicates the part of the date/time to consider: • Yearindicates 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.

  14. 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, ToShortTimeStringof Date may be used to the same effect. Run

  15. 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: • From File menu, select Add New Item …., orFrom Project menu, select Add Windows Form…., or, etc. • In Add New Item, ensure Windows form is selected. • Provide an appropriate name. • Click open.

  16. 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. Dim anotherForm As New Form2 Static Dim anotherForm As New Form2 anotherForm.Text = “The new form”

  17. Multiple form code – cont’d The new form will not be visible until theShow()orShowDialog()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 methodsHide()orClose(). 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. anotherForm.Show()

  18. Example: Multiple Forms Visual programming: Run

  19. MessageBox • The MessageBox dialogdisplays aString 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)) • The pop-upMessageBoxdisplays a custom dialog that provides the user with information that must be acknowledged before the program moves on.

  20. Function InputBox This is similar to MessageBox , in that it does not occupy screen space permanently – it pops up when required. InputBoxtakes 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. num1 = CInt(InputBox("Enter first integer"))num2 = CInt(InputBox("Enter second integer"))

More Related