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
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
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!"
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
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
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
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
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
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
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 & "!"
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 & !"
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 "!"
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
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
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
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
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
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>
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>
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>
(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
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
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
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
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 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
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
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
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
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
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
(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
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
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
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
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
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
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)
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
(/) 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
= 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
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
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