cis 451: asp.net concepts dr. ralph d. westfall january, 2009
Post on 20-Dec-2015
219 views
TRANSCRIPT
Former Visual Basic Family Visual Basic
standalone development product VB for Applications
subset of VB works within Microsoft applications
VBScript subset of VBA, for Internet
applications
Previous VBScript "chopped down" version of Visual
Basic many of same
data types syntax control structures functions
ASP.NET Is Not Just VB can also use C, C++, C#, J#,
JScript.NET, Perl, and other languages just as CGI can work with different
languages ASP.NET supports more than 25
languages additional languages expected to be
adapted to work with .NET in the future
VBScript Data Types all VBScript data stored as variants
can't declare with types as in VB actual type based on data assigned to
variable can subsequently use a different type Dim varLength varLength = 5 varLength = "five"
ASP.NET Data Types 11 primitive types built into .NET
framework 4 integer types: Byte, Short, Integer, Long 3 decimal types: Single, Double, Decimal Text: String and Char (Char is really a #) Other: Date and Boolean
objects are also a data type but not a primitive type
Variable Naming Conventions in some programming languages,
people like to attach the data type to the front of the variable name also known as "Hungarian notation" popular at Microsoft not popular with Java programmers
cultural issue between Microsoft and rest of world?
bottom line: Prof. Westfall likes for objects
Object Naming Conventionbtn Button
cbo Comboboxchk Checkboxdg Data grid frm Formgpb Group boximg Imagelbl Label
lst Listboxmnu Menuopt Option button (Radio button) pic Picturerpt Report control txt Text Box
Object Naming Issues advantages of a convention
makes code easier to debug and maintain
improves your grade on projects disadvantages of a convention
1-letter declarations don’t always match
Dim sName as stringDim fSmallDecimal as Single
extra work
Other Variable Naming Issues all variable names must start with a letter use letters and numbers
avoid other (special) characters (other than underscore: _ )
capitalize initial letters of words after prefixes datQuarterStarts datQuarterEnds
use meaningful names of reasonable length
Other Data Types object
can have multiple values of different types
empty: memory location but no value NULL: no data at all
= nothing (not even 0 or blank or "") has no memory location
error
Declaring Variables implicit (ASP.NET figures out type)
Dim strCustName = "Lee" 'string only works if Strict=False (next
slide) explicit (better)
Dim strCustName as String strCustName = "Lee" 'OR Dim strCustName as String="Lee"
Setting Declaration Options to force declaring all variables and
to avoid type conversion errors <%@ Page Language="VB" Strict=True %>
put on very first line of file of ASP file could use Explicit=True instead
allows implicit declarations e.g., Dim strCustName="Li" 'no as Type
Array Variables Dim strUSAStates(49) strUSAStates(0) = "AL"
counts from 0 (same as in C++, etc.) Redim Preserve strUSAStates(50)
changes size without losing current data
Dim strTicTacToe(2,2) for a 3 x 3 array
Constant value can't change naming convention is to capitalize
all letters in name makes code more understandable Const TAX_RATE = .28 UNDERSCORES_SEPARATE_WORDS in contrast to "camel case" coding
Variable Scope procedure level (local i.e., inside a Sub)
variable values are NOT available elsewhere in ASP.NET code
other variables (global level, outside of any Subs) are accessible to all ASP.NET code in a page (all Subs, and in HTML)
'see notes
Subprocedures Sub ProcName([arg1 as Type, etc.])
'lines of code, including arg1 '& other local/global variables End Sub
Call ProcName([arg1, etc.])
Operators = assignment e.g., CustAge = 22 +, -, *, / (arithmetic) ^ exponentiation e.g., RSquare = Radius^2 how does Java do exponentiation?
Operators - 2 \ operator for integer division (contra
/ )7 / 4 = 1.75\ gives integer part: 7 \ 4 = 1
decimals are rounded off before integer division
MOD gives remainder: 7 MOD 4 = 3 "clock arithmetic"
Concatenation Operation “link together in series or chain” (
Webster) & concatenates items regardless of
type "Le " & "Dinh" = "Le Dinh" 1 & 1 = "11"
+ concatenates strings like & does, but 1 + "1" = 2 'except when Strict=On
Concatenation - 2 concatenation can construct
ASP.NET code also (not just for calculations) intIndex = 2 Session("intProdQuantity" & _ intIndex)
evaluates to Session("intProdQuantity2") buya.aspx
Comparison Operators = (equal [how does Java do
this?]) < > (not equal [Java?]) < (less than) > (greater than) <= (less than or equal) >= (greater than or equal)
String Handling Functions, Etc.
strText.ToLower(), "abc".ToUpper() (converts to capitals or lower case) Len(strText) (or strText.Length) Left(strText, intN), Right(strTxt, intN)
(returns intN left or right characters) Mid(strText, intWhere, intN) (intN characters, starting at intWhere) Excel demo
String Handling - 2 strText.IndexOf(StrFind[, intStart]) strText.IndexOf("[some text]") (finds location [actual numeric position]
of StrFind variable or text within a String variable or literal [starting at intStart])
example: check e-mail address format intLoc = strText.IndexOf ("@", 2) (if returns value <1, string wasn't found)
String Handling - 3 strText = " abc " strText.TrimStart() "abc ".TrimEnd() remove spaces from left or right
side
strText.Trim() remove spaces from both sides
Converting Variable Types strType = TypeName(intWeight) (returns data type)
intTons = CInt(dblTons) strPop = CStr(lngPopulation) (convert variables to Integer,
String, etc.)
Checking Types, Etc. IsNumeric(Request.QueryString("Zip"))
checks to see if data is numeric
IsDate(Request.QueryString("DoB"))
checks to see if data is in a date format
If Request.QueryString("CustName") = "" Then If Request.QueryString("Zip") Is Nothing Then
checks for missing data
Line Continuations problem: many VB.NET functions
won't work if split onto more than 1 line
solution: use _ (space underscore) just before line break If Request.Form("CustEMail").IndexOf("@") _ >= 2 AND _
Request.Form("CustEMail").IndexOf(".") _ >= 4 Then