网站公告列表

  没有公告

加入收藏
设为首页
联系站长
您现在的位置: 网络学院 >> 程序设计 >> VB编程 >> 文章正文
  VB.NET模块的总结(二)            【字体:
VB.NET模块的总结(二)
作者:佚名    文章来源:不详    点击数:    更新时间:2007-9-12    

 

'Module1.vb文件中的代码:

Module MainClas
  
Sub Main()

    
Dim theFirstInstanceOfTestscope As New TestScope
    theFirstInstanceOfTestscope.TestScopeSubMethodOne()
    
Dim aStr As String = theFirstInstanceOfTestscope.TestScopeFunctiongMethodOne()
    Console.WriteLine(aStr)

    
Dim theFirstInstanceofOutTestScope As New OutTestScope()
    
'同上作用一样,实例化OutTestScope类
    'Dim theFirstInstanceOfOutTestScope As OutTestScope
    'theFirstInstanceOfOutTestScope = New OutTestScope()
    '同上作用一样,实例化OutTestScope类
    'Dim thefirstinstanceofouttestscope = New OutTestScope()
    theFirstInstanceofOutTestScope.OutTestScopeSubMethodOne()
    
Dim aaStr As String = theFirstInstanceofOutTestScope.OutTestScopeFunctiongMethodOne()
    Console.WriteLine(aaStr)
    Console.WriteLine(theFirstInstanceofOutTestScope.cc)

    theFirstInstanceOfTestscope.DiaoYongMMethod() 
'通过类的实例调用其他模块的方法
    Dim mTwoClassFiled As New mTwoClass
    mTwoClassFiled.mTwoClassMethod() 
'模块直接调用其他模块的类中的方法
    Console.WriteLine(mThreeMethod()) '模块直接调用其他模块的方法
    Console.ReadLine()

  
End Sub
End Module


Class TestScope
  
Dim a As String
  
Private b As String
  
Public c As String
  
Sub New() '无参数构造函数
    Me.a = "Dim_a"
    
Me.b = "Private_b"
    
Me.c = "Public_c"
  
End Sub
  
'Public Static Sub TestScopeMethodOne()  '不能用Static去声明Sub方法(或者Sub过程),提示错误:方法不能声明为“Static”
  Public Sub TestScopeSubMethodOne()
    Console.WriteLine(
String.Format("TestScopeSubMethodOne():{0} {1} {2}", a, b, c))
  
End Sub
  
'Public Static Function TestScopeFunctiongMethodOne() As String  ''不能用Static去声明Function方法(或者Function函数),提示错误:方法不能声明为“Static”
  Public Function TestScopeFunctiongMethodOne() As String
    
Return String.Format("TestScopeFunctiongMethodOne():{0} {1} {2}", a, b, c)
  
End Function
  
Public Sub DiaoYongMMethod()
    
'mTwoMethod()
    Console.WriteLine(mThreeMethod())
  
End Sub
End Class

Module mTwo
  
'Public Sub mTwoMethod()
  Private mTwoFiled As String = "mTwoFiled"
  
Private Sub mTwoMethod()  '模块中的方法可以用Private修饰,但只能在当前模块中使用,不能在其他模块中使用
    Console.WriteLine(mTwoFiled)
  
End Sub
  
Class mTwoClass
    
Sub mTwoClassMethod()
      mTwoMethod()
    
End Sub
  
End Class
End Module

Module mThree
  
Private Sub mPrivateThreeMethod()
    Console.WriteLine(
"mPrivateThreeMethod()")
  
End Sub
  
'Public Function mThreeMethod() As String
  Function mThreeMethod() As String '模块中的方法隐士共享,可以在模块外部访问,加不加Public都一样
    mPrivateThreeMethod()
    
Return "mThreeMethod()"
  
End Function
End Module

'在vb.net中不能用static来声明方法(Sub和Function),并且不能用来声明成员变量,只能用来声明方法(Sub和Function)中的静态变量。
正在装载数据……
   

'
shared既可以用来声明变量也可以用来声明方法,还可以用来声明成员变量,这一点刚好跟static相反。
'
vb.ne中的shared更像C#中static的作用。在vb.net中用shared声明的成员变量(字段)和方法(Sub和Function),只能用类名来访问,而不能用类的实例来访问

'一个文件中可以定义多个模块
'
可以像C#一样在另一个文件中定义一类,该类的用法和在一个文件中定义的类的用法相同(包裹字段,方法,属性等等都一样)

'模块中的方法隐士共享,可以在模块外部(其他模块,类,结构等)被直接访问,加不加Public都一样
'
模块中的方法不能声明为Protected,Shared和Static,只能声明为Public(默认就是Public)和Private。
'
但模块中类(或者结构)的方法可以声明为Public,Private,Protected和Shared。



'OutTestScope.vb文件中的代码:

Public Class OutTestScope
  
Dim aa As String
  
Private bb As String
  
Public cc As String
  
Sub New() '无参数构造函数
    Me.aa = "Dim_aa"
    
Me.bb = "Private_bb"
    
Me.cc = "Public_cc"
  
End Sub
  
'Public Static Sub TestScopeMethodOne()  '不能用Static去声明Sub方法(或者Sub过程),提示错误:方法不能声明为“Static”
  Public Sub OutTestScopeSubMethodOne()
    Console.WriteLine(
String.Format("OutTestScopeSubMethodOne():{0} {1} {2}", aa, bb, cc))
  
End Sub
  
'Public Static Function TestScopeFunctiongMethodOne() As String  ''不能用Static去声明Function方法(或者Function函数),提示错误:方法不能声明为“Static”
  Public Function OutTestScopeFunctiongMethodOne() As String
    
Return String.Format("OutTestScopeFunctiongMethodOne():{0} {1} {2}", aa, bb, cc)
  
End Function
End Class




本文来源:http://blog.csdn.net/Terry001/archive/2007/08/27/1761224.aspx
站内文章搜索 高级搜索
文章录入:admin    责任编辑:admin 
  • 上一篇文章:

  • 下一篇文章:
  • 发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
    最新热点 最新推荐 相关文章
     如何在ado中使用connect…
     vb 贪吃蛇 单人版游戏 (…
     关于在vb中进行com组件的…
     用vb6.0编写自我升级的程…
     vb自动登陆网络站点详解…
     vb打造超酷个性化菜单(…
  • Sevrlet 中防止中文乱码策略

  • (JSP)在文本域中显示超链接n…

  • genexus中对字符串的格式补空…

  • SIP简介,第2部分:SIP SERV…

  • JavaWeb中的Session、Sessio…

  • tomcat下配置jspservletbean…

  • .net程序员的盲点(八):泛…

  • OpenCV在VC.net,VC6.0等中的…

  • 同步租塞SOCKET编程

  • 内存管理内幕--Jonathan Bar…

  •   网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)
    网络学院©2007 www.23book.net
    为您提供web编程,vb编程,vc编程,服务器架设管理,数据库设计等方面的知识 站长:David