Handling Strings and Containment
-
Upload
agrippa-mungazi -
Category
Documents
-
view
216 -
download
0
Transcript of Handling Strings and Containment
8/11/2019 Handling Strings and Containment
http://slidepdf.com/reader/full/handling-strings-and-containment 1/11
CONTAINMENT AND EVEDRIVEN PROGRAMMI
8/11/2019 Handling Strings and Containment
http://slidepdf.com/reader/full/handling-strings-and-containment 2/11
Containment/aggregation/composition
• These are links/associations between objects that allow them communicate.
• For instance: a form on a screen is an object. On the object foare other objects, e.g. delete button, display button, exit butt
• These buttons (which are objects) communicate with the formobject).
• Thus the linkage between the form and the buttons is called thcontainment.
8/11/2019 Handling Strings and Containment
http://slidepdf.com/reader/full/handling-strings-and-containment 3/11
Event Driven Programming
• This is whereby the sequence of code execution is determined(triggered) by external events or by user actions, e.g. clicking on a form is an event that can form a program to execute certsections of the code.
• Thus clicking a button or menu item is an event.
• This is important because the programmer cannot predict the sequence a user can perform his/her tasks.
• Event handlers: small program codes which are invoked (calleresponse to external events.
• Dispatcher: small program codes that call event handlers so thcan be processed.
8/11/2019 Handling Strings and Containment
http://slidepdf.com/reader/full/handling-strings-and-containment 4/11
Handling Strings
8/11/2019 Handling Strings and Containment
http://slidepdf.com/reader/full/handling-strings-and-containment 5/11
What is a string?• A string is a series of alphanumeric characters enclosed in quota
• A string is data type that stores one or more characters, which calphabetic or alphanumeric.
• If a variable is declared as a string, it must not be used for dailymathematical calculations because strings are not numbers.
• A string is sometimes just referred to as „text‟. Any type of alph
numeric data can be stored as a string: “Birmingham City”, “3/“36.85” are all examples of strings.
• Each character within a string will be stored in one byte using itmodern systems might store each character in two bytes using i
• The maximum length of a string is limited only by the available
8/11/2019 Handling Strings and Containment
http://slidepdf.com/reader/full/handling-strings-and-containment 6/11
Declaration of strings
Dim Name As String
• If a value of string variable is assigned within the program code, itenclosed in quoates e.g.
Name = “Agrippa”
Name = “”
• The second example will be a null (un-initialised) string. It is diffe
space but is distinct in nature.
• Strings can be of fixed length or variable length
8/11/2019 Handling Strings and Containment
http://slidepdf.com/reader/full/handling-strings-and-containment 7/11
Variable length string
• Variable length string
Dim Surname As String
• Variable length does not specify the number ofcharacters, each data items occupies the numbeof spaces it requires.
• This is declared as follows
• Dim Sname as string
8/11/2019 Handling Strings and Containment
http://slidepdf.com/reader/full/handling-strings-and-containment 8/11
Fixed length string• Fixed length string
Dim StudentNumber As String * 5
• Fixed length: has specified maximum number of charact
Dim Sname as string * 20
• The field will be allocated 20 spaces in the computer me
• If a name shorter than 20 characters is entered, blank spremain unoccupied since they would have been assigned already.
• If too long than allocated space, the extra characters are
8/11/2019 Handling Strings and Containment
http://slidepdf.com/reader/full/handling-strings-and-containment 9/11
Processing strings: Sorting
• Using relational operators.
if Name1< Name2 then
Print Name1 & “ “ & Name2
else
Print Name2 & “ “ & Name1
end if
8/11/2019 Handling Strings and Containment
http://slidepdf.com/reader/full/handling-strings-and-containment 10/11
Processing Strings cont‟d • Count number of characters in a string using len
•The function Len returns the number of characters stored in a strinit.
Syntax: CharacterNumber = len(string) then Print CharacterNumber
Print Len(string)
• Question:
•Design a program in VB to capture five names of computer scienceand count the number of characters in each student name. the prodisplay the student name and in front of it, the number of charact
• E.g. Mungazi Davies This name has 14 characters
8/11/2019 Handling Strings and Containment
http://slidepdf.com/reader/full/handling-strings-and-containment 11/11
VB codeDim Spaces As Integer
Dim EmployName As String
Dim Index As Integer
Dim Charater As String * 1EmployeeName = txtName.text
Spaces = 1
For index = 1 to Len(EmployeeName)
Character = Mid(EmployeeName, Index)
If character = “” then
Spaces = Spaces + 1
End if
Next Index
If Spaces >= 2 then
Msgbox (“too many spaces”), vbExclamation