Share
Embed
Annual Pro sale · Save $30
Have presentations of your own?
Share with private links, password protection, and more control over your work.
Transcript
60 slides
-
Instructor: William McDaniel Albritton Slides based on Sams Teach Yourself
ASP.NET 4 in 24 Hours, Complete Starter Kit by Scott Mitchell -
Programming Languages Programming languages use simple instructions to solve
a specific problem Such as calculating the monthly payments of a fixed- rate mortgage Or displaying the current time and date -
Common Features All programming languages have the following in
common: 1. Use variables to temporarily store data 2. Use operators to manipulate the data stored in variables, such as adding (+) two variables 3. Use control structures, such as if statements and loops, to change the flow of the program 4. Use subroutines (functions, methods) to group together similar instructions of the program -
Variables A variable is a place in the computer’s
memory (RAM – random access memory), where you can store data, such as a number or string -
String Data Type A String data type is a
sequence of characters (letters, digits, and other keyboard characters), surrounded by double quotes (") Such as "Hey, y'all!" "Aloha" is a String "The quick brown fox jumped over the lazy dog." is a String -
Variables Variables have 3 components 1. Variables have a
name, which is used to identify the value, such as Number5, PriceOfGas, or LastWords 2. Variables have a data type, which is used to restrict the kind of data that can be stored, such as Integer, Decimal, or String 3. Variables have a value, which is some sort of data, such as 5, 4.99, or "Hey man, check this out!" -
Variables as boxes You should think of a variable
as a box with a name that can only store values (data) of a certain data type name: LastWords data type: String value: "Hey man, check this out!" -
Variable Names Visual Basic has a few restrictions on
variable names 1. Two different variables cannot share the same name 2. Variable names cannot start with a number, so 5Num, 88, or 123abc are not valid names 3. Variable names are not case sensitive (uppercase and lowercase letters are the same), so NumberFive, numberFive, and numberfive are the same variable -
Initializing a Variable Initialization is when you assign a
name, data type, and value to a variable in one statement Example initialization statements for Strings: Dim Movie As String = "Shaw Shank Redemption" Dim Name As String = "Sally Suzuki" Dim LastWords As String = "Hey man, check this out!" Syntax of initialization (Dim is short for dimension): Dim Name As DataType = Value -
Two Parts Initialization actually has two parts: 1. Declaration
makes space in memory for the variable (makes a box) and assigns a name and data type to this space (write name and data type on box) Example declaration statements: Dim Number5 As Integer Dim PriceOfGas As Decimal Dim LastWords As String -
Two Parts Initialization actually has two parts: 2. Assignment
gives a value to a variable (puts a value in the box) by using the assignment operator, which is the equals sign (=) Example assignment statements: Number5 = 5 PriceOfGas = 4.99 LastWords = "hey man, check this out!" The assignment operator (=) assigns the value on the right to the variable on the left -
Rules for Variable Creation You can only create (initialize
or declare) a variable one time You have to create (initialize or declare) a variable before you use it in a method Usually, you create variables at the beginning of your method You cannot have two (or more) variables with the same name -
Comments The apostrophe (') is used to make a
comment in Visual Basic that people can read, but the computer cannot read For example: 'Hey, Computer! 'Your cannot read this! 'Ha! Ha! Ha! 'Comments tell us humans how the code works -
String Operator For Strings, we have the string concatenation
operator (&), which attaches the end of the first string to the beginning of the second string We have to put an & (ampersand) between every String Examples: Dim Word1 As String = "I " & "like" Dim Word2 As String = "ice" & " " & "cream" Dim Sentence As String = Word1 & " " & Word2 & "!" -
String Operator A common mistake is to forget to
begin or end the String with a double-quote Examples of INCORRECT Strings: Dim Word1 As String = "I " & like" Dim Word2 As String = "ice" & " " & "cream Dim Sentence As String = Word1 & " & Word2 & !" -
String Operator Another common mistake is to forget to
put a & between each String Examples of INCORRECT Strings: Dim Word1 As String = "I " "like" Dim Word2 As String = "ice" & " " "cream" Dim Sentence As String = Word1 & " " & Word2 "!" -
String Operator See the class web page for code
examples StringExample.aspx and StringExample.aspx.vp, which demonstrate initializing (creating) and concatenating (adding) strings You have to create a web site in Visual Basic, and then a Web Form with the name StringExample and copy & paste the source code into the .aspx and .aspx.vb files to get this to work -
String Assignment See the class web page for an
assignment on Strings -
Tips: Naming Your File Do not rename your file
When you first create a file, decide on the name, and don’t change it Otherwise, you will have to change the text in the .aspx and .vb files in at least three places -
Tips: Naming Your Variables The variables in the .vb
file should have different names than the Web Controls in the .aspx file See the next slide for an example -
Tips: Naming Example .aspx file HTML code: <asp:TextBox ID="Input"
runat="server"> </asp:TextBox> <asp:Label ID="Output" runat="server"> </asp:Label> Example .vb file Visual Basic code: Dim WordFromUser As String = Input.Text Dim Input As String = Input.Text Dim ThreeStrings As String = WordFromUser & " " & WordFromUser & " " & WordFromUser Output.Text = ThreeStrings -
Tips: Subroutines These subroutines are in the .vb file
Subroutines contain code that processes data Usually there is an input, some processing, and an output There are two subroutines that we will use: 1. Page_Load 2. Button_Click -
Tips: Subroutine Page_Load This subroutine runs when the webpage
is loaded into the browser There is no input for this subroutine The output for the subroutine is sent to the Label Web Control (from the Toolbox) To create this subroutine: open the .vb file, click somewhere inside the class, on the left drop-down menu click: (Page Events), on the right drop-down menu click: Load -
Tips: Subroutines Button_Click This subroutine runs when the user
clicks on a button The input for this subroutine is from a TextBox Web Control (from the Toolbox) The output for the subroutine is sent to the Label Web Control (from the Toolbox) To create this subroutine: open the .aspx file, add a button, change to the Design view, double click on the button, which will automatically open the .vb file and create the subroutine -
Tips: TextBox Web Control The TextBox Web Control is
used to input data from a webpage to the Button_Click subroutine To simplify and clarify your code, you can change the ID="TextBox1" to ID="Input" Default code: <asp:TextBox ID="TextBox1" runat="server"> </asp:TextBox> More better code: <asp:TextBox ID="Input" runat="server"> </asp:TextBox> -
Tips: Label Web Control The Label Web Control is
used to display data output from a subroutine We will always delete the Text="Label" attribute/value pair, because a value will be assigned in the .vb file To simplify and clarify your code, you can change the ID="Label1" to ID="Output" Default code: <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> More better code: <asp:Label ID="Output" runat="server"> </asp:Label> -
Tips: <label> Element Bug When you delete the Text="Label"
attribute/value pair, don’t delete the last > (greater-than sign), or the Visual Basic code that corresponds to the <label> element will not be able to identify the label’s ID Correct code: <asp:Label ID="Label1 " runat="server"></asp:Label> Incorrect code: <asp:Label ID="Label1" runat="server"</asp:Label> -
Methods and Variables Note that variables are only visible
(can be used) within one method Variables in one method are not visible (cannot be used) in a second method See Methods.aspx and Methods.aspx.vb files for an example -
Integer Data Type The Integer data type is a
whole number (not a fraction or a decimal), which is roughly in the range -2 billion to 2 billion Integer examples are 5, -5, 983254, etc. -
Initializing a Variable Initialization is when you assign a
name, data type, and value to a variable in one statement Example initialization statements for Integers: Dim Number5 As Integer = 5 Dim BigNumber As Integer = 2000000000 Dim NegativeNumber As Integer = -395 Syntax of initialization (Dim is short for dimension): Dim Name As DataType = Value -
Variables as boxes You should think of a variable
as a box with a name that can only store values (data) of a certain data type name: Number5 data type: Integer value: 5 -
Arithmetic Operators For Integers (and Decimals), we can use
these four binary operators (binary just means “2”): 1. Addition (+) 2. Subtraction (-) 3. Multiplication (*) 4. Division (/) -
Integer Division Addition, subtraction, and multiplication work as usual,
but Integer division will cut off the decimal point will round up if greater than .5 will round up if x is odd for x.5 (if the number in front of .5 is an odd number) will round down if x is even for x.5 (if the number in front of .5 is an even number) will round down if less than .5 -
Integer Division For example: 10/6 is 2 (1.66666667
– round up) 11/7 is 2 (1.57142857 – round up) 3/2 = 2 (1.5 – round up) 7/2 = 4 (3.5 – round up) 10/3 is 3 (3.33333333 – round down) 10/9 is 1 (1.111111111 – round down) 1/2 is 0 (0.5 – round down) 5/2 = 2 (2.5 – round down) -
Arithmetic Operators For Integers (and Decimals), we can use
the unary minus operator (unary just means “1”), which changes the variable to the opposite value 1.Unary minus operator (-) Examples: -(2) is -2 -(-5) is 5 -
Arithmetic Operators For Integers (and Decimals), we can use
parenthesis to determine the order of operations For example: 2+3*4 is 14 (because 3*4 is 12, and 2+12 is 14) (2+3)*4 is 20 (because 2+3 is 5, and 5*4 is 20) -
Variable Names Visual Basic has a few restrictions on
variable names 1. Two different variables cannot share the same name 2. Variable names cannot start with a number, so 5Num, 88, or 123abc are not valid names 3. Variable names are not case sensitive (uppercase and lowercase letters are the same), so NumberFive, numberFive, and numberfive are the same variable -
Integer Examples For some examples of Integer operators in
action, see the web page IntegerExample.aspx and the Visual Basic file IntegerExample.aspx.vb Try different values and see the results! To view the examples, you have to make a web site, make a file with the name IntegerExample, copy and paste the source code into your files in Visual Web Developer -
Troubleshooting Visual Basic code must go inside a method
Good Visual Basic code: Partial Class IntegerExample Inherits System.Web.UI.Page Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Dim Number1 As Integer = Input1.Text Dim Number2 As Integer = Input2.Text Dim Sum As Integer = Number1 + Number2 Output.Text = "The sum is " & Sum End Sub End Class -
Troubleshooting If you do not have a method (event
handler) for your button click or page load, then your Visual Basic program will not work Bad Visual Basic code: Partial Class IntegerExample Inherits System.Web.UI.Page Dim Number1 As Integer = Input1.Text Dim Number2 As Integer = Input2.Text Dim Sum As Integer = Number1 + Number2 Output.Text = "The sum is " & Sum End Class -
Troubleshooting Make sure you have the correct method
This is the Page_Load method, which does NOT get input from the user, because it runs automatically when the browser loads the webpage Good Visual Basic code: Partial Class IntegerExample Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim Number1 As Integer = 5 Dim Number2 As Integer = 10 Dim Sum As Integer = Number1 + Number2 Output.Text = "The sum is " & Sum End Sub End Class -
Troubleshooting This is the Button_Click method, which gets input
from the user Good Visual Basic code: Partial Class IntegerExample Inherits System.Web.UI.Page Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Dim Number1 As Integer = Input1.Text Dim Number2 As Integer = Input2.Text Dim Sum As Integer = Number1 + Number2 Output.Text = "The sum is " & Sum End Sub End Class -
Troubleshooting The HTML file and Visual Basic file communicate
(send values) through the Text attribute of elements, so the value of the ID attribute for these elements must be the same in both files Good code for File.aspx: <asp:TextBox ID="Input1" runat="server"></asp:TextBox> Good code for File.aspx.vb: Dim Integer1 As Integer = Input1.Text -
Troubleshooting If the value of the ID attribute is
not the same in the HTML and Visual Basic files, your program will not work Bad code for File.aspx: <asp:TextBox ID="Input1" runat="server"></asp:TextBox> Bad code for File.aspx.vb: Dim Integer1 As Integer = Month.Text -
Troubleshooting On the other hand, the name of the
variable should be different than the value of the ID attribute Good code for File.aspx: <asp:TextBox ID="Number1" runat="server"></asp:TextBox> Good code for File.aspx.vb: Dim Integer1 As Integer = Number1.Text -
Troubleshooting The program runs, but the computer gets confused
if you use the same name for the variable and the value of the ID attribute Bad code for File.aspx: <asp:TextBox ID="Number1" runat="server"></asp:TextBox> Bad code for File.aspx.vb: Dim Number1 As Integer = Number1.Text -
Integer Exercise See the class web page for an
exercise using the Integer data type -
Decimal Data Type A Decimal data type is a
decimal number roughly in the range from -79 x 1027 to 79 x 1027 Example Decimals are 3.14, -5.0, and 987.324 -
Common Features All programming languages have the following in
common: 1. Use variables to temporarily store data 2. Use operators to manipulate the data stored in variables, such as adding (+) two variables 3. Use control structures, such as if statements and loops, to change the flow of the program 4. Use subroutines and functions to group similar instructions of the program -
Variables A variable is a place in the computer’s
memory (RAM – random access memory), where you can store data, such as a number or string -
Data Types Data types are used to restrict the
kind of data that can be stored in a variable Visual Basic is a strongly typed language This means that once a variable is declared to be of a certain type, only values of this type can be assigned to this variable Loosely typed languages use variables that can be assigned to any type -
Data Types Once we declare number5 to be type
Integer: Dim Number5 As Integer = 5 Then we have to always assign Integers Number5 = 129 We should not assign Number5 to a Decimal, as it will be automatically converted to an integer Number5 = 99.99 (bad code) We should not assign Number5 to a String, as it will crash your webpage Number5 = "five funny frogs" (bad code) -
Data Types There are many kinds of data types;
however, for this class, we will only use these 3 data types: 1. String Dim LastWords As String = "hey, check this out!" 2. Integer Dim Number5 As Integer = 5 3. Decimal Dim PriceOfGas As Decimal = 4.99 -
Arithmetic Operators For Decimals (and Integers), we can use
these four binary operators (binary just means “2”): 1. Addition (+) 2. Subtraction (-) 3. Multiplication (*) 4. Division (/) -
The Division Operator The result of a division operator
(/) is always a Decimal Even if the two operands are Integers, the result is still a Decimal number. For example: Dim Y as Decimal = 3/2 'Y has value 1.5 If the result is assigned to an Integer variable, the result is cast (changed to a different type) into an Integer value. For example: Dim X As Integer = 3/2 'X has value 1 Therefore, when dividing numbers, it is best to simply keep all the variables as type Decimal -
The Division Operator Examples Dim Num1 As Decimal
= 10/5 'Num1 is 2.0 (not 2) Dim Num2 As Decimal = 10/3 'Num2 is 3.3333333 (not 3) Dim Num3 As Decimal = 10.0/3.0 'Num3 is 3.3333333 Dim Num4 As Integer = 10/3 'Num4 is 3 Dim Num5 As Decimal = 10/3 'Num5 is 3.3333333 Dim Num6 As Integer = 10.0/3.0 'Num6 is 3 -
Arithmetic Operators For Decimals (and Integers), we can use
the unary minus operator (unary just means “1”), which changes the variable to the opposite value 1.Unary minus operator (-) Examples: -(2.2) is -2.2 -(-5.5) is 5.5 -
Arithmetic Operators For Decimals (and Integers), we can use
parenthesis to determine the order of operations For example: 2.0+3.0*4.0 is 14.0 because 3.0*4.0 is 12.0, and 2.0+12.0 is 14.0 (2.0+3.0)*4.0 is 20.0 because 2.0 +3.0 is 5.0 , and 5.0 *4.0 is 20.0 -
Decimal Example See the class web page for an
example with the Decimal data type DecimalExample.aspx DecimalExample.aspx.vb -
Decimal Exercise See the class web page for an
exercise using the Decimal data type