Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to VB.NET 2005 Dr. McDaniel IDS4704 Spring 2005.

Similar presentations


Presentation on theme: "Introduction to VB.NET 2005 Dr. McDaniel IDS4704 Spring 2005."— Presentation transcript:

1 Introduction to VB.NET 2005 Dr. McDaniel IDS4704 Spring 2005

2 What this lecture covers Types of projects VB can create Types of projects VB can create VB Modules and Module Access VB Modules and Module Access VB Subroutines VB Subroutines Passing Arguments by Value Passing Arguments by Value Passing Arguments by Reference Passing Arguments by Reference Simple output using the console Simple output using the console VB Functions VB Functions The VB Dim Statement (variables) The VB Dim Statement (variables)

3 What this lecture covers (cont) Constants Constants Common variable types Common variable types Variable scope Variable scope If/Then/Else control structure If/Then/Else control structure While/End While control structure While/End While control structure For loops For loops Select Case control structure Select Case control structure

4 VB.NET: What Can it Do? Create console applications Create console applications Create Windows GUI applications Create Windows GUI applications Create dynamic, server-side web applications Create dynamic, server-side web applications Create custom applications for Microsoft Office (VBA) Create custom applications for Microsoft Office (VBA) Create applications for mobile devices Create applications for mobile devices

5

6 How do you get started? Open up.NET 2005, and choose Visual Basic as your language of choice. Open up.NET 2005, and choose Visual Basic as your language of choice. For an application: For an application: –Click on File, New Project –Choose the type of project you wish to design For a website: For a website: –Click on File, New Web Site

7 Console Application: First Steps File, New Project, Console Application File, New Project, Console Application A Module is a wrapper than contains your visual basic code A Module is a wrapper than contains your visual basic code Module Module1 Sub Main() End Sub End Module

8 Comments To use a comment, include a ‘ To use a comment, include a ‘ Ex: ‘this is a comment for VB Ex: ‘this is a comment for VB

9 A simple VB application What does the sub keyword do? What does the sub keyword do? Subroutines: Subroutines: –Don’t return a value –Useful for printing out information Module Module1 Sub Main() Console.Out.WriteLine("Hi! I'm a stupid console program.") Console.In.ReadLine() End Sub End Module

10 Building and running What does “building” mean? What does “building” mean? To compile and run, press CTRL+F5 To compile and run, press CTRL+F5 Or, choose Build from menu then click Green Arrow, which starts debugging Or, choose Build from menu then click Green Arrow, which starts debugging

11 Output

12 Adding More Subs

13 The New Output

14 How it looks in the editor

15 Passing Arguments by Value Module Module2 Sub PrintInfo(ByVal Name As String, ByVal IQ As Integer) Console.Out.WriteLine("The name is " & Name & " and the IQ is " & IQ) End Sub End Module To pass arguments by value, include them in between the parentheses using the ByVal keyword and specify their types using the AS keyword Notice this subroutine is in a different module. How do we call it?

16

17 Output

18 Declaring Variables DIM: Declare in Memory DIM: Declare in Memory Used to declare a variable, which can hold data that changes. Used to declare a variable, which can hold data that changes. Example dim statements: Example dim statements: –Dim Name As String (“Joe Blow”) –Dim Age As Integer (“21”) –Dim Radius As Double (“1.2323”)

19 Rules for Variable Names Maximum 255 characters Must begin with a letter Do not use spaces Do not use VB keywords No punctuation except the underscore (_) character

20 Constants Constants are special variables which contain only one value, which cannot change during program execution Constants are special variables which contain only one value, which cannot change during program execution To use a constant, it is a good idea to name it in all CAPS so you can easily find and reference your constant To use a constant, it is a good idea to name it in all CAPS so you can easily find and reference your constant Ex: Const PI As Integer = 3.14 Ex: Const PI As Integer = 3.14 Ex2: Const PROFESSOR As String = “Rudy” Ex2: Const PROFESSOR As String = “Rudy”

21 Scope Where you define your variables determines where these variables will be accessible within your program Where you define your variables determines where these variables will be accessible within your program For example, when you use DIM inside a subroutine or function, that variable is only accessible within your function or subroutine For example, when you use DIM inside a subroutine or function, that variable is only accessible within your function or subroutine Play around with declaring variables to see where they are usable Play around with declaring variables to see where they are usable

22 Passing Arguments by Reference To pass arguments by reference, include them in between the parentheses using the ByRef keyword and specify their types using the AS keyword Passing by reference will modify the original variable in the calling routine

23

24 Output

25 Functions Another way to modify a variable without passing arguments by reference is to use a function. Another way to modify a variable without passing arguments by reference is to use a function. The syntax for declaring a function is very similar to the subroutine syntax. The syntax for declaring a function is very similar to the subroutine syntax.

26 An example function The main difference is the additional AS that occurs after the list of parameters. This defines the type of variable that is RETURNED by the function.

27 More Functions

28 Calling a Function

29 Output

30 Program Control: If Else Statement If (condition) Then If (condition) Then –Do Stuff Else If (condition) Then Else If (condition) Then –Do Stuff Else Else –Do Stuff End If End If

31 Example Code for If/Else

32 Calling the Code

33 Output

34 Program Control: For Loops Use statement like this: Use statement like this: –For i As Integer = [insert low number] To [insert high number]  Do stuff inside the loop –Next

35 Program Control: While Loops Very similar, except using a While and End While syntax Very similar, except using a While and End While syntax

36 Output

37 Select/Case Select [ Case ] testexpression [ Case expressionlist [ statements ] ] [ Case Else [ elsestatements ] ] End Select

38 Sample Select/Case Code Dim Number As Integer = 8 '... Select Case Number ' Evaluate Number. Case 1 To 5 ' Number between 1 and 5, inclusive. Debug.WriteLine("Between 1 and 5") ' The following is the only Case clause that evaluates to True. Case 6, 7, 8 ' Number between 6 and 8. Debug.WriteLine("Between 6 and 8") Case 9 To 10 ' Number is 9 or 10. Debug.WriteLine("Greater than 8") Case Else ' Other values. Debug.WriteLine("Not between 1 and 10") End Select

39 Let’s Write Some Subroutines! Work in groups of 2 or 3 to write the following subroutines: Work in groups of 2 or 3 to write the following subroutines: –Create a sub to print out all prime numbers below an initial prime number, which is set using a constant in the code. –Create a sub to print out a letter grade according to a numeric grade that is set as a constant in the code. –Create a sub to print a customized greeting based on a person’s name and city of birth.

40 Now let’s do functions Create a function that will compute and return a person’s GPA based on some made up formula (hours studied, etc.) Create a function that will compute and return a person’s GPA based on some made up formula (hours studied, etc.) Create a function that will calculate the monthly hours of television watched based on daily TV viewing habits Create a function that will calculate the monthly hours of television watched based on daily TV viewing habits Create a function that will compute a person’s life span based on a few of their health habits (do they smoke, do they exercise, family illness, etc.) Create a function that will compute a person’s life span based on a few of their health habits (do they smoke, do they exercise, family illness, etc.)

41 Next Lecture VB Objects and Classes VB Objects and Classes Windows Forms Windows Forms Homework: start working on programming assignment 1: a Pizza Sales Kiosk Homework: start working on programming assignment 1: a Pizza Sales Kiosk


Download ppt "Introduction to VB.NET 2005 Dr. McDaniel IDS4704 Spring 2005."

Similar presentations


Ads by Google