Presentation is loading. Please wait.

Presentation is loading. Please wait.

MIC305 Week 6 Beyond controls Review of properties Differences with VB6: using classes and instances Programming constructs.

Similar presentations


Presentation on theme: "MIC305 Week 6 Beyond controls Review of properties Differences with VB6: using classes and instances Programming constructs."— Presentation transcript:

1 MIC305 Week 6 Beyond controls Review of properties Differences with VB6: using classes and instances Programming constructs

2 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click MessageBox.Show("Hello") End Sub

3 Displaying the message

4 Closing a form in VB.Net '---uses another instance of it Dim frm2 As New Form2 frm2.Show() In VB.NET Whidbey, there is no need to create an instance of another form before you can display it. You can open it directly (just as you can in VB6) using its default instance: '---default instance

5 Review of program constructs Selection and repetition

6 Programming constructs Select Case –For multiple instances of If….then Loop structures –For..next –Do..until –Do….while

7 The if Statement If….then….else The if statement is used to control the flow of execution down one of two or more paths, depending on the result of a logical test. Further conditions may be specified with else if, should previous conditions be false, and else may be used should none of the above be true.

8 Select Case Select Case test expression Case expression-1...this is the code that executes if expression-1 matches test_expression Case expression-2...this is the code that executes if expression-2 matches test_expression Case expression-3...this is the code that executes if expression-3 matches test_expression Case Else...this is the code that executes if expression-n matches test_expression End Select

9 Example: a tourist information site Suppose you want to give some simple information about counties depending on what the user types in: Eg they want to know about Co Durham or Northumberland

10 VB.NET code for the above which object do you attach this to? Dim County As String County = txtInputCounty.Text Select Case County Case "Northumberland" MsgBox ("Northumberland is a beautiful county to visit if you enjoy relaxing countryside.") Case "Co Durham" MsgBox ("Visit the Co Durham for a wide variety of scenary from coastal to the historic city of Durham.") Case "Yorkshire" MsgBox ("Yorkshire is Heartbeat country") Case "Cumbria" MsgBox ("Cumbria is noted for its spectacular mountains scenary and lakes.") Case Else MsgBox ("No specific information is available about this county.") End Select End Sub

11

12 Using Control Structures to Make Code Repeat To write code that repeats some set of statements – for example when you need to perform some calculation over and over or when you have to apply the same calculations or processing to more than one variable. This section shows you all the control structures you can use in VBA to control code in your program that repeats.

13 For…Next For counter = 1 to 10 x = x + 5 msgbox x Next

14 Example Dim x As Integer Dim counter As Integer x = Val(txtInput.Text) For counter = 1 To 10 Step 1 x = x + 2 MsgBox x Next End Sub Event is triggered when the visible form (called the detail) is clicked Variables declared For..next loop, events keep occurring until end of loop

15 Do While…Loop Do While condition...code within the loop goes here Loop

16 Exercise We need 6 people One is the counter 5 are the students in the game

17 Example of Do While First open a new form Put a command button the form called cmdNum Put a textbox on the form called txtOutput InputProcessOutput A numberCheck if it is less than 15, loop if not The number or finished message

18 Example of do…while Dim x As Integer Do While x < 15 x = InputBox("Enter a number") txtOutput.text = x Loop txtOutput.text="finished"

19 Runs where the condition is either true or false. As long as the condition is true, the code within the loop gets executed. Once the condition becomes false, the loop stops and the code after the loop is executed. The only way for the program to break out of the loop is if the condition becomes false or if an Exit Do statement is encountered somewhere inside the loop.

20 Review Constructs –how useful for programming games Next week OO programming: using classes


Download ppt "MIC305 Week 6 Beyond controls Review of properties Differences with VB6: using classes and instances Programming constructs."

Similar presentations


Ads by Google