Class 43 (Computer Programming)

Add to Favourites
Post to:

Description
VB.NET language, declaring variable


Type: ppt

Discussion
Presentation Transcript Presentation Transcript

Online Teaching Initiative by Dr. C.V. Raman University : Online Teaching Initiative by Dr. C.V. Raman University Topic: VB.NET language Course : DCA Module : Programming in Visual Basic .NET Slide no. : 1 Session: 43/52

Learning Objectives : Learning Objectives After attending this session students will be able to: Understand VB.NET language. Understand how to declare a variable. Understand constants and arrays. Understand types of arrays. Understand functions and passing variable. Understand how to return value from function. Understand conditional & loop statement,msgbox and inputbox. Topic: VB.NET language Course : DCA Module : Programming in Visual Basic .NET Slide no. : 2 Session: 43/52

VB.net language : VB.net language Visual Basic .NET  ( VB.NET ) is a multi-paradigm, high level programming language, implemented on the .NET Framework. Microsoft launched VB.NET in 2002 as the successor to its original Visual Basic language. Along with Visual C#, it is one of the two main languages targeting the .NET framework. Microsoft currently supplies two main editions of IDE for developing in VB.NET:  Course : DCA Module : Programming in Visual Basic .NET Slide no. : 3 Session: 43/52 Topic: VB.NET language

VB.net language : VB.net language Microsoft Visual Studio 2013, which is commercial software and Visual Studio Express Edition 2013, which is free of charge. The command-line compiler, VBC.EXE, is installed as part of the freeware .NET Framework SDK. Mono also includes a command-line VB.NET compiler. Course : DCA Module : Programming in Visual Basic .NET Slide no. : 4 Session: 43/52 Topic: VB.NET language

VB.net language : VB.net language VB.NET has the following syntax: Statements can terminate with keywords such as "End If", instead of using "{}"s to group statements. Statements can also be terminated with a new line, instead of semicolons. Variables are both assigned and compared using an equals sign. Round brackets are used with arrays, both to declare them and to get a value at a given index in one of them. Course : DCA Module : Programming in Visual Basic .NET Slide no. : 5 Session: 43/52 Topic: VB.NET language

VB.net language : VB.net language Course : DCA Module : Programming in Visual Basic .NET Slide no. : 6 Session: 43/52 The following is a very simple VB.NET program, a version of the classic "Hello world" example created as a console application: Module Module1 Sub Main() Console.WriteLine("Hello, world!") End Sub End Module Topic: VB.NET language

VB.net language : VB.net language Visual Basic .NET, the next generation of the Visual Basic language, is a fast and easy way to create .NET-based applications, including XML Web services and Web applications. Visual Basic .NET has many new and improved features that make it a powerful object-oriented programming language, including inheritance, interfaces, and overloading. Course : DCA Module : Programming in Visual Basic .NET Slide no. : 7 Session: 43/52 Topic: VB.NET language

VB.net language : VB.net language Other new language features include free threading and structured exception handling. Visual Basic .NET also fully integrates the .NET Framework and the Common Language Runtime, which provide language interoperability, garbage collection, enhanced security, and improved versioning support. Course : DCA Module : Programming in Visual Basic .NET Slide no. : 8 Session: 43/52 Topic: VB.NET language

VB.net language : VB.net language Module Module1 This is a module definition, a division of code similar to a class, although modules can contain classes. Modules serve as containers of code that can be referenced from other parts of a program. Sub Main() This is the entry point where the program begins execution . Sub  is an abbreviation of "subroutine." Course : DCA Module : Programming in Visual Basic .NET Slide no. : 9 Session: 43/52 Topic: VB.NET language

VB.net language : VB.net language Console.WriteLine("Hello, world!") This line performs the actual task of writing the output.  Console  is a system object, representing a command-line interface and granting programmatic access to the operating system's standard streams. The program calls the  Console  method  WriteLine which causes the string passed to it to be displayed on the console. Another common method is using MsgBox (a Message Box ). Course : DCA Module : Programming in Visual Basic .NET Slide no. : 10 Session: 43/52 Topic: VB.NET language

Declaring variables : Declaring variables You declare a variable to specify its name and characteristics. The declaration statement for variables is the Dim Statement (Visual Basic). Its location and contents determine the variable's characteristics. For variable naming rules and considerations, see Declared Element Names (Visual Basic). Course : DCA Module : Programming in Visual Basic .NET Slide no. : 11 Session: 43/52 Topic: VB.NET language

Declaring variables : Declaring variables Option Explicit Even before VB.NET, programming gurus have been telling us that it's a best practice to make sure that you declare all of your variables in a program. In VB6, you could get the compiler to help you by adding the Option Explicit statement to flag undeclared variables. But in VB.NET, Option Explicit is now the default. (Actually, it's Option Explicit On, but if you just enter Option Explicit, the IDE enters the word 'On' for you.) If you use the 'Off' parameter, however, you get this result: Course : DCA Module : Programming in Visual Basic .NET Slide no. : 12 Session: 43/52 Topic: VB.NET language

Declaring variables : Declaring variables Option Explicit Off ... Console.WriteLine("var1 is {0}", VarType(var1)) Which gives you ... var1 is Object Initialization and Dim In VB.NET, variables can be declared and initialized on a single line ... Dim myVar As String = "This is a string." Course : DCA Module : Programming in Visual Basic .NET Slide no. : 13 Session: 43/52 Topic: VB.NET language

Declaring variables : Declaring variables Course : DCA Module : Programming in Visual Basic .NET Slide no. : 14 Session: 43/52 Syntax of declaring variable : Dim< var_name > As Dim a As Integer Topic: VB.NET language

Constants : Constants The  constants  refer to fixed values that the program may not alter during its execution. These fixed values are also called literals. Constants can be of any of the basic data types like an integer constant, a floating constant, a character constant, or a string literal. There are also enumeration constants as well. The constants are treated just like regular variables except that their values cannot be modified after their definition. Course : DCA Module : Programming in Visual Basic .NET Slide no. : 15 Session: 43/52 Topic: VB.NET language

Constants : Constants Declaring Constants In VB.Net, constants are declared using the  Const  statement. The Const statement is used at module, class, structure, procedure, or block level for use in place of literal values. The syntax for the Const statement is: [ < attributelist > ] [ accessmodifier ] [ Shadows ] Const constantlist Const ConstantName as DataType Course : DCA Module : Programming in Visual Basic .NET Slide no. : 16 Session: 43/52 Topic: VB.NET language

Arrays : Arrays An array is a set of values that are logically related to each other, such as the number of students in each grade in a grammar school. By using an array, you can refer to these related values by the same name, and use a number that’s called an index or subscript to tell them apart. The individual values are called the elements of the array. They’re contiguous from index 0 through the highest index value. Course : DCA Module : Programming in Visual Basic .NET Slide no. : 17 Session: 43/52 Topic: VB.NET language

Arrays : Arrays Arrays  are low-level collections of elements. One element is stored after another in a linear way. Arrays often provide the best performance for certain requirements. Other collections are implemented with internal arrays. Example There are many syntax forms we use to declare one-dimensional arrays in the VB.NET language. To start, this example creates an array by specifying in the maximum index in the first statement. Course : DCA Module : Programming in Visual Basic .NET Slide no. : 18 Session: 43/52 Topic: VB.NET language

Arrays : Arrays Note: In VB.NET we must specify the maximum index, not the actual array element count or length. Course : DCA Module : Programming in Visual Basic .NET Slide no. : 19 Session: 43/52 Topic: VB.NET language

Arrays : Arrays Program that uses array: VB.NET (based on.NET 4.5) Module Module1 Sub Main() ' Create an array. Dim array(2) As Integer array(0) = 100 array(1) = 10 array(2) = 1 Course : DCA Module : Programming in Visual Basic .NET Slide no. : 20 Session: 43/52 Topic: VB.NET language

Arrays : Arrays Course : DCA Module : Programming in Visual Basic .NET Slide no. : 21 Session: 43/52 For Each element As Integer In array Console.WriteLine(element) Next End Sub End Module Output 100 10 1 Topic: VB.NET language

Types of array : Types of array Course : DCA Module : Programming in Visual Basic .NET Slide no. : 22 Session: 43/52 One dimensional is like: a(2) Multidimensional is like : a(2)(3) An array stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type. All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element. Topic: VB.NET language

Functions : Functions A procedure is a group of statements that together perform a task when called. After the procedure is executed, the control returns to the statement calling the procedure. VB.Net has two types of procedures: Functions Sub procedures or Subs Functions return a value, whereas Subs do not return a value. Course : DCA Module : Programming in Visual Basic .NET Slide no. : 23 Session: 43/52 Topic: VB.NET language

Functions : Functions Defining a Function The Function statement is used to declare the name, parameter and the body of a function. The syntax for the Function statement is: [Modifiers] Function FunctionName [( ParameterList )] As ReturnType [Statements] End Function Course : DCA Module : Programming in Visual Basic .NET Slide no. : 24 Session: 43/52 Topic: VB.NET language

Functions : Functions Where, Modifiers : specify the access level of the function; possible values are: Public, Private, Protected, Friend, Protected Friend and information regarding overloading, overriding, sharing, and shadowing. FunctionName : indicates the name of the function ParameterList : specifies the list of the parameters ReturnType : specifies the data type of the variable the function returns Course : DCA Module : Programming in Visual Basic .NET Slide no. : 25 Session: 43/52 Topic: VB.NET language

Functions : Functions Course : DCA Module : Programming in Visual Basic .NET Slide no. : 26 Session: 43/52 Example Following code snippet shows a function  FindMax  that takes two integer values and returns the larger of the two. Function FindMax(ByVal num1 As Integer, ByVal num2 As Integer) As Integer ' local variable declaration */ Topic: VB.NET language

Functions : Functions Dim result As Integer If (num1 > num2) Then result = num1 Else result = num2 End If FindMax = result End Function Course : DCA Module : Programming in Visual Basic .NET Slide no. : 27 Session: 43/52 Topic: VB.NET language

Passing variable : Passing variable Call by value Call by reference Call by value: When passing the arguments by value a copy of actual argument is passed and thus any change made in the formal argument never affects the actual argument. Byval keyword is used to indicate pass by value. Course : DCA Module : Programming in Visual Basic .NET Slide no. : 28 Session: 43/52 Topic: VB.NET language

Passing variable : Passing variable Course : DCA Module : Programming in Visual Basic .NET Slide no. : 29 Session: 42/52 Call by reference: When argument is passed by reference its actual memory position gets passed and thus any change made in the formal argument always affects the actual argument. The default argument passing mechanism in vb.net is Byval. Topic: VB.NET language

Returning value from function : Returning value from function In VB.Net, a function can return a value to the calling code in two ways: By using the return statement By assigning the value to the function name The following example demonstrates using the  FindMax  function: Module myfunctions Function FindMax( ByVal num1 As Integer, ByVal num2 As Integer) As Integer ' local variable declaration */ Course : DCA Module : Programming in Visual Basic .NET Slide no. : 30 Session: 43/52 Topic: VB.NET language

Returning value from function : Returning value from function Course : DCA Module : Programming in Visual Basic .NET Slide no. : 31 Session: 43/52 Dim result As Integer If (num1 > num2) Then result = num1 Else result = num2 End If FindMax = result End Function Topic: VB.NET language

Returning value from function : Returning value from function Sub Main() Dim a As Integer = 100 Dim b As Integer = 200 Dim res As Integer res = FindMax (a, b) Console.WriteLine("Max value is : {0}", res) Console.ReadLine () End Sub End Module Course : DCA Module : Programming in Visual Basic .NET Slide no. : 32 Session: 43/52 Topic: VB.NET language

Returning value from function : Returning value from function Course : DCA Module : Programming in Visual Basic .NET Slide no. : 33 Session: 43/52 When the above code is compiled and executed, it produces the following result: Max value is : 200 Topic: VB.NET language

Conditional statement : Conditional statement Course : DCA Module : Programming in Visual Basic .NET Slide no. : 34 Session: 43/52 An  If  statement can be followed by an optional  Else  statement, which executes when the Boolean expression is false. Syntax: The syntax of an If...Then... Else statement in VB.Net is as follows: Topic: VB.NET language

Conditional statement : Conditional statement Course : DCA Module : Programming in Visual Basic .NET Slide no. : 35 Session: 43/52 If( boolean_expression )Then 'statement(s) will execute if the Boolean expression is true Else 'statement(s) will execute if the Boolean expression is false End If Topic: VB.NET language

Conditional statement : Conditional statement The If...Else If...Else Statement An  If  statement can be followed by an optional  Else if...Else  statement, which is very useful to test various conditions using single If...Else If statement. If( boolean_expression 1)Then ' Executes when the boolean expression 1 is true ElseIf ( boolean_expression 2)Then ' Executes when the boolean expression 2 is true Course : DCA Module : Programming in Visual Basic .NET Slide no. : 36 Session: 43/52 Topic: VB.NET language

Conditional statement : Conditional statement ElseIf ( boolean_expression 3)Then ' Executes when the boolean expression 3 is true Else ' executes when the none of the above condition is true End If Course : DCA Module : Programming in Visual Basic .NET Slide no. : 37 Session: 43/52 Topic: VB.NET language

Loop statement : Loop statement Course : DCA Module : Programming in Visual Basic .NET Slide no. : 38 Session: 43/52 There may be a situation when you need to execute a block of code several number of times. In general, statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on. Programming languages provide various control structures that allow for more complicated execution paths. A loop statement allows us to execute a statement or group of statements multiple times and following is the general form of a loop statement in most of the programming languages: Topic: VB.NET language

Loop statement : Loop statement Course : DCA Module : Programming in Visual Basic .NET Slide no. : 39 VB.Net provides following types of loops to handle looping requirements. Click the following links to check their details. Session: 43/52 Loop Type Description Do Loop It repeats the enclosed block of statements while a Boolean condition is True or until the condition becomes True. It could be terminated at any time with the Exit Do statement. For...Next It repeats a group of statements a specified number of times and a loop index counts the number of loop iterations as the loop executes. Topic: VB.NET language

Loop statement : Loop statement Course : DCA Module : Programming in Visual Basic .NET Slide no. : 40 Session: 43/52 For Each...Next It repeats a group of statements for each element in a collection. This loop is used for accessing and manipulating all elements in an array or a VB.Net collection. While... End While It executes a series of statements as long as a given condition is True. With... End With It is not exactly a looping construct. It executes a series of statements that repeatedly refer to a single object or structure. Nested loops You can use one or more loops inside any another While, For or Do loop. Topic: VB.NET language

Msgbox and Inputbox : Msgbox and Inputbox A MsgBox() and InputBox() Overview You use input boxes and message boxes when you need to ask the user questions or display error messages and advice to the user. As stated earlier, the form's controls don't often work well for such user dialog boxes. Topic: VB.NET language Session: 43/52 Course : DCA Module : Programming in Visual Basic .NET Slide no. : 41

Msgbox and Inputbox : Msgbox and Inputbox A  message box  is a dialog box you display to give the user information. An  input box  is a dialog box you display to ask the user questions. A message box is typically used to display a short single sentence message to the user. Program execution continues after the user acknowledges the message box. An input box, on the other hand, can be thought of as a message box with a text box. An input box displays a prompt to the user and obtains text input from the user. Topic: VB.NET language Session: 43/52 Course : DCA Module : Programming in Visual Basic .NET Slide no. : 42

Msgbox : Msgbox Course : DCA Module : Programming in Visual Basic .NET Slide no. : 43 Topic: VB.NET language Session: 43/52 A message box can tell the user what to do.

Inputbox : Inputbox Course : DCA Module : Programming in Visual Basic .NET Slide no. : 44 Topic: VB.NET language Session: 43/52  Input boxes get user information.

Learning Outcomes : Learning Outcomes VB.NET language. How to declare a variable. What is constants and arrays. Types of arrays. What is functions and how to pass a variable. How to return value from function. Conditional & loop statement,msgbox and inputbox. Course : DCA Module : Programming in Visual Basic .NET Slide no. : 45 Session: 43/52 Topic: VB.NET language

THANKS : THANKS Class presented by NOSHABA RUHI For any information Contact us at contentgroup@aisect.org Course : DCA Module : Programming in Visual Basic .NET Slide no. : 46 Session: 43/52

7 Members Recommend
22 Followers

Your Facebook Friends on WizIQ