asp.net in asp.net we implement code reuse by adding component classes
Add a component class to the ASP.net project file if Conn.vb
Open this file, this file is
public class Conn
End Class
So we have two ways of reusing code.
One way to write code directly in class is to define a namespaces on the outside.
as follows (1)
Public Class Conn
Inherits System.ComponentModel.Component
Dim connstring as String
#Region "Component Designer generated code"
Public Sub New (ByVal Container as System.ComponentModel.IContainer)
MyClass.New ()
' Windows.Forms class is required to compose designer support
Container.add (Me)
End Sub
Public Sub New ()
MyBase.New ()
' This call is required by the Component Designer.
InitializeComponent ()
' Add any initialization after the InitializeComponent () call
End Sub
' Component overrides dispose to clean up the list of components.
Protected Overloads Overrides Sub Dispose (ByVal disposing as Boolean)
If disposing Then
If not (components are nothing) Then
Components. Dispose ()
End If
End If
Mybase.dispose (disposing)
End Sub
' Required by the Component Designer
Private Components as System.ComponentModel.IContainer
' NOTE: The following procedure is required by the Component Designer
' You can use the Component Designer to modify this procedure.
' Do not modify it using the Code Editor.
<system.diagnostics.debuggerstepthrough () > Private Sub InitializeComponent ()
components = New System.ComponentModel.Container ()
End Sub
#End Region
Public Function Conned ()
connstring = "User id=sa;password=;initial catalog=huoyun_0405;data Source=chenyang; Connect timeout=30 "
Conned = connstring
End Function
End Class
This is the first way we define a function that returns a database connection string conned
This allows us to reuse code in other files in the following way
Dim include As new {asp.net project name}.conn = new {asp.net project name}.conn
myConnection.ConnectionString = include. Conned ()
So we can define the database connection.
The second way is to customize the namespace
Namespace include
public class Class1
End Class
public class Class2
End Class
End Namespace
The advantage is that you can include multiple functions, processes, until the variable
Other files can be passed in
Imports {asp.net project name}.include references
Or
Imports {asp.net project name}.include.class1 for explicit reference
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.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service