Introducing Data Types Variables and Expressions The importance

Introducing Data Types Variables and Expressions

The importance of sequence in programming; the event-driven programming paradigm; the anatomy of C# event-handlers The importance of variables in programming; C# data types; creating and using simple variables The assignment operator; expressions string and simple arithmetic operators Type conversions; string to numeric; numeric to string

Code Sequence You’ve heard several times that a program is just a set of instructions to describe how to do a task. . . Clearly the order or sequence in which instructions are given is often very important. . . 1 st left 2 nd right 2 nd left 1 st left . . . the same instructions executed in a different order can lead to very different outcomes

Programming paradigms procedural the sequence of operations is determined by the structure of the program, not by the user’s actions programs run instruction-by-instruction, from start to finish

Programming paradigms event-driven the sequence of operations is largely determined by user’s actions. Programs wait for events to occur, respond to those events, then wait again. Code order execution depends on which events occur, and in what order programs respond to the user rather than vice-versa, leading to a more interactive experience. However, the sequence of instructions within each event code is still significant.

event-handler subroutines (methods) program ‘waits’ until the user ‘does something’ C# fires an ‘Event’ event code is executed event-handler procedures hold instructions on what to do, and in what order, when a particular event is fired for a particular object the heart of most event-driven applications

Let’s take a quick look at an event-handler in C# It is a method, or a subroutine, in the Class that represents the form. Most GUI controls have lots of potential events they can react to, but also a default event – for the Button object it is the Click event

SE 2 S 551 Computer Programming the anatomy of a C# event-handler scope data type name normally the control’s name code block - executed whenever the event is raised by this object event the specific event this code will respond to an object variable representing the object that raised the event a variable of class Event. Args which returns information about the event

In VCE it is always best to rename GUI objects immediately and before generating any Event Handlers for them… . . . because the Event Handler’s given name is derived from the object’s name If you change the object’s name later the event handler will still work – but its name no longer matches; not good programming practice

You can’t just edit the event hander’s name to match either. . . because the event handler also has code in the you must edit both parts if you want to rename an event hander once created file

For the same reason, to delete an event handler once created. . . you must delete both sections of code

Always let VCE create event-handler methods for you, using the Events button on the Properties panel if you do not want the default event for an object or just double-click the object (once it has been renamed!) to create its default event-handler stub

. . . but understanding what they are, why we use them, how to use them, variables, . . . is vital to understanding programming

Variables programs use variables to store and manipulate data (information) a variable is a container in which you place an item of data (information) for storage, then later retrieve back its contents you may work with different types of data in a C# program each different type requires a different storage container. . .

a C# program typically stores, processes and manages lots of pieces of information; to store something and then retrieve the contents of the ‘right box’ you give a name to each variable (labelling the box) so to use a variable you must. . . specify its type give it a name assign a value to it you may then retrieve a stored value, use it in expressions, or replace its contents with a new value

SE 2 S 551 Computer Programming . NET languages (C#, VB, etc) use a common set of data types. . . C# name bytes . NET base class byte 1 System. Byte bool 1 System. Boolean char 2 System. Char int 4 System. Int 32 short 2 System. Int 16 long 8 System. Int 64 float 4 System. Single double 8 System. Double decimal 12 System. Decimal object 4 System. Object

Suppose we want to work with an item of text, perhaps to store the name of someone. . . - first, we create a variable to store the information. . . - in this case the most appropriate type of variable is string - we give the variable a name, so we can refer to this piece of information within our code. . . - the C# code we need is. . . string persons. Name;

string persons. Name; match the data type to what it will store a ns. N me rso pe if possible use integers; quick and no loss of accuracy use floating point numbers only if a fractional part must be stored variable name can be anything made from the letters, (A-Z; a-z), the digits (0 -9), and the underscore “ _ “ - it must start with a letter, or “_” - is case sensitive; so “name” is different to ”Name” ** always try to use a meaningful variable name **

the assignment operator “fred” once you’ve created a variable, you may store something (of the same type) in it persons. Name = “fred”; rso pe e am ns. N = the symbol (assignment operator) causes the thing on the left-side to have its value changed to whatever is on the right-side in C# you must assign a value to a variable before you first use it the operator = doesn’t test for equality but rather it causes whatever is on the right-side to be assigned to whatever is on the left-side

persons. Name = “fred”; persons. Name = “jane”; “fjar ende ” a variable is so-called because its a storage space whose value can vary (be changed) pe rso a ns. N me

some examples. . . string first. Name; first. Name = “fred”; string last. Name = “flintstone”; string another. Name; another. Name = first. Name; string 2 nd. Name; first. Name = 2 nd. Name;

variables to hold numbers variables for manipulating and storing numbers within a program are much the same, except several types of number are identified integers are whole numbers (1, 45, 623, -87653) they require a different container to store them – int my. Age; int my. Age = 21; 21 my. Age = 21;

more about int the int container holds whole numbers exactly, with no loss of precision, but it has a limited capacity or range they are the most widely used number type within programs, particularly good for counting and for controlling loops. NET has several variations of integer; allow different ranges of number to be stored, using varying amounts of memory short int long -32, 768 -2, 147, 483, 648 -9, 223, 372, 036, 854, 775, 808 to to to +32, 767 +2, 147, 483, 647 +9, 223, 372, 036, 854, 775, 807

variables to hold numbers float and double are used for fractional numbers (e. g. 3. 141526) again, think of these as different types of container double my. Height; double my. Height = 1. 82; 2 1. 8 my. Height = 1. 82;

more about float, double, and decimal when computers store fractional (decimal) numbers they can only do so to a certain degree of accuracy or precision unlike integers, there is a limit to the precision of these numbers, but they do have an almost unlimited capacity or range float double decimal approximately 7 decimal places approximately 16 decimal places massive precision, but a lot more memory

more about float, double, and decimal literal fractional values are always assumed to be of type double to specify a float or a decimal literal value you must add an extra character on to the end to signify this. . . float my. Height; my. Height = 1. 82 F; decimal my. Height = 1. 8206512523003 M;

using expressions so you know how to create variables and how to assign values to them assigning literal values to variables is useful but limiting more often we assign the results of an expressions combine variables (and possibly literals) using various operators to yield new values we’ll start with some simple text and numeric operators, and discuss the complexities of using them more fully next week

string operators there is really one string operator, so this is easy it is called concatenation – a long word meaning ‘to join’ it uses the symbol “+” and it joins the contents of two strings. . . string my. Name = “mitch” + “langford”; string text. A = “hello”; string text. B = “Fred”; string text. C; text. C = text. A + “ again ” + text. B; assigns “mitchlangford” to the string variable called my. Name assigns “hello again Fred” to the string variable called text. C

simple arithmetic operators as you might guess, these perform the familiar mathematical calculations, processing numeric data types + - adds two numeric values * / multiplies two numeric values subtracts two numeric values divides two numeric values the only ‘catch’ is if you divide (/) two integer values – where the result is also returned as an integer

simple arithmetic operators int the. Ans = 56 + 22; assigns 78 to the int variable the. Ans float val 1 = 56. 35 F; float val 2 = 4. 22 F; float val 3; val 3 = val 1 * val 2; the float variable val 3 would contain the value 237. 797 int val 1 = 10; int val 2 = 4; the expression “val 1 / val 2” returns the value 2 (the decimal fraction is lost)

. . . but understanding variables, and how to use them . . . is vital

SE 2 S 551 Computer Programming ne “ja 2 21 1. 8 ” C# is a strongly-typed language. . . Err? ? you cannot take a string value and assign it to an int or a float variable you cannot take the value stored in a float and assign it to an int variable you cannot take a float or an int value and assign it to a string variable

SE 2 S 551 Computer Programming “ 25” plus “ 46” is. . . ? txt. Num 2. Text “ 7 “ 4 25 txt. Answer. Text 1” “ 2 6” 5” txt. Num 1. Text 6 +4 1 =7

Convert string to number there are several ways to convert string data (i. e. text) into an equivalent numeric value. Since there are many types of numeric data (int, float, etc) we must convert to a specific numeric type. One method is to use Parse. . .

Convert number to string it would be reasonable to expect to use string. Parse(), but no! all number types (int, float, long, decimal, etc) have a built-in method for converting their current numeric value into a string it is called To. String()

txt. Num 1. Text “ 4 6” “ 2 5 ” txt. Num 2. Text su “re txt. Answer. Text answer second. Value e+ lu ue Va a st d. V fir con se lt” first. Value

You will have the creation of the Simple Adder application demonstrated to you.
- Slides: 37