String and Character types

Document Sample
String and Character types Powered By Docstoc
					String and Character types
           What is a variable
 Temporary memory cell in primary memory
 The contents of the cell is assigned or calculated
 When a new value is assigned, the old value is
 The name (also called identifier) of the variable
  is also the ‘address’ of the memory cell
         What are data types
 A classification of variable data
 For example:
  – Char: consists of a single character that need 1
    byte, eg: ‘A’
  – String: consists of strings of characters,
    eg: ‘Delphi’ and is written in single quotes
   The declaration of the string
 Any variable must first be declared with
  the word var (variable). This instructs the
  computer to make memory available for
  temporary storage of the variable declared
  under the var (before the begin..end)
            String variable
 The length of the string can be specified.
  Insert the declaration in before the
 For example:
   Rules when using variables
 Use descriptive names
 The first character of the variable must be a
  letter (eg: study1 and not 1study)
 The underscore character and numbers
  may be used (eg: my_program_1)
 No spaces or reserved words may be used
 The variable name may not exceed 63
    Assigning values to string
 Example:
               Keep in mind
 edtName cannot be used on its own. The word
  .Text indicates the contents of the edit box.
 Numerical data (eg: 63, -32) cannot be assigned
  to string or character types
 A local variable is declared in a procedure and
  only this procedure may use it.
 A global variable can be used in all the procedure
  Example of
 assignment to
string variables
        The List Box

                             The list box
                             found under the
                             standard tab

                            The list box on a form

•A list box displays a list of sting values.
•To add items to the list box, use Add
•For example: listbox1.Items.Add(sVariable);
•Name a list box with the prefix lst
•Do example on pg 28
        More on the list box
 The data within the list box can be sorted
  alphabetically using the sorted property

 If one wants to hard code a list within the
  list box, use the items property

                    Click here
 Handling string variable values
 Variables are initialised by assigning
  values, eg: sMydata := edtName.Text;
             sMydata := ‘’;
 String variables can be changed by
  concatenation, eg:
      sMydata := sMydata + edtName;
       Concatenating strings
 String data is concatenated with the concat
  function. For example:

 The above can also be written like this:
           Character types
 Declaration:

 Assignment:
 Use Upcase to convert small letters to capital
       cLetter := Upcase(Letter);
       cLetter := Upcase(edtName.Text[1]);
 Strings can be displayed in capitals or lowercase
  letters with the Uppercase and Lowercase
       sMydata := Lowercase(edtName.Text);
       sMydata := Uppercase(edtName.Text);
       Additional operations
 Length function finds the length of a string
     iNumber := Length(edtName.Text);

 Pos function finds the position of a
  character within a string:
     iNum := Pos(‘ ’,edtText.Text);
           //this will find the space within the string
        Additional operations
 Copy function finds part of a string:
sMydata :=Copy(edtName.text, iBeginCopy, iStopCopy);

 Delete procedure removes part of a string:
  Delete(sMydata, 5, 4);
      //Removes 4 characters from position 5
       Additional operations
 Insert procedure inserts characters into a
      Insert(‘Fife’, sMydata, 3);
            //inserts a string at position 3
The memo-component

                 The memo
                 found under the
                 standard tab

                 The memo
                 component on
                 the form
         What is an events?
 Lines of code linked to an event which is
  executed when this event takes place
 User-events such as NoClick and
  OnMouseMove are initiated by the user
 System-events are executed without
  interaction by the user
     What is an object method
 A small code (procedure or function) which is
  linked to an object
 The method determines the ‘behavior’ of the
  object by certain actions
 The method is called with a statement:
      E.g.: edtName.clear;
 Methods are also operations performed on the
 Methods can be remembered as the ‘verbs’ of the
 Up to now you have used the OnClick-
  event – that is when you double-click a
  component, the procedure (event-handler)
  is created automatically. The code you
  entered is executed the moment you click
  the component when the program is
 There are many other events. In the
  object inspector you can look at all
  the events of a particular component.

 To create the event handler of the
  event (procedure), you must choose
  the event in the object inspector and
  then double-click in the open space
  next to the event
              Input boxes
 This will be done next week, because I’ve
  had enough.