项目四_1_曾剑平.ppt

什么是过程? VB中有三类: 事件过程; 函数过程; 子过程。 事件过程VB设置好外壳: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click ‘ 自己的代码 End Sub Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress ‘ 自己的代码 End Sub 函数过程和子过程 – 程序员自己编写 (1)什么时候要把自己的代码写成函数过程或子过程? (2)怎么写,有什么规定? 三个事件过程的代码都相同带来的问题 如果现在想改变验证规则,比如用户名不区分大小写、密码区分大小写等,就需要在三个事件过程中都要进行修改。 容易产生疏忽 编程容易、修改不易。 为什么需要过程? 程序 Dim i as Integer, t1 as Double , t2 as Double , t3 as Double ,m as Integer, n as Integer, c as double M = val(text1.text) N= val(text2.text) t1=1 For i=1 to mt1=t1*I Next I t2=1 For i=1 to m-nt2=t2*I Next I t3=1 For i=1 to nt3=t3*I Next I C = t1/(t2*t3) 过程的一个例子 Private Function factorial(ByVal n as Integer) as Double Dim i as Integer, t as Double t=1 For i=1 to nt=t*I Next I Factorial = t End function 过程的一个例子 函数过程 Private Function factorial(ByVal n as Integer) as Double Dim i as Integer, t as Double t=1 For i=1 to nt=t*i Next i Factorial = t End function 函数过程被调用的范围 两个Form调用的例子: 一个form提供函数f(x)=x*x+1, 另一个form调用这个form的函数 f(x) f(x) 为public时,允许调用; f(x) 为private时,调用失败; 函数的参数列表 参数列表包含参数传递方式,变量名称,as, 变量类型。参数列表中,不同参数之间用,分割。变量名称不能相同。 Private Function factorial(ByVal n as Integer) as Double Private Function Add(ByVal n as Integer, ByVal m as Integer) as Double 变量名称,称为形式参数(formal parameter),简称:形参。 函数的参数列表 函数的参数列表可以是以下几种: (1)没有一个参数,但是()不能省略! Private Function getName() as String (2)有一个参数 Private Function factorial(ByVal n as Integer) as Double (3)有多个参数 Private Function Add(ByVal n as Integer, ByVal m as Integer) as Double 函数的返回与终止 正常结束 : End Function作为结束标志; 如果需要在函数过程体中退出,则用Exit Function 例如:计算(x+y)/(x-y) public function cal(byval x as Integer, byval y as Integer) as Doubleif x-y = 0 thenexit Functionelsecal = (x+y)/(x-y)end if end Function 函数的调用 Private Function factorial(ByVal n as Integer) as Double Dim i as Integer, t as Double t=1 For i=1 to nt=t*i Next i Factorial = t End function

文档评论(0)

1亿VIP精品文档

相关文档