Chapter No 6: Control Structure
Chapter No 6: Control Structure
<script language="vb" runat="server"> Sub Page_Load() Dim intEarn As Integer Dim intTax As Integer Dim decTotal As Decimal intEarn = 150 intTax = 23 decTotal = intEarn - ((intEarn/100)*intTax) Display1.Text = decTotal End Sub </script> <html> <head> <title>Declaring Variables</title> </head> <body> Your total earnings after tax are $ <asp:label id="Display1" runat="server" /> </body> </html>
Structured data types Array Declare an array One dimentional Array Dim nameofarray(size) as datatype
Example of an Array
Sub Page_Load() Dim strArrayDetails(2) As String Dim intLoop As Integer strArrayDetails(0) = text1.Text strArrayDetails(1) = text2.Text strArrayDetails(2) = text3.Text Message1.text = CStr(strArrayDetails(0)) Message2.text = CStr(strArrayDetails(1)) Message3.text = CStr(strArrayDetails(2)) End Sub </script>
<html> <head> <title>Text Box Example</title> </head> <body> <asp:label id="message1" runat="server" /> <br /> <asp:label id="message2" runat="server" /> <br /> <asp:label id="message3" runat="server" /> <br /> <form runat="server"> Please enter your name: <asp:textbox id="text1" runat="server" /> <br /><br /> Please enter your address: <asp:textbox id="text2" runat="server" rows=5 textmode="multiline" /> <br /><br /> Please enter your chosen password: <asp:textbox id="text3" runat="server" textmode="password" /> <br /><br /> <input type="Submit"> </form> </body> </html>
IfThen structure <script language="vb" runat="server"> Sub Page_Load() Dim theNumber As Integer Dim theGuess As Integer theNumber = int(10 * rnd) + 1 If Page.IsPostBack Then theGuess = Guess.SelectedItem.Value If theGuess > theNumber then Message.Text = "<BR><BR>Guess is too high<BR>Try again it was " _ & theNumber End If If theGuess < theNumber then Message.Text = "<BR><BR>Guess is too low<BR>Try again it was " _ & theNumber End If If theGuess = theNumber then Message.Text = "<BR><BR>Guess is correct!" End If End If End Sub </script>
<html> <head></head> <body> <form runat="server"> What number am I thinking of? <asp:dropdownlist id="Guess" runat="server"> <asp:listitem>1</asp:listitem> <asp:listitem>2</asp:listitem> <asp:listitem>3</asp:listitem> <asp:listitem>4</asp:listitem> <asp:listitem>5</asp:listitem> <asp:listitem>6</asp:listitem> <asp:listitem>7</asp:listitem> <asp:listitem>8</asp:listitem> <asp:listitem>9</asp:listitem> <asp:listitem>10</asp:listitem> </asp:dropdownlist> <br> <br> <input type="submit" value="Submit guess"> <asp:label id="message" runat="server"/> </form> </body> </html>
If ThenElse structure if confirm=Yes then Message.text=This is correct fax numer Else Message.text=please enter the correct Fax number End if
If ThenElse if structure
IfThenElse if If confirm=Fax Then Messsage.text=please Enter your Fax number Else if confirm=Email Then Message.text=please enter your email numher Else if confirm=voicemail Then Message.text=please enter the voice mail number Else Message.text=NO confirmation will be sent. End if
Select case
<script language="vb" runat="server"> Sub Page_Load() If Page.IsPostBack Then Select Case(Destination.SelectedItem.Value) Case "Barcelona": Message.Text = "You selected Spain's lively Catalan city" Case "Oslo": Message.Text = "Experience the majesty of Norway's capital city" Case "Lisbon": Message.Text = "Portugal's famous seaport and cultural hub" Case else Message.Text = "you did not select a destination we travel to" End Select End If End Sub </script> <html> <head></head> <body> <form runat="server"> Select your choice of destination: <br><br> <asp:radiobuttonlist id="destination" runat="server"> <asp:listitem>Barcelona</asp:listitem> <asp:listitem>Oslo</asp:listitem> <asp:listitem>Lisbon</asp:listitem> </asp:radiobuttonlist> <br><br> <input type="submit" value="Submit Choice"> <br><br> <asp:label id="message" runat="server"/> </form> </body> </html>
Sub Page_load() Dim counter As Integer For counter = 1 to 6 response.write("this is value no " & counter & "") Next counter
The ForEach structure For Each element In group [statement] [Exit for] [Statement] Next [element]
The Do Structure:
The DoUntil /While structure: Do Code here Loop Until /While a condition is True
<script language="vb" runat="server"> Sub Page_load() Dim diceRoll As Integer Do diceRoll = int(rnd * 6) + 1 Message1.Text = Message1.Text & "Rolled a: " & diceRoll & "<br />" Loop {Until/While} diceroll = 6 End Sub </script> <html> <head> <title>Do Loop Example</title> </head> <body> <asp:label id="message1" runat="server"/> </body> </html>
Sub add() or
Sub add(no1 as integer,no2 as integer)
dim no1 as integer=100 Dim no2 as Integer=200 Dim Result as Integer Result=no1+no2 message1.text=result End Sub
Functions
<script .> Sub Page_Load() dim r as integer response.write("Required Value of Name Function type=" & NameMyfun()& "<br />" ) 'r=Myfun() 'response.write(r) response.write("Required Value of Return Function type=" & ReturnMyfun() & "<br />") response.write("Required Value of As Data type Function type=" & AstypeNameMyfun() & "<br />") End Sub First type of Function Function NameMyfun() NameMyfun=100 end Function Second type of Function Function ReturnMyfun() return 200 end Function Third type of Function Function AstypeNameMyfun() as integer return 400 end Function </script>
Call by value
Sub incr(byval number as integer) number=number+1 end sub Sub Page_Load() dim A as integer A=100 incr(A) Message.text=A End sub
Sub incr(byval number as integer) number=number+1 Message.text=number end sub Sub Page_Load() dim A as integer A=100 incr(A) End sub
Call By Reference
Sub incr(byref number as integer) number=number+1 Message.text=number End sub Sub Page_Load() Dim A as integer A=100 incr(A) Message.text=A End sub
Variable Scope
1. Scope refers to the area in which a variable is used. 2. Performance
Local variable:
Sub Procedure_Number1 () Dim Different As string Different=Hello Im Variable one End Sub Sub Procedure_Number2 () Dim different as String Different=Hello I m variable two End Sub Sub Procedure_Number3 () Dim Difference as String Different=Hello I m variable Three End Sub
Global Variable:
<script language=vb runat=server> dim Global as string//this is accessible any where.