Monday, 9 February 2015

forcing variable declaration in vb.net

Option Explicit statement ensures whether the compiler requires all variables to be explicitly declared or not before it use in the program.
Option Explicit [On Off]

The Option Explicit has two modes. On and Off mode. If Option Explicit mode in ON , you have to declare all the variable before you use it in the program . If not , it will generate a compile-time error whenever a variable that has not been declared is encountered .If the Option Explicit mode is OFF , Vb.Net automatically create a variable whenever it sees a variable without proper declaration.

By default the Option Explicit is On

With the Option Explicit On , you can reduce the possible errors that result from misspelled variable names. Because in Option Explicit On mode you have to declare each variable in the program for storing data.
Take a look at the following programs, it will give you a clear picture of Option Explicit.
The following program is a normal vb.net program , so the default mode of Option Explicit On is using. The default is Option Explicit On , so we do not need to put it in the source code.
VB.NET Source Code

  Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click
        Dim someVariable As String
        someVariable = "Option Explicit ON"
        MsgBox(someVariable)
    End Sub
End Class

Saturday, 7 February 2015

Scope And Lifetime of a variable

Scope and Life Time of Variable: 

      Programming मे variable का scope variable के use होने की limit बताता है। कोई भी variable कहाँ तक प्रयोग किया जा सकता है यह scope के द्वारा ही define होता है। इसे visibility भी कहते हैं। Visual Basic.Net मे चार प्रकार के scope पाये जाते हैं।

1.     Block Scope
2.     Procedural Scope
3.     Module Scope
4.     Namespace Scope(Public Scope)

      I.        Block Scope: जब कोई variable किसी codes के block के अंदर declare किए जाते हैं हो उस variable को उस block के बाहर नहीं किया जा सकता है। इसमे निम्न statements होती हैं।

a.     Do and Loop
b.    For [Each] and Next
c.     If and End If
d.    Select and End Select
e.     Try and End Try
f.      While and End While
g.    With and End With
If n < 1291 Then
    Dim cube As Integer
    cube = n ^ 3
End If



     II.        Procedural Scope(Local Scope) : यदि कोई भी variable किसी भी procedure के अंदर declare किया जाता है तब इस स्कोप को procedural scope कहते है। इस scope मे declare किए गए variable को केवल declare किए गए procedure मे use किया जा सकता है। यह variable उस procedure के बाहर कहीं भी प्रयोग नहीं किया जा सकता है।
Program 1: Printing Sum of Even from 1 to 100
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim i As Integer
        Dim sum As Integer
        For i = 0 To 100 Step 2
            sum = sum + i
        Next
        MsgBox("Sum of All Even No = " & sum)
    End Sub
End Class

इस example मे I और sum दोनों procedural scope मे हैं।

2. Module Scope: यह scope variable को पूरे code module मे उसे करने की सुविधा देता है। इस scope मे variable declare करने के लिए dim या private statement को code window मे public class form के नीचे लिखते है। इस scope मे declare किए गए variables को सभी procedures मे उसे किया जा सकता है। जो उस code module मे available हैं। इस scope को module scope कहते है।

Program 2: Module Scope

Public Class Form1
    Dim x As Integer
    Sub Exp1()
        Dim y As Integer
        x = 10
        y = 20
        MsgBox(y)
    End Sub
    Sub Exp2()
        Dim y As String
        y = "VB.NET"
        MsgBox(y)
        MsgBox(x)
    End Sub
    Sub Exp3()
        MsgBox(x)
    End Sub
End Class
इस example मे x module scope का variable है जिसके कारण इसे Exp1, Exp2 और Exp3 तीनों sub procedures मे use किया गया है। जबकि y local scope का variable है।

3. Namespace Scope: यह scope variable को पूरे project मे use करने की सुविधा देता है। इस scope मे variable को declare करने के लिए public statement को code window मे public class form के नीचे लिखा दिया जाता है। इसमे dim या private के स्थान पर Public का use किया जाता है। यहाँ पर declare किया गया variable project मे कंही भी use किया जा सकता है। इसे use करते समय code module का नाम लिखना पड़ता है। जैसे –

Program 3: Public Module- इस program मे दो form use होंगे:

Form1.vb

Public Class Form1
    Public x As Integer
  
  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        x = 100
        Form2.Show()
    End Sub
End Class
Form2.vb

Public Class Form2
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        MsgBox(Form1.x)
    End Sub
End Class
Output:

Life Time of Variables:


                सभी programming languages की तरह ही Visual Basic.Net language मे भी सभी variables का life time होता है। life time वह समय जब तक की variable मे store की गई value available रहती है। यदि किसी variable को local या procedural scope मे declare किया गया है तो उसे केवल उसी procedure मे उसे किया जा सकता है। procedure के use के बाद वह variable और उसमे store value दोनों destroy हो जाते है। procedure के दुबारा call होने पर variables फिर से create होंगे । Module Scope मे declare किए गए variables का life time उस code module के रन होने तक होता है। module के end होने पर सभी variables destroy को जाएंगे। और memory system को return हो जाएगी। Public Scope मे declare किए गए variable project के run होने तक memory मे रहता है। यदि project का कोई भी component run होगा तो variable को use किया जा सकता है। project के close होने पर यह destroy हो जाएगा।