VB.net 中Module的使用1

创建只包含一个共享成员的一个VB.NET类的更简单的方法就是定义一个Module。Module是规则的、不能创建的类,它的成员是静态的。
类模块与模块的区别:
1.类模块中的数据对创建的每一个新实例都是惟一的,而在标准模块中的数据对应 应用程序或者对标准模块内的方法是全局的.
2.当类实例失效时,类模块中的数据也失效,或者收集到垃圾桶,而标准模块在应用程序的生存期都是活动的,直到应用程序退出.
但还是不太清楚其实用性,想问一下大家,
1、如果我在 模块(Module)里实例化一个类,如:
Module Model
    Public mod_DB As New DB
        …… ……
 是否 模块(Module)会把这个 mod_DB 成员 shared 了,并且每次在其他地方调用mod_DB时,mod_DB是共享的,mod_DB里面的数据(如果有)那就一定要是相同的,不然数据就不能独立而错误了。
 
 还是 模块(Module)在每次被调用时都 new 一次 DB 赋给 mod_DB ,这时 mod_DB里面的数据就可以互相独立而不受干扰了??
最后,问一下 在模块(Module)里实例化一个类 是否有其意义。好不好或能不能这样做?
请大家指教指教,谢谢。

我以前做了一个小东西,使用module,就是因为类定义问题出现了bug.
模块(Module)在每次被调用时都 new 一次 DB 赋给 mod_DB  会出现干扰。
模块(Module)里实例化一个类 有其意义,如果那个类是唯一的不可变的,用在module里面很好。
Module好比静态类,可以直接使用ModuleName.FunctionName()调用其中方法。

Class ClassName
   public shared Function fnXXX() 
   ...
   End Function
End Class
类似
直接使用ClassName.fnXXX()调用。
Module ModuleName
   public Function fnXXX()
End Module
调用:ModuleName.fnXXX()
Module里的成员在第一次使用到它时被自动初始化。
(默认有个不带参数的New方法,你可以自己添加)

模块怎么会被new 很多次呢?
你在模块中建立了一个类的实例,那么就相当于声明了一个shared的变量。也就是说你可以在程序的任何地方直接使用该类的这个实例的属性或者方法,而不用再实例化一个出来。
共享级别的过程或者字段,是说经常被调用的对象。比如说连接数据库的字符串和返回连接的方法和过程。
你可以在模块中实例化一个类,定义若干的方法或过程,然后就要使用application.run()来打开一个主窗口(或登录窗口)
存在这种疑问可以自己测试呀:
Public Class Form1
    Inherits System.Windows.Forms.Form
    Windows 窗体设计器生成的代码
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim a As AppClass
        a = New AppClass
        a = New AppClass
        a = New AppClass
    End Sub
End Class
Public Class TestClass
    Private i As Integer
    Sub New()
        i = i + 1
    End Sub
    Public ReadOnly Property NowI() As Integer
        Get
            Return i
        End Get
    End Property
End Class
Public Module TestModule
    Public m As New TestClass
End Module
Public Class AppClass
    Sub New()
        MsgBox(TestModule.m.NowI)
    End Sub
End Class
再将Module转换为Class,效果是一样的。
Public Class AppClass
    Sub New()
        MsgBox(TestModuleClass.m.NowI)
    End Sub
End Class
Public NotInheritable Class TestModuleClass
    Public Shared m As TestClass
    Shared Sub New()
        m = New TestClass
    End Sub
End Class
谢谢 大家的意见!按此说法应该是共享的,但这样 实例了的类的数据也要是一定相同的,不然就会有数据不正确,这样说
在 Module 直接写 需 shared 的方法和其字段,而不用实例化其他的类,因为实实例化一个对象一般是要独立的,不受影响的,这样和 Module 里的 共享 思想就有冲突了,
大家觉得是否是这样呢??
另,谢谢大家的热心帮助!
我那个Public Class TestClass
有点问题
Private i As Integer
应该是
Private Shared i As Integer
这样才能反映i是否改变了。
当然,改了以后的结果还是一样的,i没变。
Module和静态类思想是一样的。在内存中只被实例化一次。当然不需要你进行实例化。
包括一些静态的数据成员,他们在托管堆中的位置与一般对象的位置是不同的。
以上是我自己的想法 :)
感觉是那种首先运行,首先实例化的东西。提供初始化功能,具体功能和class很像!
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Contents Introduction Course Materials......................................................................................................2 Prerequisites............................................................................................................3 Course Outline.........................................................................................................4 Microsoft Certified Professional Program...............................................................6 Facilities..................................................................................................................8 Module 1: Overview of the Microsoft .NET Platform Overview.................................................................................................................1 What Is the Microsoft .NET Platform?...................................................................2 What Is the .NET Framework?................................................................................5 What Are the .NET Framework Components?.......................................................9 What Are the Visual Basic .NET Enhancements?................................................17 Review...................................................................................................................19 Module 2: Development Environment Features Overview.................................................................................................................1 Describing the Integrated Development Environment............................................2 Creating Visual Basic .NET Projects......................................................................3 Demonstration: Creating a Visual Basic .NET Project.........................................16 Using Development Environment Features...........................................................17 Demonstration: Using the Visual Studio .NET IDE..............................................29 Debugging Applications........................................................................................30 Demonstration: Debugging a Project....................................................................37 Compiling in Visual Basic .NET...........................................................................38 Lab 2.1: Exploring the Development Environment...............................................42 Review...................................................................................................................47 Module 3: Language and Syntax Enhancements Overview.................................................................................................................1 Data Types...............................................................................................................2 Using Variables.......................................................................................................9 Demonstration: Using Variables and Data Structures...........................................20 Functions, Subroutines, and Properties.................................................................21 Lab 3.1: Working with Variables and Procedures.................................................29 Exception Handling...............................................................................................36 Demonstration: Structured Exception Handling....................................................48 Lab 3.2: Implementing Structured Exception Handling........................................49 Review...................................................................................................................52 iv Programming with Microsoft® Visual Basic® .NET (Prerelease) Module 4: Object-Oriented Design for Visual Basic .NET Overview.................................................................................................................1 Designing Classes....................................................................................................2 Practice: Deriving Classes from Use Cases...........................................................10 Object-Oriented Programming Concepts..............................................................11 Advanced Object-Oriented Programming Concepts.............................................20 Using Microsoft Visio...........................................................................................25 Lab 4.1: Creating Class Diagrams from Use Cases...............................................33 Review...................................................................................................................41 Module 5: Object-Oriented Programming in Visual Basic .NET Overview.................................................................................................................1 Defining Classes......................................................................................................2 Creating and Destroying Objects...........................................................................16 Demonstration: Creating Classes...........................................................................23 Lab 5.1: Creating the Customer Class...................................................................24 Inheritance.............................................................................................................31 Demonstration: Inheritance...................................................................................43 Interfaces...............................................................................................................44 Demonstration: Interfaces and Polymorphism......................................................50 Working with Classes............................................................................................51 Lab 5.2: Inheriting the Package Class...................................................................65 Review...................................................................................................................74 Module 6: Using Windows Forms Overview.................................................................................................................1 Why Use Windows Forms?.....................................................................................2 Structure of Windows Forms...................................................................................4 Using Windows Forms..........................................................................................12 Demonstration: Manipulating Windows Forms....................................................27 Using Controls.......................................................................................................28 Demonstration: Implementing Drag-and-Drop Functionality...............................42 Windows Forms Inheritance..................................................................................43 Demonstration: Using Windows Forms Inheritance.............................................48 Lab 6.1: Creating the Customer Form...................................................................49 Review...................................................................................................................57 Module 7: Building Web Applications Overview.................................................................................................................1 Introduction to ASP .NET.......................................................................................2 Creating Web Form Applications..........................................................................16 Demonstration: Creating Web Forms....................................................................30 Lab 7.1: Creating the Customer Logon Web Forms..............................................31 Building Web Services..........................................................................................42 Demonstration: Creating a Web Service...............................................................50 Using Web Services..............................................................................................51 Demonstration: Using a Web Service...................................................................57 Multimedia: How Web Services Work.................................................................58 Lab 7.2: Creating and Using the CustomerService Web Service..........................60 Review...................................................................................................................70 Programming with Microsoft® Visual Basic® .NET (Prerelease) v Module 8: Using ADO .NET Overview.................................................................................................................1 ADO .NET Overview..............................................................................................2 .NET Data Providers...............................................................................................5 Demonstration: Retrieving Data Using ADO .NET..............................................18 The DataSet Object...............................................................................................19 Practice: Using DataSets.......................................................................................35 Data Designers and Data Binding.........................................................................37 XML Integration....................................................................................................45 Demonstration: Using XML Schema....................................................................55 Lab 8.1: Creating Applications That Use ADO .NET...........................................56 Review...................................................................................................................69 Module 9: Developing Components in Visual Basic .NET Overview.................................................................................................................1 Components Overview............................................................................................2 Creating Serviced Components.............................................................................11 Demonstration: Creating a Serviced Component..................................................27 Lab 9.1: Creating a Serviced Component..............................................................28 Creating Component Classes.................................................................................35 Demonstration: Creating a Stopwatch Component...............................................40 Creating Windows Forms Controls.......................................................................41 Demonstration: Creating an Enhanced TextBox...................................................48 Creating Web Forms User Controls......................................................................49 Demonstration: Creating a Simple Web Forms User Control...............................53 Lab 9.2: Creating a Web Forms User Control.......................................................54 Threading..............................................................................................................60 Demonstration: Using the SyncLock Statement....................................................73 Review...................................................................................................................74 Module 10: Deploying Applications Overview.................................................................................................................1 Describing Assemblies............................................................................................2 Choosing a Deployment Strategy..........................................................................11 Deploying Applications.........................................................................................18 Lab 10.1: Packaging a Component Assembly.......................................................20 Demonstration: Deploying a Web-Based Application..........................................28 Lab 10.2: Deploying a Windows-Based Application............................................29 Review...................................................................................................................33 Module 11: Upgrading to Visual Basic .NET Overview.................................................................................................................1 Deciding Whether to Upgrade.................................................................................2 Options for Upgrading.............................................................................................7 Recommendations.................................................................................................11 Performing the Upgrade........................................................................................13 Demonstration: Using the Upgrade Wizard..........................................................22 Review...................................................................................................................23
Programming with Microsoft® Visual Basic® .NET (Prerelease) iii Contents Introduction Course Materials.........................................................................................2 Prerequisites...............................................................................................3 Course Outline............................................................................................4 Microsoft Certified Professional Program.......................................................6 Facilities.....................................................................................................8 Module 1: Overview of the Microsoft .NET Platform Overview....................................................................................................1 What Is the Microsoft .NET Platform?...........................................................2 What Is the .NET Framework?......................................................................5 What Are the .NET Framework Components?.................................................9 What Are the Visual Basic .NET Enhancements?..........................................17 Review.....................................................................................................19 Module 2: Development Environment Features Overview....................................................................................................1 Describing the Integrated Development Environment.......................................2 Creating Visual Basic .NET Projects..............................................................3 Demonstration: Creating a Visual Basic .NET Project....................................16 Using Development Environment Features...................................................17 Demonstration: Using the Visual Studio .NET IDE........................................29 Debugging Applications.............................................................................30 Demonstration: Debugging a Project............................................................37 Compiling in Visual Basic .NET..................................................................38 Lab 2.1: Exploring the Development Environment.........................................42 Review.....................................................................................................47 Module 3: Language and Syntax Enhancements Overview....................................................................................................1 Data Types.................................................................................................2 Using Variables...........................................................................................9 Demonstration: Using Variables and Data Structures......................................20 Functions, Subroutines, and Properties.........................................................21 Lab 3.1: Working with Variables and Procedures...........................................29 Exception Handling...................................................................................36 Demonstration: Structured Exception Handling.............................................48 Lab 3.2: Implementing Structured Exception Handling...................................49 Review.....................................................................................................52 iv Programming with Microsoft® Visual Basic® .NET (Prerelease) Module 4: Object-Oriented Design for Visual Basic .NET Overview....................................................................................................1 Designing Classes........................................................................................2 Practice: Deriving Classes from Use Cases...................................................10 Object-Oriented Programming Concepts.......................................................11 Advanced Object-Oriented Programming Concepts........................................20 Using Microsoft Visio................................................................................25 Lab 4.1: Creating Class Diagrams from Use Cases.........................................33 Review.....................................................................................................41 Module 5: Object-Oriented Programming in Visual Basic .NET Overview....................................................................................................1 Defining Classes..........................................................................................2 Creating and Destroying Objects.................................................................16 Demonstration: Creating Classes.................................................................23 Lab 5.1: Creating the Customer Class...........................................................24 Inheritance................................................................................................31 Demonstration: Inheritance.........................................................................43 Interfaces..................................................................................................44 Demonstration: Interfaces and Polymorphism...............................................50 Working with Classes................................................................................51 Lab 5.2: Inheriting the Package Class...........................................................65 Review.....................................................................................................74 Module 6: Using Windows Forms Overview....................................................................................................1 Why Use Windows Forms?...........................................................................2 Structure of Windows Forms.........................................................................4 Using Windows Forms...............................................................................12 Demonstration: Manipulating Windows Forms..............................................27 Using Controls..........................................................................................28 Demonstration: Implementing Drag-and-Drop Functionality...........................42 Windows Forms Inheritance........................................................................43 Demonstration: Using Windows Forms Inheritance........................................48 Lab 6.1: Creating the Customer Form...........................................................49 Review.....................................................................................................57 Module 7: Building Web Applications Overview....................................................................................................1 Introduction to ASP .NET.............................................................................2 Creating Web Form Applications.................................................................16 Demonstration: Creating Web Forms...........................................................30 Lab 7.1: Creating the Customer Logon Web Forms........................................31 Building Web Services...............................................................................42 Demonstration: Creating a Web Service.......................................................50 Using Web Services...................................................................................51 Demonstration: Using a Web Service...........................................................57 Multimedia: How Web Services Work.........................................................58 Lab 7.2: Creating and Using the CustomerService Web Service.......................60 Review.....................................................................................................70 Programming with Microsoft® Visual Basic® .NET (Prerelease) v Module 8: Using ADO .NET Overview....................................................................................................1 ADO .NET Overview...................................................................................2 .NET Data Providers....................................................................................5 Demonstration: Retrieving Data Using ADO .NET........................................18 The DataSet Object....................................................................................19 Practice: Using DataSets.............................................................................35 Data Designers and Data Binding................................................................37 XML Integration.......................................................................................45 Demonstration: Using XML Schema............................................................55 Lab 8.1: Creating Applications That Use ADO .NET.....................................56 Review.....................................................................................................69 Module 9: Developing Components in Visual Basic .NET Overview....................................................................................................1 Components Overview.................................................................................2 Creating Serviced Components....................................................................11 Demonstration: Creating a Serviced Component............................................27 Lab 9.1: Creating a Serviced Component......................................................28 Creating Component Classes.......................................................................35 Demonstration: Creating a Stopwatch Component.........................................40 Creating Windows Forms Controls..............................................................41 Demonstration: Creating an Enhanced TextBox.............................................48 Creating Web Forms User Controls..............................................................49 Demonstration: Creating a Simple Web Forms User Control...........................53 Lab 9.2: Creating a Web Forms User Control................................................54 Threading.................................................................................................60 Demonstration: Using the SyncLock Statement.............................................73 Review.....................................................................................................74 Module 10: Deploying Applications Overview....................................................................................................1 Describing Assemblies.................................................................................2 Choosing a Deployment Strategy.................................................................11 Deploying Applications..............................................................................18 Lab 10.1: Packaging a Component Assembly................................................20 Demonstration: Deploying a Web-Based Application.....................................28 Lab 10.2: Deploying a Windows-Based Application......................................29 Review.....................................................................................................33 Module 11: Upgrading to Visual Basic .NET Overview....................................................................................................1 Deciding Whether to Upgrade.......................................................................2 Options for Upgrading.................................................................................7 Recommendations.....................................................................................11 Performing the Upgrade.............................................................................13 Demonstration: Using the Upgrade Wizard...................................................22 Review.....................................................................................................23
Contents Introduction Course Materials..................................................................................................2 Prerequisites.........................................................................................................3 Course Outline.....................................................................................................4 Microsoft Certified Professional Program...........................................................6 Facilities...............................................................................................................8 Module 1: Overview of the Microsoft .NET Platform Overview..............................................................................................................1 What Is the Microsoft .NET Platform?................................................................2 What Is the .NET Framework?............................................................................5 What Are the .NET Framework Components?....................................................9 What Are the Visual Basic .NET Enhancements?.............................................17 Review...............................................................................................................19 Module 2: Development Environment Features Overview..............................................................................................................1 Describing the Integrated Development Environment.........................................2 Creating Visual Basic .NET Projects...................................................................3 Demonstration: Creating a Visual Basic .NET Project......................................16 Using Development Environment Features.......................................................17 Demonstration: Using the Visual Studio .NET IDE..........................................29 Debugging Applications....................................................................................30 Demonstration: Debugging a Project.................................................................37 Compiling in Visual Basic .NET.......................................................................38 Lab 2.1: Exploring the Development Environment...........................................41 Review...............................................................................................................46 Module 3: Language and Syntax Enhancements Overview..............................................................................................................1 Data Types...........................................................................................................2 Using Variables....................................................................................................9 Demonstration: Using Variables and Data Structures.......................................20 Functions, Subroutines, and Properties..............................................................21 Lab 3.1: Working with Variables and Procedures.............................................29 Exception Handling...........................................................................................36 Demonstration: Structured Exception Handling................................................48 Lab 3.2: Implementing Structured Exception Handling....................................49 Review...............................................................................................................52 Module 4: Object-Oriented Design for Visual Basic .NET Overview..............................................................................................................1 Designing Classes................................................................................................2 Practice: Deriving Classes from Use Cases.......................................................10 Object-Oriented Programming Concepts...........................................................11 Advanced Object-Oriented Programming Concepts..........................................20 Using Microsoft Visio........................................................................................25 Lab 4.1: Creating Class Diagrams from Use Cases...........................................33 Review...............................................................................................................42 iv Programming with Microsoft® Visual Basic® .NET Module 5: Object-Oriented Programming in Visual Basic .NET Overview..............................................................................................................1 Defining Classes..................................................................................................2 Creating and Destroying Objects.......................................................................16 Demonstration: Creating Classes.......................................................................26 Lab 5.1: Creating the Customer Class................................................................27 Inheritance..........................................................................................................34 Demonstration: Inheritance................................................................................46 Interfaces............................................................................................................47 Demonstration: Interfaces and Polymorphism...................................................53 Working with Classes........................................................................................54 Lab 5.2: Inheriting the Package Class................................................................68 Review...............................................................................................................78 Module 6: Using Windows Forms Overview..............................................................................................................1 Why Use Windows Forms?.................................................................................2 Structure of Windows Forms...............................................................................4 Using Windows Forms.......................................................................................12 Demonstration: Manipulating Windows Forms.................................................27 Using Controls...................................................................................................28 Demonstration: Implementing Drag-and-Drop Functionality............................42 Windows Forms Inheritance..............................................................................43 Demonstration: Using Windows Forms Inheritance..........................................48 Lab 6.1: Creating the Customer Form................................................................49 Review...............................................................................................................58 Module 7: Building Web Applications Overview..............................................................................................................1 Introduction to ASP.NET.....................................................................................2 Creating Web Form Applications......................................................................16 Demonstration: Creating Web Forms................................................................30 Lab 7.1: Creating the Customer Logon Web Forms..........................................31 Building Web Services.......................................................................................42 Demonstration: Creating a Web Service............................................................50 Using Web Services...........................................................................................51 Demonstration: Using a Web Service................................................................57 Multimedia: How Web Services Work..............................................................58 Lab 7.2: Creating and Using the CustomerService Web Service.......................60 Review...............................................................................................................71 Programming with Microsoft® Visual Basic® .NET v Module 8: Using ADO.NET Overview..............................................................................................................1 ADO.NET Overview...........................................................................................2 .NET Data Providers............................................................................................5 Demonstration: Retrieving Data Using ADO.NET...........................................19 The DataSet Object............................................................................................20 Practice: Using DataSets....................................................................................36 Data Designers and Data Binding......................................................................38 XML Integration................................................................................................46 Demonstration: Using XML Schemas...............................................................56 Lab 8.1: Creating Applications That Use ADO.NET........................................57 Review...............................................................................................................70 Module 9: Developing Components in Visual Basic .NET Overview..............................................................................................................1 Components Overview.........................................................................................2 Creating Serviced Components..........................................................................11 Demonstration: Creating a Serviced Component...............................................27 Lab 9.1: Creating a Serviced Component..........................................................28 Creating Component Classes.............................................................................35 Demonstration: Creating a Stopwatch Component............................................41 Creating Windows Forms Controls....................................................................42 Demonstration: Creating an Enhanced TextBox................................................49 Creating Web Forms User Controls...................................................................50 Demonstration: Creating a Simple Web Forms User Control............................54 Lab 9.2: Creating a Web Forms User Control...................................................55 Threading...........................................................................................................61 Demonstration: Using the SyncLock Statement................................................74 Review...............................................................................................................75 Module 10: Deploying Applications Overview..............................................................................................................1 Describing Assemblies.........................................................................................2 Choosing a Deployment Strategy......................................................................11 Deploying Applications.....................................................................................18 Lab 10.1: Packaging a Component Assembly...................................................20 Demonstration: Deploying a Web-Based Application.......................................30 Lab 10.2: Deploying a Windows-Based Application........................................31 Review...............................................................................................................35 Module 11: Upgrading to Visual Basic .NET Overview..............................................................................................................1 Deciding Whether to Upgrade.............................................................................2 Options for Upgrading.........................................................................................7 Recommendations..............................................................................................11 Performing the Upgrade.....................................................................................13 Demonstration: Using the Upgrade Wizard.......................................................22 Review...............................................................................................................23

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值