Thursday, June 13, 2013

VBS : Tutorial 5 (Funtions )

VBS : Tutorial 5 (Funtions )

Functions

 

Example 1 :         Syntax

 

Call func_name

Function Func_name()

                msgbox 1+2

end function

 

 

Example 2:          Returning Value

msgbox func_name

 

Function func_name()

                func_name=1+2

end function

 

Example :Calculate age of a person

 

Dim sVar1

sVar1=inputbox("e.g.  1947 "," Please Enter Years")

msgbox fn(sVar1)

 

Function fn(x)

                fn= year(date)-x

End Function

 

 

Example 3:          Sub-routine

Call func_name

 

Sub Func_name()

                msgbox 1+2

end Sub

 

Example 4:          Function vs Sub

msgbox func_name

 

Sub Func_name()

                func_name = 1+2

end Sub

 

Example 5 :         Sending Values to Functions

 

Dim a,b

a=3

b=4

 

msgbox Add(a,b)

 

Function Add(x,y)

                Add=x+y

End Function

 

 

Example 6:          Sending arrays to function

 

Dim a(9)

Call Add(a)

 

Function Add(x)

                msgbox isarray(x)           

End Function

 

Example 7 :         Catching the Return Value  and Flags in framework

 

Dim a(9),bVar

bVar = Add(a)

msgbox  bVar

 

Function Add(x)

                Add=isarray(x) 

End Function

 

Example 8:          Function accept multiple arguements but returns only 1 value

 

Example 9:          Variables are Global Unless declared ,-----NOT DECLARED i.e., PASS BY REF

msgbox a

call fn

call fn2()

msgbox a

 

Function fn()

                a=5

                msgbox a

end function

 

Function fn2()

                msgbox a

                a="dfsdf"

                msgbox a

end function

 

Example 10: ---             DECLARED

msgbox a

call fn

call fn2()

msgbox a

 

Function fn()

                a=5

                msgbox a

end function

 

Function fn2()

                Dim a

                msgbox a

                a="dfsdf"

                msgbox a

end function

 

Example

dim a

a=10.5

msgbox a

call fn

call fn2()

msgbox a

 

Function fn()

dim a

                a=5

                msgbox a

end function

 

Function fn2()

dim a

                msgbox a

                a="piyush"

                msgbox a

end function

 

Example 9 :         Pass By Value and pass By Ref  , By Default --> ByRef

 

 

-------------------------------------------

By Default variables are passed ByRef

------------------------------------------

Ex Byval

 

Option Explicit

Dim strTest

 

strTest = "FOO"

RunTest (strTest)

msgbox  strTest

 

Sub RunTest(ByVal strIn)

                WScript.Echo strIn

                strIn = "BAR"

                msgbox strIn

End Sub

 

OUTPUT:

FOO

 BAR

 FOO

------------------------------------------------------

The default is ByRef. This stands for "By Reference"

any changes that you make inside the Sub/Function will be seen outside the Sub/Function. Here is a demonstration:

 

Option Explicit

 

Dim strTest

 

strTest = "FOO"

RunTest (strTest )

msgbox  strTest

 

Sub RunTest(strTest)

                WScript.Echo strTest

                strTest = "BAR"

                msgbox strTest

End Sub

 

OUTPUT: >>

FOO

 BAR

 BAR

 

Example 1:  Redim and preserve

Redim a(1,2)

Redim a(2,3)

 

Example 2

Redim preserve  a(1,2)

Redim a(2,3)

 

 

 

 

 

 

 

VBS :Tutorial 4 (Loops )

VBS :Tutorial 4 (Loops )

 
Loop Statements

 

1.      ·      Do...Loop: Loops while or until a condition is True.

2.      ·      While...Wend: Loops while a condition is True.

3.      ·      For...Next: Uses a counter to run statements a specified number of times.

4.      ·      For Each...Next: Repeats a group of statements for each item in a collection or each element of an array.

5.      exit do

6.      exit while

7.      exit for

 

---------------------------------------------------------------------------------Do While

 

1) Do While  condition

    Statements

    -----------                  

    -----------

    Loop

 

Example :

Dim sStr

sStr="Deepak"

i=1

 

Do while mid(sStr,i,1) <> ""

               msgbox mid(sStr,i,1)

               i=i+1

loop

 

 

2) Do

    Statements

    -----------                  

    -----------

    Loop While  condition

 

Example :

Dim sStr

sStr="Deepak"

i=1

 

Do

               msgbox mid(sStr,i,1)

               i=i+1

 

loop while mid(sStr,i,1) <> ""

 

-------------------------------------------------------------------------------------Do Until

 

3) Do Until  condition'-----------       False =loop , True=Exit

    Statements

    -----------                  

    -----------

    Loop

 

Example :

Dim sStr

sStr="Deepak"

i=1

 

Do until mid(sStr,i,1) = ""

               msgbox mid(sStr,i,1)

               i=i+1

 

loop

 

4) Do

    Statements

    -----------                  

    -----------

    Loop Until condition

 

Example :

Dim sStr

sStr="Deepak"

i=1

 

Do

               msgbox mid(sStr,i,1)

               i=i+1

 

loop until mid(sStr,i,1) = ""

 

--------------------------------------------------------------------------------------While...Wend

5) While  condition   'True= loop ,false =Exit

            Statements

            -----------          

            -----------               

   Wend

 

Example :

Dim sStr

sStr="Deepak"

i=1

 

while  mid(sStr,i,1) <> ""

               msgbox mid(sStr,i,1)

               i=i+1

 

wend

-------------------------------------------------------------------------------------For

6) For start to end [Step step]

       statements

Next

Example :

Dim sStr

sStr="Deepak"

for i=1 to len(sStr) step 1

            msgbox mid(sStr,i,1)

next

 

-----------------------------------------------------------------------------------Excercise

 

1.   Display Even numbers up to n?

2.   Display Natural numbers in reverse order up to n?

3.   Reverse a string

4.   Display String in the form of a right angled triangle ?

5.       Matrix 2D,Accept and Display

6.       Transpose--

 

-------------------------------------------------------------------

 

Dim aMat(2,2), row_count , col_count  , sRes

 

aMat(0,0)=1

aMat(0,1)=1

aMat(0,2)=1

 

aMat(1,0)=2

aMat(1,1)=2

aMat(1,2)=2

 

aMat(2,0)=3

aMat(2,1)=3

aMat(2,2)=3

 

for row_count=0 to ubound(aMat,1) step 1

 

 

        for col_count =0 to ubound(aMat,2) step 1

       

                                       

                                        sRes1=sRes1& " "& aMat(row_count ,col_count)

                                        sRes2=sRes2& " "& aMat(col_count  ,row_count  )

         

        next

 

  sRes1=sRes1&vblf

  sRes2=sRes2&vblf

 

 

next

 

 

msgbox sRes1

msgbox sRes