Build namespace with vb.net
(Wang Tian Kai November 05, 2001 18:53)
Since Microsoft released the. Net framework, as. NET an important program development language--vb.net, began to let those who previously biased to VB people surprised. Although VB.net is a subsequent version of VB, the difference between the two is very huge. The previous version of VB, although claiming to be an OOP (object-oriented) programming language, is not a thoroughly OOP programming language, at most just half an object-oriented programming language. In fact, he is an event-oriented programming language. It is for this reason, so the previous VB in the function is relatively weak. But this has changed dramatically since Microsoft launched vb.net, vb.net is already a fully object-oriented programming language. Among him support the basic object-oriented features: inheritance, polymorphism, and overloading. This makes it difficult or impossible to realize the problem in VB before, in the vb.net can be smooth, simple realization.
The. NET FrameWork SDK, introduced by Microsoft, is a huge software development package specifically designed for the. NET Program development language. Where the basic element is the namespace (NameSpace), the namespace is essentially a large class library (class libraries). Many of the classes, objects, properties, and methods are defined in it ... NET development language is to rely on some classes, objects, attributes and methods to enrich their own interface, to achieve the powerful function of software. In fact, it can be said that only with these namespaces,. Net development tools (which must include vb.net) to maximize their capabilities. So can we rely on our own development tools to make a name space of our own? This article is about how to use the. NET development tool, to create a namespace of your own. The order of introduction is:
(1) Create a WinForm application with vb.net.
(2) Turn the WinForm program into a namespace with a well-defined class in the namespace. This class is the WinForm, which is the key to this article.
(3). Then use vb.net to create a WinForm program that inherits the class from the namespace above and constructs its own form interface. This paragraph is mainly about testing the namespaces we just created.
The following steps to install the above step by step to build their own namespaces.
I. Design and operation of the environment
(1) Microsoft Company Windows 2000 Professional Edition
(2). Net FrameWork SDK Beta 2
Two. Create a WinForm application with vb.net.
For how to use vb.net to create a WinForm application, you can refer to the site of an article "with the vb.net to do WinForm application," in the article has a more detailed introduction. This article uses an already established vb.net WinForm program as an example, and here is the running interface and source code for this WinForm application:
(1). Source code (App.vb):
Imports System.Drawing
Imports System.Windows.Forms
Imports System.ComponentModel
Public Class Form2
Inherits Form
Public Sub New ()
MyBase.New ()
InitializeComponent ()
End Sub
Private Components as System.ComponentModel.Container
Protected Cancel as Button
Private LastName as TextBox
Private FirstName as TextBox
Private Label2 as Label
Private Label1 as Label
Protected OK as Button
Dim Form2 as Form
Private Sub InitializeComponent ()
Me.label1 = New Label ()
Me.firstname = New TextBox ()
Me.ok = New Button ()
Me.label2 = New Label ()
Me.lastname = New TextBox ()
Me.cancel = New Button ()
Me.suspendlayout ()
'
Me.Label1.Location = New Point (8, 24)
Me.Label1.Name = "Label1"
Me.Label1.TabIndex = 1
Me.Label1.Text = "Name:"
Me.FirstName.Location = New Point (120, 16)
Me.FirstName.Name = "Name"
Me.FirstName.Size = New Size (136, 20)
Me.FirstName.TabIndex = 3
Me.FirstName.Text = ""
Me.ok.Location = New Point (8, 128)
Me.ok.Name = "OK"
Me.ok.Size = New Size (112, 32)
Me.ok.TabIndex = 0
Me.ok.Text = "OK"
Me.Label2.Location = New Point (8, 48)
Me.Label2.Name = "Label2"
Me.Label2.Size = New Size (88, 16)
Me.Label2.TabIndex = 2
Me.Label2.Text = "Mailbox:"
Me.LastName.Location = New Point (120, 40)
Me.LastName.Name = "Mail"
Me.LastName.Size = New Size (136, 20)
Me.LastName.TabIndex = 4
Me.LastName.Text = ""
Me.cancel.Location = New Point (144, 128)
Me.cancel.Name = "Cancel"
Me.cancel.Size = New Size (112, 32)
Me.cancel.TabIndex = 5
Me.cancel.Text = "Cancel"
Me.autoscalebasesize = New Size (5, 13)
Me.clientsize = New Size (272, 181)
Me.Controls.AddRange (New control () {me.cancel, me.lastname, Me.firstname, Me.label2, Me.label1, Me.ok})
Me.Name = "Form2"
Me.Text = "main Window"
Me.resumelayout (False)
End Sub
End Class
Module Module1
Sub Main ()
Application.Run (New Form2 ())
End Sub
End Module
(2). Compile command:
After compiling with the following command line:
Vbc/t:winexe/r:system.dll/r:system.windows.forms.dll/r:system.drawing.dll App.vb
The following interfaces are available when you run the program:
Figure 01:winform Run the generated interface
Three. Change this WinForm program (App.vb) to a namespace.
This paragraph is the focus of this article, in fact, any one. NET after a certain change, generally can become a namespace, here on the process of introducing this transformation.
The following two steps are modified on the App.vb:
(1). In the next row of the import namespace, add namespace my, and in later-generated files, a namespace with my name is encapsulated, as follows: Imports System.Drawing
Imports System.Windows.Forms
Imports System.ComponentModel
NameSpace my
(2). Delete the call to the end of the program and add the code end NameSpace as follows:
Delete:
Module Module1
Sub Main ()
Application.Run (New Form2 ())
End Sub
End Module
Join:
End NameSpace
(3). The modified procedure Code (Name.vb) is as follows:
Imports System.Drawing
Imports System.Windows.Forms
Imports System.ComponentModel
(3). The modified procedure Code (Name.vb) is as follows:
Imports System.Drawing
Imports System.Windows.Forms
Imports System.ComponentModel
NameSpace my
Public Class Form2
Inherits Form
Public Sub New ()
MyBase.New ()
InitializeComponent ()
End Sub
Private Components as System.ComponentModel.Container
Protected Cancel as Button
Private LastName as TextBox
Private FirstName as TextBox
Private Label2 as Label
Private Label1 as Label
Protected OK as Button
Dim Form2 as Form
Private Sub InitializeComponent ()
Me.label1 = New Label ()
Me.firstname = New TextBox ()
Me.ok = New Button ()
Me.label2 = New Label ()
Me.lastname = New TextBox ()
Me.cancel = New Button ()
Me.suspendlayout ()
'
Me.Label1.Location = New Point (8, 24)
Me.Label1.Name = "Label1"
Me.Label1.TabIndex = 1
Me.Label1.Text = "Name:"
Me.FirstName.Location = New Point (120, 16)
Me.FirstName.Name = "Name"
Me.FirstName.Size = New Size (136, 20)
Me.FirstName.TabIndex = 3
Me.FirstName.Text = ""
Me.ok.Location = New Point (8, 128)
Me.ok.Name = "OK"
Me.ok.Size = New Size (112, 32)
Me.ok.TabIndex = 0
Me.ok.Text = "OK"
Me.Label2.Location = New Point (8, 48)
Me.Label2.Name = "Label2"
Me.Label2.Size = New Size (88, 16)
Me.Label2.TabIndex = 2
Me.Label2.Text = "Mailbox:"
Me.LastName.Location = New Point (120, 40)
Me.LastName.Name = "Mail"
Me.LastName.Size = New Size (136, 20)
Me.LastName.TabIndex = 4
Me.LastName.Text = ""
Me.cancel.Location = New Point (144, 128)
Me.cancel.Name = "Cancel"
Me.cancel.Size = New Size (112, 32)
Me.cancel.TabIndex = 5
Me.cancel.Text = "Cancel"
Me.autoscalebasesize = New Size (5, 13)
Me.clientsize = New Size (272, 181)
Me.Controls.AddRange (New control () {me.cancel, me.lastname, Me.firstname, Me.label2, Me.label1, Me.ok})
Me.Name = "Form2"
Me.Text = "main Window"
Me.resumelayout (False)
End Sub
End Class
End NameSpace
(4). Compile Name.vb generate namespaces, this is also a more important aspect, in the compilation of Name.vb to compile into DLL file, the specific commands are as follows:
Vbc/t:library/r:system.dll/r:system.windows.forms.dll/r:system.drawing.dll Name.vb
A Name.dll file is generated at this point, and the namespace--my is defined in this file. A class--form2 is encapsulated in the My namespace.
Four Test our namespace--my.
As with other namespaces, the best way to test him is to import him into the program, which is to generate its own WinForm program by inheriting a Form2 class from this namespace my. For how to inherit this Form2 class, the specific statement is as follows:
Imports my
Public Class Form1
Inherits My. Form2
(1). The following program mainly inherits a Form2 class from the My namespace and adds a label and a text box to it. The program source code (Main.vb) is as follows:
Imports System.Drawing
Imports System.Windows.Forms
Imports System.ComponentModel
Imports my
Public Class Form1
Inherits My. Form2
Private Label3 as Label
Private Externalcompany as TextBox
Public Sub New ()
InitializeComponent ()
End Sub
Private Sub InitializeComponent ()
Label3 = New Label ()
Externalcompany = New TextBox ()
Label3.location = New Point (8, 72)
Label3.size = New Size (88, 16)
Label3.text = "Address:"
Externalcompany.location = New Point (120, 62)
Externalcompany.size = New Size (136, 20)
Me.Text = "Inherit the window!" "
ME.CONTROLS.ADD (LABEL3)
ME.CONTROLS.ADD (Externalcompany)
End Sub
End Class
Module Module1
Sub Main ()
Application.Run (New Form1 ())
End Sub
End Module
(2). Compile the build Guest execution file (Main.exe):
This section is also very important, how to compile the Main.vb file? The Name.dll file is introduced at compile time because it encapsulates the my namespace inside of him. The specific command line is as follows:
Vbc/t:winexe/r:system.dll/r:system.windows.forms.dll/r:systme.drawing.dll/r:name.dll Mani.vb
After the compilation is complete, the Main.exe file is generated, and the following interface is executed:
Figure 02: The interface generated when testing my namespace
This shows that we have successfully created a namespace of our own.
Five Summarize:
Inheritance is an important part of object-oriented development language, in program design, it can improve the usability of the program, and make use of the minimum code to develop the larger application.
Through the above also can be seen to do a namespace is not a difficult thing, the key is to be right. NET structure has a more clear understanding. Vb. NET as a new generation of development language, he provides us with a broad program development platform, on this platform, we can be a wider range of flying their own ideas.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.