1 / 24

 过程

 过程. 创建过程 使用过程 使用预定义函数. 过程的创建. 1 过程的创建. 定义 创建 Sub 过程 创建 Function 过程 在过程中声明参数 使用可选参数 代码的可复用性. 过程. 1.1 过程. 过程是程序中可以重复执行的一段代码。过程包含在一个声明语句和一个 End 语句之间 三种类型 Sub 过程(包含事件 Sub 过程) Function 过程 Property 过程 可以重用代码 声明时默认为公有类型. 创建 Sub 过程. 1.2 创建 Sub 过程.

koko
Download Presentation

 过程

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1.  过程 创建过程 使用过程 使用预定义函数

  2. 过程的创建 1 过程的创建 定义 创建 Sub 过程 创建 Function 过程 在过程中声明参数 使用可选参数 代码的可复用性

  3. 过程 1.1 过程 • 过程是程序中可以重复执行的一段代码。过程包含在一个声明语句和一个 End 语句之间 • 三种类型 • Sub过程(包含事件 Sub过程) • Function 过程 • Property 过程 • 可以重用代码 • 声明时默认为公有类型

  4. 创建 Sub 过程 1.2 创建 Sub 过程 Sub 过程执行操作,但是不向调用它的过程返回值 [访问限定符] Sub 过程名[(参数列表)] ′在此输入过程代码 End Sub 示例 Private Sub AboutHelp( ) MessageBox.Show(“我的第一个应用程序 V1.0", "我的第一个应用程序 Help") End Sub

  5. 例如:MessageBox.Show用法: • MessageBox.Show(text , caption, buttons) • text • 要在消息框中显示的文本。 • caption • 要在消息框的标题栏中显示的文本。 • buttons • MessageBoxButtons值之一,可指定在消息框中显示哪些按钮。

  6. 创建 Function 过程 1.3 创建 Function 过程 Function 过程执行操作并且可以向其调用程序返回值 [访问限定符] Function name[(参数列表)] As 数据类型 ′在这里输入函数体语句返回语句 End Function 示例 Public Function Area(ByVal R As Double) As Double . . . Return R*R *3.14159 . . . ‘或Area=R*R *3.14159 End Function

  7. 在过程中声明参数 1.4 在过程中声明参数 • 传递给过程的值称为参数 • 使用 ByVal 和 ByRef 传递参数 • ByVal:被调函数不能改变变量的值 • ByRef:被调函数可使用参数向调用函数返回新的值 • VB .NET 默认采用 ByVal 来传递参数 • 语法和示例 ([ByVal|ByRef] 参数名As datatype) (ByVal Name As String)

  8. 例如: ByVal方式 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim myNumber As Integer = 10 Count(myNumber) MessageBox.Show(myNumber) End Sub Sub Count(ByVal Number As Integer) Number = Number + 1 End Sub

  9. ByRef方式 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim myNumber As Integer = 10 Count(myNumber) MessageBox.Show(myNumber) End Sub Sub Count(ByRef Number As Integer) Number = Number + 1 End Sub

  10. 使用可选参数 1.5 使用可选参数 • 声明可选参数 • 必须指定所有可选参数的缺省值 • 可选参数的缺省值必须是常数表达式 • 过程的定义中,可选参数后面的每个参数也必须是可选的 • 语法 (Optional [ByVal|ByRef]参数名 As数据类型 = 默认值) 示例 Function Add (ByVal value1 As Integer, ByVal value2 As _ Integer, Optional ByVal value3 As Integer = 0) As Integer

  11. 代码的可复用性 1.6 代码的可复用性 类 别 目的 示例 结构 创建不需要扩展而且占用内存较少的对象 Size 和 Point 模块 为多个模块或类提供工具函数和全局数据 温度转换 类 需要扩展的对象,或者是需要资源清理的对象 Forms 类、 Button 类等 创建一个模块: [Public|Friend] Module ModuleName . . . End Module

  12. 课堂练习 在模块中创建函数 1.7 课堂练习 在模块中创建函数 打开新的项目 在项目中添加新的模块 在模块中创建一个新的函数 为函数编写代码

  13. 过程 创建过程 使用过程 使用预定义函数

  14. 使用过程 2 使用过程 • 使用 Sub 过程 • 使用 Function 过程 • 向过程传递数组 • 创建 Sub Main

  15. 使用 Sub 过程 2.1 使用 Sub 过程 Public Sub Hello(ByVal name As String) MessageBox.Show("Hello " & name) End Sub Sub Test( ) Hello(“北京!") End Sub

  16. 课堂练习 通过引用传递参数 2.2 课堂练习 通过引用传递参数 打开新的项目 创建用户界面 创建含有一个引用型参数的过程 调用过程并传递参数

  17. 使用 Function 过程 2.3 使用 Function 过程 • 调用函数 • 函数的调用可以放在赋值语句的右面或是表达式中 • 在表达式中调用函数 : Dim celsiusTemperature As Single celsiusTemperature = FtoC(80) If FtoC(userValue) < 0 Then ... End If

  18. 版本差异 2.4 版本差异 • 传给过程的参数必须放在圆括号中 • Option Strict 设置

  19. 为应用程序编写代码 课堂练习 函数返回值的使用 2.5 课堂练习 函数返回值的使用 创建用户界面

  20. 向过程传递数组 2.6 向过程传递数组 • 传递数组 Sub PassArray(ByVal testScores As Integer( )) ... End Sub Dim scores( ) As Integer = {80, 92, 73} PassArray(scores) • 声明参数数组 Sub StudentScores(ByVal name As String, ByVal _ ParamArray scores( ) As String) ' Statements for Sub procedure End Sub • 使用参数数组调用过程 StudentScores("Anne","10","26","32","15","22","16")

  21. 创建 Sub Main 2.7 创建 Sub Main • Sub Main:应用程序启动对象 • Application.Run:开始应用程序 • Application.Exit:退出应用程序

  22. 课堂练习 创建 Sub Main 2.8 课堂练习 创建 Sub Main 声明变量 创建 Sub Main 过程 为 Selection 窗体编写代码 编写代码退出应用程序 测试应用程序

  23. 小结 学习完本章后,能够 区分 Sub 过程和 Function 过程 创建与调用 Sub 过程和 Function 过程 为模块编写过程,以实现代码重用 分别通过值和引用来传递参数

  24. 实验 1 创建和使用过程 • 练习 1 在模块中创建函数 • 练习 2 使用主窗体

More Related