Tuesday, 22 August 2017

Examples of Implicit,Explicit Declarations

As we seen in earlier we have two types of declarations in VB script namely implicit,explicit.
In this post we will see some basic examples for those declaration types.

Implicit:

Ex 1: write the code in notepad or any editor

dim a
a=20
msgbox a

Now I am saving the file with the name example1.vbs.




The file will be saved like above.Now to run the file double click on the file then the result will be displayed in a dialogue box as shown below


In this way implicit way of declaration will work.

Implicit :

Now will see about the explicit declaration.As I said earlier in Explicit declaration variables are declared first before using it and we use “Option Explicit “in the first line of  the script. 

Ex 2: write the code in notepad or any editor

option explicit
dim a
a=20
msgbox a

When you try to run the file it will give output as 20 same like the above example.but when we use another variable b without declaring first then it will show an error

Ex 3:
option explicit
dim a,b
a=20
msgbox a

The above code wont show any error as we declared the variable b.now we will do some changes to the code

Ex 4:
option explicit
dim a
a=20
b=30
msgbox a

When you try to run the file it will give an error stating that Variable is undefined.Because this is explicit declaration and we are using variable b without declaring it first .

We will see about dim,msgbox,typename..etc in next post.

No comments:

Post a Comment