VB.net语言
成都创新互联公司坚持“要么做到,要么别承诺”的工作理念,服务领域包括:网站制作、成都网站制作、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的宁明网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!
1、创建组件定义
新建一个Probject,在代码中写入:
Public Interface IDemo
Function Test() As String
End Interface
2、实现组件定义
新建一个Probject,添加对上述Probject的引用后,在代码中写入
Public Class Demo
Implements IDemo.IDemo
Public Function Test() As String Implements IDemo.IDemo.Test
Test = "Hello World!"
End Function
End Class
3、编译出Demo.dll组件
4、使用.net的工具 Visual Studio .Net 2003 Command Prompt,输入以下命令
Regasm Demo.dll /tlb:Demo.tlb
把Demo.dll注册为一个com对象(这个com对象不等同于普通的用VB编写的Com对象),并导出类型库Demo.tlb
5、新建一个VB工程,添加对Demo.tlb和IDemo.tlb的引用
6、定义两个全局变量
Dim vbDemo As Demo.Demo
Dim vbIDemo as IDemo.IDemo
7、在Form_Load事件中初始化
'初始化实体类
Set vbDemo = New Demo.Demo
'将实体类引用到抽象类
Set vbIDemo = vbDemo
8、然后,我们就可以用 vbIDemo.Test 这样的方法了
代码已测试。
一、测试需要的INI文件。名称为“setinfo.ini”,存放路径为你解决方案里“Bin“文件夹的”Debug“文件夹里。
内容:
[Location]
pnlTime.Location.X=787
pnlTime.Location.Y=0
[TopMost]
Top=True
二、操作INI文件的类。新建一个类,类中内容为:
Public Class Class1
Public Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" _
( _
ByVal lpApplicationName As String, _
ByVal lpKeyName As String, _
ByVal lpDefault As String, _
ByVal lpReturnedString As String, _
ByVal nSize As Int32, _
ByVal lpFileName As String _
) As Int32
End Class
三、界面代码:
Imports System.IO 'for path
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
getINI()
End Sub
Private Sub getINI()
Dim iniclass As New Class1 '
Dim myBf As String 'get set_info 开辟存储的内存
myBf = New String(CChar(""), 256)
Dim myFN As String '配置文件的文件路径
myFN = Path.Combine(My.Application.Info.DirectoryPath, "setinfo.ini")
Dim myAN As String 'Application_NAME,也是Section名称
myAN = Me.TextBox1.Text
Dim myKN As String 'KEY_NAME,关键字名称
myKN = Me.TextBox2.Text
Class1.GetPrivateProfileString(myAN, myKN, "", myBf, myBf.Length, myFN) '调用读取INI文件的操作
TextBox3.Text = Microsoft.VisualBasic.Left(myBf, InStr(myBf, Chr(0)) - 1) '截取有用部分,有必要
End Sub
End Class
四、测试截图如下
在你的网站的根目录建立一个叫App_Code的文件,并把这个类放进去。
Sub datalj(ByVal sql1 As String)改成
Public Function datalj(ByVal sql1 As String) As SqlDataReader
conn.ConnectionString = str
conn.Open()
comm = New SqlCommand(sql1, conn)
rs = comm.ExecuteReader
datalj = rs
End Function
使用的时候
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim d As New DB
Dim rs As SqlDataReader = d.datalj("select * from userb")
……
End Sub
'代码来源:代码街codejie.net
'由于在vb.net里变量名不区分大小写(StuName和stuname是一样的),所以个别变量名需要修改
'在vb.net中,模块级变量的命名原则是以m_开头
Public Class Student
Private m_stuNo As String '学生号字段
Private m_stuName As String '学生姓名字段
Private m_stuAge As Integer '学生年龄字段
Private m_stuAddress As String '学生住址字段
'无参构造方法
Public Sub New()
End Sub
'有参构造方法
Public Sub New(ByVal stuNo As String, ByVal stuName As String, ByVal stuAge As Integer, ByVal stuAddress As String)
m_stuNo = stuNo
m_stuName = stuName
m_stuAge = stuAge
m_stuAddress = stuAddress
End Sub
'学生号属性
Public Property StuNo() As String
Get
Return m_stuNo
End Get
Set(ByVal value As String)
m_stuNo = value
End Set
End Property
'学生姓名属性
Public Property StuName() As String
Get
Return m_stuName
End Get
Set(ByVal value As String)
m_stuName = value
End Set
End Property
'学生年龄属性
Public Property StuAge() As Integer
Get
Return m_stuAge
End Get
Set(ByVal value As Integer)
m_stuAge = value
End Set
End Property
'学生住址属性
Public Property StuAddress() As String
Get
Return m_stuAddress
End Get
Set(ByVal value As String)
m_stuAddress = value
End Set
End Property
End Class