vb script – a fundamental introduction

Upload: apextgi

Post on 02-Jun-2018

237 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/10/2019 VB Script A Fundamental Introduction

    1/14

    By

    Apex TG India Pvt Ltd

    http://www.apextgi.in/http://www.apextgi.in/
  • 8/10/2019 VB Script A Fundamental Introduction

    2/14

    What is VBScript?

    An interpreted programming language primarily used to write relatively

    short programs that access operating system and application objects. Suchlanguages are referred to as "scripting" languages. The VBScript

    commands are a subset of the more complete, compiled Visual Basic

    language.

  • 8/10/2019 VB Script A Fundamental Introduction

    3/14

    Why is VBScript used ?VBScript is an easy to learn language and thus is a good

    candidate for a brief introduction to programming

    VBScript! VB! and VBA "Visual Basic for Applications# arecollectively used more than any other language in the $orldso exposure to these languages is important

    VBScript can be used to automate tas%s in $eb pages! &S'xcel spreadsheets! &S Access (atabases! and other &Sapplications

    VBScript can be used to automate administrative tas%s suchas moving or copying )les! creating user accounts! etc

  • 8/10/2019 VB Script A Fundamental Introduction

    4/14

    What if I can already

    program?*nly about )ve labs $ill be used on the intro to

    programmingTa%e advantage of the opportunity to learn a ne$ language

    if VBScript is not a language you have used beforeTa%e advantage of the opportunity to practice your s%illsand perhaps pic% up some missing pieces in your%no$ledge of the language

    If you )nish a lab early! challenge yourself by learningfeatures of the language that are ne$ to you and notre+uired in the lab

    ,e $ill try to include a -challenge- tas% in each VBScriptlab

  • 8/10/2019 VB Script A Fundamental Introduction

    5/14

  • 8/10/2019 VB Script A Fundamental Introduction

    6/14

    HelloWorld.!s

    '****************

    ' HelloWorld.vbs

    '****************

    option explicit

    dim strGreetingstrGreeting = "Hello World"

    Wscript.echo(strGreeting)

  • 8/10/2019 VB Script A Fundamental Introduction

    7/14

  • 8/10/2019 VB Script A Fundamental Introduction

    8/14

    Varia!le #eclaration

    In most languages variables must be declared before they canbe used VBScript does not re+uire variable declaration bydefault It is a good idea to override this default or elsedebugging your programs $ill be much more diEcult Theprogram belo$! $hich calculates the total price due for thesale of /0 $idgets! can be found in the itec//0 folder .opy

    the program to your lab/ folder and execute it (oes theresult loo% right to youF

    '******************

    ' NoOpExplicit.vbs

    '******************

    rice = !.#$%ntit& =

    ot%l = rice * #%$ntit&

    Wscript.echo("he ot%l is " + ot%l)

  • 8/10/2019 VB Script A Fundamental Introduction

    9/14

    Varia!les #eclaration 6ere is the same program $ith variable declaration re+uired

    and implemented *nly t$o lines have been added *n the)rst line the -option explicit- statement turns on re+uiredvariable declaration The second line declares or-dimensions- the variables needed for this program using the

    (I& statement Add these )rst t$o lines to your $idgetprogram and execute it ,hat is dierent about the resultF

    Option Explicit

    ,im rice- #$%ntit&- ot%l

    rice = !.

    #$%ntit& =

    ot%l = rice * #%$ntit&

    Wscript.echo("he ot%l is " + ot%l)

  • 8/10/2019 VB Script A Fundamental Introduction

    10/14

  • 8/10/2019 VB Script A Fundamental Introduction

    11/14

    %ssignment Statements

    Assignment is the act of storing a value in a variable InVBScript the ; sign is used for assignment 'xamples:TotalPrice = Quantity x Price

    HoursPerWeek = 7 x 24

    Profit = Total Revenue Total Expenses

    Lasta!e = "#!it$"Some languages use :; for assignment to dierentiate

    bet$een an assignment statement and an e+uality 4oticethat the VBScript assignment statements belo$ are validbut do not represent e+ualities

    %increase &ounter 'y (&ounter = &ounter ) (

    %increase a custo!er accounts receiva'le 'alance

    %'y t$e a!ount of a ne* sale

    &usto!er+alance = &usto!er+alance )

    e*#ale,!ount

  • 8/10/2019 VB Script A Fundamental Introduction

    12/14

    %ssignment.!s

    '****************' ssignment.vbs'****************option explicit

    dim // = 0 * 1/ = 2 * / = / 3 1Wscript.echo(/)

    What is the final value of the variable X?

    Write and execute this program to find out if you are correct.

  • 8/10/2019 VB Script A Fundamental Introduction

    13/14

    &ath 'perations

    H exponentiation xcubed ; xH@

    addition x ; @ =

    2 subtraction x ; @ J / 1

    K &ultiplication x ; ? K @0/?0

    division x ; /< @ >

    M integer division x ; /@ M ? 1

    &od &odulo x ; /@ mod ?@

    The follo$ing math operations are available inVBScript:

  • 8/10/2019 VB Script A Fundamental Introduction

    14/14