前面学了对数据的组织,现在再来学对语句的组织。模块化程序的设计思想就是分而治之,这就有了函数,VB.NET中还有一种没有返回值的函数:过程。
1,使用过程4-1.vb可以改写为
 代码
代码
'7-1.vb
Class SimpleCnl
Public Shared Sub Main()
Dim username As String = String.empty, password As String = String.empty
Dim isAuthenticated As Boolean = False
Input("输入用户名:", username)
Input("输入密码:", password)
ValidateUserLogin(username, password, isAuthenticated)
If isAuthenticated Then
System.Console.WriteLine("登陆成功")
Else
System.Console.WriteLine("用户名或密码有误")
End If
End Sub
Public Shared Sub Input(ByVal inputTip As String, ByRef var As String)
System.console.write(inputTip)
var = System.console.ReadLine()
End Sub
Public Shared Sub ValidateUserLogin(ByVal username As String, ByVal password As String, ByRef isAuthenticated As Boolean)
If username = "admin" And password = "123456" Then
isAuthenticated = True
Else
isAuthenticated = False
End If
End Sub
End Class
2.使用函数则可以改为
 代码
代码
'7-2.vb
Class SimpleCnl
Public Shared Sub Main()
Dim username, password As String
username = Input("输入用户名:")
password = Input("输入密码:")
If ValidateUserLogin(username, password) Then
System.Console.WriteLine("登陆成功")
Else
System.Console.WriteLine("用户名或密码有误")
End If
End Sub
Public Shared Function Input(ByVal inputTip As String) As String
System.Console.Write(inputTip)
Input = System.Console.ReadLine()
End Function
Public Shared Function ValidateUserLogin(ByVal username As String, ByVal password As String) As Boolean
If username = "admin" And password = "123456" Then
Return True
Else
Return False
End If
End Function
End Class
3.不管是使用函数,还是过程,定义方式,调用方式,参数的传递方式,都是我们要搞清楚的,VB中定义可变长参数这篇文章,可以给我们一些参考。函数和过程的使用,用多了,我们自然就能掌握使用技巧了。
 
                    
                     
                    
                 
                    
                 

 
                
            
        