Using Visual Basic .NET within an ASP.NET Page - PowerPoint PPT Presentation

1 / 54
About This Presentation
Title:

Using Visual Basic .NET within an ASP.NET Page

Description:

Use Visual Basic .NET to manipulate the code behind the page ... A property is a method that is used to get and set values. Property methods are used to keep ... – PowerPoint PPT presentation

Number of Views:226
Avg rating:3.0/5.0
Slides: 55
Provided by: Kat8210
Category:
Tags: asp | net | basic | page | using | visual | within

less

Transcript and Presenter's Notes

Title: Using Visual Basic .NET within an ASP.NET Page


1
Using Visual Basic .NET within an ASP.NET Page
  • Introduction to ASP.NET
  • By Kathleen Kalata

2
Objectives
  • In this chapter, you will
  • Define object-oriented programming
  • Use Visual Basic .NET to manipulate the code
    behind the page
  • Create classes using Visual Basic .NET
  • Use variables to store data

3
Objectives
  • In this chapter, you will
  • Learn how data types are stored in the .NET
    Framework
  • Use collections to store groups of data elements
  • Create and use procedures in Visual Basic .NET
  • Learn how to use Visual C .NET instead of Visual
    Basic .NET to create ASP.NET pages

4
Object-Oriented Programming
  • Programming statements, or code, can be combined
    into logical groupings functions, event handlers,
    and procedures
  • In Object-Oriented Programming, you create
    objects based upon a class, and then you can
    access the object across multiple Web pages
  • An object is a set of related code that is
    compartmentalized and built upon these classes
  • The creation of an object consists of two
    separate tasks
  • The first task is to create the object
    definition, called the class, it is not an object
    that you can access directly
  • You use the class as the template for creating
    the new object. When you create an object, you
    are really creating an instance of the class

5
Instantiation
  • Instantiation is the process of declaring and
    initializing an object from a class
  • You can create many objects from the same object
    definition
  • Create a class named TaraStoreClass
  • The name of the class is TaraStoreClass
  • Contains one variable named StoreName
  • Public Class TaraStoreClass
  • Private StoreName As String "Tara Store"
  • End Class

6
Protecting the Class Code
  • Restrict applications access to the class
  • Public - can interact with other objects outside
    of the base class
  • Private - can only be called within the base
    class
  • Protected - can be called from within the base
    class, and within subclasses
  • Subclasses are used to create a class that
    inherits from a base class
  • Friend - can be called anywhere from within the
    same application

7
How to Instantiate an Object
  • Instantiate an object based on the class
    definition
  • Declare a variable with the keyword Dim, to store
    the object
  • Use the keyword New to identify that this is an
    object based on a class definition
  • The class in the sample code below is named
    TaraStoreClass
  • The object is stored in the variable named
    Ch5Class
  • The class is contained within the Chapter5
    namespace
  • Dim Ch5Class As New Chapter5.TaraStoreClass()

8
Commonly Used Terms
  • Decision control structures - allow you to
    organize the order in which the code is executed
  • Event handlers - execute code when the event
    occurs
  • Procedures - named grouping of one or more
    programming statements, executed when the
    procedure has been called by another procedure or
    event handler

9
Commonly Used Terms
  • Parameters - values passed to the subprocedures
    and functions multiple values are separated in a
    comma delimited list within a pair of parentheses
  • Built-in functions - include mathematical
    functions, date and time, string, and formatting
    functions, among others
  • Functions - a named grouping of one or more
    programming statements that can return a value to
    the program that called the function by using the
    return keyword

10
Properties
  • Properties are used to set the value of a
    variable defined within an object
  • Properties are identified by the name of the
    object, followed by a period, followed by the
    name of the property
  • Properties are assigned a default value within
    the object definition, or the value is set as
    unassigned
  • All new objects inherit the same properties as
    the original object definition

11
Object-Oriented Programming Concepts
  • Inheritance allows you to derive the interface
    and behaviors from another class.
  • The inherits keyword allows you to inherit from
    another .NET class.
  • With the Common Language Runtime, all objects are
    inherited from the System namespace, so some of
    the properties such as ToString will apply to
    most objects.
  • Encapsulation - the ability to assign different
    values to each objects properties.
    Encapsulation means that the inner workings of
    the object, such as the procedures and
    properties, are maintained within the object so
    objects or programs are self-contained. You can
    create multiple objects based on the same class,
    as long as the names are unique

12
Object-Oriented Programming Concepts
  • Abstraction is the ability to create a concept
    using code. For example, a customer is a person
    who makes purchases from a company. A customer
    object is the programming code that represents
    the customer. The customer object may contain
    information about the customer, such as their
    shipping address, and purchases
  • Polymorphism is the ability to create code that
    can be applied to objects that are derived from
    different classes. If you have an application
    that contains a student object and a faculty
    object, you can write an application that
    retrieves the name property from both objects.
    Polymorphism allows you to apply programs across
    different classes

13
WinForms
  • The Visual Basic .NET application forms
  • These forms interact with the user and are
    inherited from the System.Windows.Forms namespace
  • When you build the application, Visual Studio
    .NET compiles the code and stores the .dll file
    that contains the namespace in the projects bin
    directory
  • The bin directory has execute permission which
    allows the code to be executed at run time

14
The Page Class
  • The ASP.NET page inherits the code behind the
    page which is the page class
  • By default, when you create an ASP.NET Web page
    in Visual Studio .NET, a class that represents
    that page is created which is named after the Web
    page
  • On the first line of the Web page, Visual Studio
    .NET inserts the inherits keyword and identifies
    the name of the class that is created in the code
    behind the Web page
  • When the build method is called, the code behind
    the page is compiled into the same .dll file that
    contains the namespace for the application

15
Creating a Class Definition
  • Referring to the Class in the class definition
  • Because you have only created the class
    definition, and have not created an instance of
    the object yet, you cannot refer to the object by
    its name within the class definition
  • Therefore, the keyword Me can be used in place of
    the object name within the class definition
    (Note In C the keyword Me is replaced with the
    keyword this.)

16
Creating a Class
  • Create Chapter5 Web application and a class named
    TaraStoreClass
  • The class definition creates two variables that
    can be called from the ClassVariables.aspx page
  • Create the StoreName and StoreEmail instance
    variables
  • These variables are private to the TaraStoreClass
    class
  • They are not available outside of the
    TaraStoreClass class
  • Public StoreName As String "Tara Store"
  • Public StoreEmail As String "info_at_TaraStore.com
    "

17
Creating a Class
  • Create Chapter5 Web application and a class named
    TaraStoreClass
  • Modify the Code View Options
  • Add line numbers to the code view
  • Tools Options Text Editor Basic Line
    Numbers

18
Modifying the Code View Options
19
Instantiate the Object
  • Import the ClassVariables.aspx
  • In the Page_Load procedure create a new object
    named Ch5Class based on the TaraStoreClass
  • Dim Ch5Class As New Chapter5.TaraStoreClass()

20
Instantiate the Object
  • Use the ToString method to retrieve the values
    from the variable in the new class

21
(No Transcript)
22
Viewing the ClassVariables Page
23
IL Disassembler
  • Use the IL Disassembler (ILDASM) tool to view the
    namespace of the Chapter5 assembly and locate any
    of the classes created within your Windows or Web
    application
  • Go to the Visual Studio .NET command prompt
  • Change to the bin directory of your project
  • cd Inetpub\wwwroot\Chapter5\bin
  • ILDASM Chapter5.dll
  • View the TaraStoreClass class information and
    the StoreEmail field to see that it is a public
    variable of type string

24
IL Disassembler
25
Variables
  • A variable declaration consists of two parts a
    variable name and a data type
  • Declaring a variable is the process of reserving
    the memory space for the variable before it is
    used in the program
  • You must declare all variables before they are
    used
  • Variables that are declared in the class are
    referred to as members, member variables, or
    instance variables
  • The data type identifies what kind of data the
    variable can store

26
Variables Declaration
  • Where the variable is defined will determine
    where the variable can be used within the
    application
  • You use the keywords private, public, friend, and
    protected to specify the scope of the variable
  • Private variables are only available to code
    within the local class
  • Public variables, called global or public
    variables, are used outside the class
  • Friend variables are used only within the current
    application or project

27
Naming Variables
  • Choose a descriptive name, one that has some
    meaning or association with the contents or
    purpose of the variable
  • No commands or keywords as your variable name
  • Begin with a letter
  • Do not use a period or space within a variable
    name
  • Avoid using any special characters except for the
    underscore
  • Visual Basic .NET commands and variables are not
    case sensitive
  • The first letter of each word is usually
    capitalized

28
Use the First Three Characters to Identify the
Data Type
29
Assigning Values to Variables
  • The assignment operator is the equal sign ()
  • Constants
  • Constant is a variable that does not change such
    as tax rates, shipping fees, and values used in
    mathematical equations
  • The const keyword is used to declare a constant.
    The name of a constant is usually all uppercase
  • When you declare a constant, you need to assign
    the value to the constant

30
Concatenation
  • Concatenation is the process of joining one or
    more strings
  • You can also use built-in methods to determine
    the length of the string, locate characters
    within the string, and truncate the spaces at the
    beginning or end of the string
  • The string can be a literal string, or the result
    returned from an expression, or a variable that
    contains a string data type
  • Dim lblControlContent Ch5Class.StoreName.ToStrin
    g() _
  • "ltbr/gt" Ch5Class.StoreEmail.ToString()
  • lblContact.Text lblControlContent

31
Data Types
  • The two categories of types
  • Reference types are strings, classes, arrays,
    collections, and objects.
  • Value types are also referred to as primitive
    types or structures they are Boolean, Integer,
    Decimal, Char, and DateTime
  • The managed heap contains memory addresses that
    are used to store variables

32
String
  • Strings are variable in length so you dont have
    to specify the number of characters in the string
    when you create the string object
  • Ltrim, Rtrim, and Trim remove the blank spaces
    from the preceding, ending, and both ends of the
    string respectively
  • Asc provides the strings ANSI character
    equivalent
  • Chr provides the ASCII character equivalent. Chr
    is a useful method when you want to create
    control characters such as carriage returns
  • Replace replaces one string with another

33
String Object Methods
  • Split returns a string that has been split using
    a delimiter to identify where to split the string
  • Concat allows you to join strings together
    without using the concatenation operator
  • Compare - allows you to compare two strings. If
    they are equal, the method will return 0. If the
    value on the left side is less than the value on
    the right side, then a negative integer is
    returned otherwise a positive integer is
    returned
  • LCase and UCase - converts the case to lower and
    upper case respectively
  • Dim Password As String
  • Password LCase(txtPassword.Value)
  • LblPassword.Text Password

34
Char
  • Char data type allows you to store a single text
    value as a number
  • Stores a number between 0 and 65, 535
  • Represents a character in categories such as
    digit, letter, punctuation, and control character

35
Numeric Values
  • Numeric Data Types
  • Byte - stores an integer between 0 and 255
  • Short - is a 16-bit number. Therefore, a short
    data type can only store values from -32,768 to
    32,767
  • Integer data type is a 32-bit whole number
  • Long - is a 64-bit number
  • Real number data types
  • Single - represents a single-precision floating
    point number
  • Double - supports larger numbers than the single
    data type
  • Decimal - can store numbers up to 28 decimal
    places and is often used to store currency data

36
DateTime
  • DateTime data type is used to store any dates and
    times between 01/01/0001 and 12/31/9999 as
    mm/dd/yyyy and hhmmss
  • The date value is enclosed within a pair of pound
    signs
  • Dim MyBirthday As DateTime
  • MyBirthday 3/22/2002

37
Boolean
  • A Boolean data type only has two possible values,
    True value or False value
  • In binary math, one represents true and zero
    represents false
  • In Visual Studio .NET the True value is converted
    to -1, and the False value is 0

38
Using a Property to Retrieve and Set the Value
of a Variable
  • To use a variable outside of a class from a
    different class, use the property function, or
    declare the variable public
  • Set the value of the variable directly, or use a
    property
  • A property is a method that is used to get and
    set values
  • Property methods are used to keep private the
    variables internal to your code
  • Property method - to retrieve and set the
    variables value
  • Set the value and retrieve them indirectly

39
Using a Property
  • The ReadOnly and WriteOnly keywords are placed
    before the Property keyword
  • ReadOnly prevents you from writing to the
    property
  • WriteOnly allows the value to be changed, but not
    retrieved from the property
  • Public ReadOnly Property NewStoreName() As String
  • Public WriteOnly Property NewStoreName() As String

40
Collections
  • Data can be stored in structures called
    collections
  • A collection is an object that can store other
    elements
  • By storing some types of data in a collection,
    you would not need to use a database
  • The System.Collections namespace provides access
    to classes that manage data
  • Each item in the collection is also referred to
    as an element
  • These five collections
  • ArrayList, HashTable, and SortedList collections
    provide access to any element without having to
    rotate through the other elements
  • Queue and Stack collections need to rotate
    through the collection sequentially in order to
    locate an element

41
ArrayList
  • The ArrayList stores each item in sequential
    order, and each item is indexed with a number
  • Do not need to define the size of the ArrayList
  • Each item in the ArrayList is identified using an
    index number that indicates its position
  • All of the items must be of the same data type
  • ArrayLists are zero-based. The first item in the
    ArrayList is at position 0. So, an ArrayList size
    of 3 means it has 4 items

42
Create an ArrayList
  • Create an ArrayList
  • Dim StateAbbrev As New ArrayList
  • StateAbbrev.Add "IL"
  • StateAbbrev.Add "MI"
  • StateAbbrev.Add "IN"
  • Retrieve the value of any element individually,
    or using a for-next loop
  • Response.write(StateAbbrev())
  • Insert an element into the first position and
    remove it
  • StateAbbrev.Insert(0, "OK")
  • StateAbbrev.Remove("OK")

43
Properties and Methods of ArrayList
  • Add and Remove will add or delete a single
    element.
  • Insert and RemoveAt methods will add and remove
    elements at a specific index position.
  • AddRange and RemoveRange methods allow you to add
    or remove a group of elements.
  • IndexOf property allows you to find the position
    of the element in the list. A value of -1 means
    the element was not found in the list.
  • Count property identifies the number of items in
    the array which will be the largest index number
    plus 1.
  • Clear method allows you to remove all of the
    elements.

44
HashTables
  • HashTable collection creates the index of
    elements using an alphanumeric key like an
    encyclopedia
  • The keys is a collection of alphanumeric values,
    and the values is a collection of elements
  • Add and Remove method
  • Items are added using the key and value pair
    separated with a comma
  • The key is the first parameter passed with
    quotation marks
  • The second parameter is the value

45
Other Collections
  • SortedList Class- Indexed by both the key and the
    item so the index position will change
    frequently. You can iterate through the list with
    a For-Next loop
  • Queue Class - Provides sequential access to the
    elements but stores them in First In, First Out
    (FIFO). This is similar to a roller coaster ride.
    Typically, the people in the first car are let
    out first
  • Stack class - Provides sequential access but
    stores them in Last In, First Out (LIFO) order.
    The Stack is similar to the line in a theatre,
    church, or crowded elevator. The first one to
    enter the room is the last one to leave the room

46
Procedures
  • Subprocedures do not return values and cannot be
    used in an expression value
  • Create a Subprocedure
  • Use the keywords public or private
  • Declared using the keyword sub
  • Exit sub statement to exit the subprocedure
  • End with the keywords end sub
  • Call keyword can be used to call a function or
    subprocedure and is optional

47
Event Procedure
  • You can intercept the event using an event
    handler
  • Events such as click are intercepted by event
    handlers such as onServerClick
  • An event handler is also known as an event
    procedure
  • An event procedure is not executed until an event
    triggers the event procedure
  • An event procedure does not return a value
  • The Page_Load event procedure is triggered when
    the page is loaded into the browser

48
Event Procedure
  • Event procedures names are based upon the name of
    the object and the event name
  • Event handler is identified with the prefix on
    and the event name
  • An underscore (_) is used to separate the object
    name and the event name
  • Sub objectName_eventHandler(sender as Object, e
    as EventArgs)
  • action and control statements
  • End Sub

49
Functions
  • A function is a block of code that is grouped
    into a named unit.
  • Built-in functions inherit from a .NET Framework
    class
  • User defined functions are declared
  • Public functions are visible to all other
    functions in the application
  • Private functions are only available within the
    context where they are declared
  • Public Function GetStoreName() As Integer
  • 'This function returns an integer
  • Return 23422
  • End Function

50
Passing an Argument to a Function
  • A pair of parentheses follows with zero or more
    arguments, also known as parameters, which are
    passed to the function when it is called
  • If no arguments are passed, you use an empty pair
    of parentheses
  • If multiple arguments are used, you use a comma
    to separate each argument

51
Returning a Value From a Function
  • The keyword return is used to identify the value
    that is returned to the function call
  • Exiting a Function
  • Exit function is a jumping control jumping
    controls allow you to temporarily halt the
    execution of a code block, and move to another
    section of code
  • Create a Function
  • You can create a function within the class of the
    Web page, or within the Visual Basic .NET file

52
Introduction to C
  • The syntax of C is similar to JavaScript and C
  • The program is compiled by the C compiler the
    code is compiled into the same managed
    Intermediate Language code that is also generated
    by the Visual Basic .NET compiler
  • The .NET Base Classes and the Visual Studio
    development environment are available across
    programming languages
  • You have access to the same Windows Form tools
    and ASP.NET WebForm tools

53
Differences with C
  • C is case sensitive
  • When you create an array using C, instead of
    using parentheses around the index position, use
    square brackets
  • Anytime you work with one or more statements, you
    enclose the code in curly braces
  • To assign a value to a variable, specify the data
    type first, then specify the variable name
  • Sample code to declare a variable and assign it a
    value
  • String StoreName "Tara Store"
  • int counter 1

54
Differences with C
  • Comments in C are similar to comments in
    JavaScript
  • // This is a single line comment
  • /
  • This is a multiline comment
  • Always document your code
  • /
  • Declare the variable public in C you use all
    lower case letters for the keyword public
  • When you create an object data specify the data
    type of the object first, then the name of the
    object. Use the assignment operator to assign the
    name to a new Object() where the keyword new is
    lowercase
Write a Comment
User Comments (0)
About PowerShow.com