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 हो जाएगा।

Wednesday, 21 January 2015

Common Type System and MSIL in .Net

Common Type System - CTS

Common Type System (CTS) describes a set of types that can be used in different .Net languages in common . That is , the Common Type System (CTS) ensure that objects written in different .Net languages can interact with each other. For Communicating between programs written in any .NET complaint language, the types have to be compatible on the basic level .
These types can be Value Types or Reference Types . The Value Types are passed by values and stored in the stack. The Reference Types are passed by references and stored in the heap. Common Type System (CTS) provides base set of Data Types which is responsible for cross language integration. The Common Language Runtime (CLR) can load and execute the source code written in any .Net language, only if the type is described in the Common Type System (CTS) .Most of the members defined by types in the .NET Framework Class Library (FCL) are Common Language Specification (CLS) compliant Types.



   Microsoft Intermediate Language - MSIL

MSIL stands for Microsoft Intermediate Language. We can call it as Intermediate Language (IL) or Common Intermediate Language (CIL). During the compile time , the compiler convert the source code into Microsoft Intermediate Language (MSIL) .Microsoft Intermediate Language (MSIL) is a CPU-independent set of instructions that can be efficiently converted to the native code. During the runtime the Common Language Runtime (CLR)'s Just In Time (JIT) compiler converts the Microsoft Intermediate Language (MSIL) code into native code to the Operating System.
When a compiler produces Microsoft Intermediate Language (MSIL), it also produces Metadata. The Microsoft Intermediate Language (MSIL) and Metadata are contained in a portable executable (PE) file . Microsoft Intermediate Language (MSIL) includes instructions for loading, storing, initializing, and calling methods on objects, as well as instructions for arithmetic and logical operations, control flow, direct memory access, exception handling, and other operations

Assembly in .Net

Microsoft .Net Assembly is a logical unit of code, that contains code which the Common Language Runtime (CLR) executes. It is the smallest unit of deployment of a .net application and it can be a .dll or an exe . Assembly is really a collection of types and resource information that are built to work together and form a logical unit of functionality. It include both executable application files that you can run directly from Windows without the need for any other programs (.exe files), and libraries (.dll files) for use by other applications.


                     Assemblies are the building blocks of .NET Framework applications. During the compile time Metadata is created with Microsoft Intermediate Language (MSIL) and stored in a file called Assembly Manifest . Both Metadata and Microsoft Intermediate Language (MSIL) together wrapped in a Portable Executable (PE) file. Assembly Manifest contains information about itself. This information is called Assembly Manifest, it contains information about the members, types, references and all the other data that the runtime needs for execution.



                  Every Assembly you create contains one or more program files and a Manifest. There are two types program files : Process Assemblies (EXE) and Library Assemblies (DLL). Each Assembly can have only one entry point (that is, DllMain, WinMain, or Main).
We can create two types of Assembly:
1. Private Assembly
2. Shared Assembly
                 A private Assembly is used only by a single application, and usually it is stored in that application's install directory. A shared Assembly is one that can be referenced by more than one application. If multiple applications need to access an Assembly, we should add the Assembly to the Global Assembly Cache (GAC). There is also a third and least known type of an assembly: Satellite Assembly . A Satellite Assembly contains only static objects like images and other non-executable files required by the application.
 


Monday, 19 January 2015

Intro about .net framework

hello students this is a post about a intro of framework.

.Net Architecture and .Net Framework basics:

  1. Common Language Runtime (CLR): The heart of the .Net Framework. It is also called the .Net runtime. It resides above the operating system and handles all .Net applications. It handles garbage collection, Code Access Security (CAS) etc.

    NET1.gif
     
  2. Microsoft Intermediate Language (MSIL) Code: When we compile our .Net code then it is not directly converted to native/binary code; it is first converted into intermediate code known as MSIL code which is then interpreted by the CLR. MSIL is independent of hardware and the operating system. Cross language relationships are possible since MSIL is the same for all .Net languages. MSIL is further converted into native code.

    NET2.gif
     
  3. Just in Time Compilers (JIT): It compiles IL code into native executable code (exe or dlls). Once code is converted to IL then it can be called again by JIT instead of recompiling that code.
  4. Framework class library: The .Net Framework provides a huge class library called FCL for common tasks. It contains thousands of classes to access Windows APIs and common functions like string manipulations, Data structures, stream, IO, thread, security etc.
  5. Common Language Specification (CLS): What makes a language to be .Net compliant? Answer is CLS. Microsoft has defined some specifications that each .Net language has to follow. For e.g.: no pointer, no multiple inheritances etc.
     
  6. Common Type System (CTS): CTS defines some basic data types that IL can understand. Each .Net compliant language should map its data types to these standard data types. This makes it possible for two .Net compliant languages to communicate by passing/receiving parameters to and from each other. For example CTS defines Int32 for C# int and VB integer data types.
     
  7. The .Net Framework: Is a combination of CLR, FCL, ADO.Net and XML classes, Web/Window applications and Web services.

    NET3.gif


Thursday, 15 January 2015

my welcome post

Good Morning to all

this is my blog dedicate to all the students who wants to learn n share information 
about programming .

so join my blog and ask or share anything u face difficult about any programming language


so come and explore magic of programming world.