十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
new
专注于为中小企业提供成都网站设计、成都网站制作服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业谯城免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了成百上千企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。
就是实例化,比如实例化一个类,你才能访问,,
好比,你是张三,把你的地址告诉了我,我在能找到你家在哪,这个过程就可以称为实例化。
使用New关键字建立一个新的对象和用它属的类来定义是一样的。New关键字可以被用作来建立一个窗体、类模块以及集合的实例。用户在设计的时候建立的每一个窗体都是一个类。New关键字能够用来建立一个类的实例。
可以通过以下步骤来建立一个类的实例:
(1)在窗体上画一个按钮和几个其他的控件。
(2)按钮的Click事件过程加入以下代码:
Dim
x
AS
New
Form1
x.Show
(3)运行程序,并且点击几次按钮,把最前面的窗体移开,因为窗体是一个有可视界面的类,此时可以看见有很多窗体的拷贝,每一个窗体具有相同的控件,并且出现在一个地方。
(4)将列代码加到按钮的click事件过程中:
Dim
f
As
Form1
f=New
Form1
f.text="hello"
f.Show
使用New关键字可以从类模块中定义的类中建立一个新的集合和对象,要知道它们怎样工作,可以参考下面的例子:
(1)建立一个新的工程,并且在一个名为Form1的窗体上画一个按钮控件。
(2)在Project(项目)菜单下,选择"Add
Class"给工程添加一个类
(3)把新的类命名为ShowMe.VB
(4)在该新类中加入如下代码:
PUblic
Class
ShowMe
Sub
ShowFrm()
Dim
frmNew
As
Form1
frmNew=New
Form1
frmNew.Show()
frmNew.WindowState=1
End
Sub
End
Class
(5)在Button1_click事件的过程中加入如下代码:Protected
Sub
Button1_Click(Byval
sender
As
System.Object,_
Byval
AS
System.EventArgs)
Dim
clsNew
As
New
ShowMe()
clsNew.ShowFrm()End
Sub
运行程序,并且点击几次按钮,将看见每创建一个ShowMe类的新的实例时,在桌面上有一个最小化的窗体的图标。
New关键字只能用来建立一个类的对象,不能建立一个基于基本的数据类型的对象,例如Integer。而且,不能建立一个基于一个具体对象的对象。例如,下面的代码根据一个名为SomeClass的类,建立了一个名为Objx的对象,面后又错误地企图根据对象Objx再建立一个其他的对象:
Dim
Objx
As
New
SomeClass()
Dim
Objx
AS
New
Objx()
以上代码是非法的。
呵呵##~
用type关键字定义(VB.NET是Structure关键字)
或者定义一个class.VB6是类模块,VB.NET就是class类
例(VB6)
type Point
X as Integer
Y as Integer
end type
条用 dim zuobiao as point
zuobiao.x=你想要的X
zuobiao.y=你想要的Y
VB.NET
Structure Point
Dim X As Integer
Dim Y As Integer
End Structure
条用,要先创建实例对象
Dim X As New Point
X.X = 0
X.Y = 0
vb.net是基于.NET框架的完全对向对象的编程语言,而vb 6.0只是半面向对象的语言,使用vb.net可以编制出功能更加强大的windows程序。现在,我们把vb.net和vb 6.0做一下比较,看看vb.net对比vb 6.0有着什么样的特性和优点。 1.在vb 6.0中,创建对象的时候,需要等这个对象创建完毕后,再另外调用一个独立的方法对其进行初始化。在vb.net中, 可以利用构造函数为对象赋初值,这样就不需要进行烦琐的调用赋初值了。构造函数的使用,简化了编码的过程和出错的机会。 2.vb.net编写线程的能力比vb 6.0有很大的增强。 3.在vb 6.0中,需要首先声明一个变量,然后才能对其赋值,这样就需要写两行代码。在vb.net中,可以 使用初始化函数将这两个步骤合并在一行代码中完成,这个似乎微小的改进,提供了更少、更简单、更易于维护的代码。 4.vb.net基于.NET框架,开发者可以快速的可视化开发网络应用程序、网络服务、Windows应用程序和服务器端组件。 5.因为vb.net是基于.NET框架的,可以与其它.NET语言协同工作。 6.在vb.net中,通过Web窗体及ADO.NET,开发者可以快速开发可扩展的Web站点。 如果你能进一步学习vb.net,你会发现更多的新特性和优点。
用CreateObject方法创建控件,或者把对应的文件打包在同一目录,或者打包进程序里面待使用的时候再动态释放出来,还有两种动态调用的方法可供参考:
(1)VB使用未注册的ActiveX代码
''IDE下可以引用那个dll使用。编译后可以在未注册dll的计算机上正常工作了。
''使用方法:
''Dim pDll As Long ''记录Dll,用来最后完美释放
''Dim Update As Update.Handle ''要实例化的对象
''Set Update = LoadObjectByName(App.Path "Update.dll", "Handle", pDll) '' New Update.Handle
''If Update Is Nothing Then Exit Sub
''Update.Test ''--类中的方法
''Set Update = Nothing ''-----这句不能少,否则会出现意外错误
''UnLoadDll pDll ''----释放
''模块中:
Option Explicit
Private Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryW" (ByVal lpLibFileName As Long) As Long
Private Declare Function GetProcAddress Lib "kernel32" (ByVal hModule As Long, ByVal lpProcName As String) As Long
Private Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As Long) As Long
Private Declare Function CallAsmCode Lib "user32" Alias "CallWindowProcW" (FirstAsmCode As Long, ByVal pA As Long, ByVal pB As Long, ByVal pC As Long, lpD As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private AsmCode(94) As Long
Function LoadObjectByName(ByVal DllPath As String, ByVal ClsName As String, pDll As Long) As Object
Dim pObj As Long, TLIAPP As Object, TLI As Object
Dim CLSID As String, IID As String, IIDName As String
On Error GoTo Err
Set TLIAPP = CreateObject("TLI.TLIApplication")
Set TLI = TLIAPP.TypeLibInfoFromFile(DllPath)
IIDName = "_" Trim(ClsName)
CLSID = TLI.GetTypeInfo(Trim(ClsName)).GUID
IID = TLI.GetTypeInfo(Trim(IIDName)).GUID
Set LoadObjectByName = LoadObjectByID(DllPath, CLSID, IID, pDll)
Set TLI = Nothing
Err:
End Function
Function LoadObjectByID(ByVal DllPath As String, ByVal CLSID As String, ByVal IID As String, pDll As Long) As Object
Dim pObj As Long
Call InitAsmCode
pObj = CallAsmCode(AsmCode(20), StrPtr(DllPath), StrPtr(CLSID), StrPtr(IID), pDll)
CopyMemory LoadObjectByID, pObj, 4
End Function
Function UnLoadDll(pDll As Long) As Long
Call InitAsmCode
UnLoadDll = CallAsmCode(AsmCode(79), pDll, 0, 0, 0)
End Function
Private Sub InitAsmCode()
If AsmCode(4) Then Exit Sub
Dim pDll As Long
pDll = LoadLibrary(StrPtr("kernel32"))
AsmCode(0) = GetProcAddress(pDll, "LoadLibraryW")
AsmCode(1) = GetProcAddress(pDll, "GetProcAddress")
AsmCode(2) = GetProcAddress(pDll, "FreeLibrary")
Call FreeLibrary(pDll)
AsmCode(4) = H476C6C44
AsmCode(5) = H6C437465
AsmCode(6) = H4F737361
AsmCode(7) = H63656A62
AsmCode(8) = H4C430074
AsmCode(9) = H46444953
AsmCode(10) = H536D6F72
AsmCode(11) = H6E697274
AsmCode(12) = H10067
AsmCode(13) = H0
AsmCode(14) = HC00000
AsmCode(15) = H0
AsmCode(16) = H6F4600
AsmCode(17) = H65006C
AsmCode(18) = H320033
AsmCode(19) = H0
AsmCode(20) = H83EC8B55 ''创建对象函数入口
AsmCode(21) = HE853D8C4
AsmCode(22) = H0
AsmCode(23) = H6CEB815B
AsmCode(24) = H8D100010
AsmCode(25) = H105293
AsmCode(26) = H93FF5210
AsmCode(27) = H10001010
AsmCode(28) = H32938D50
AsmCode(29) = H52100010
AsmCode(30) = H1493FF50
AsmCode(31) = H8D100010
AsmCode(32) = H101C93
AsmCode(33) = HFF028910
AsmCode(34) = H101893
AsmCode(35) = H875FF10
AsmCode(36) = H101093FF
AsmCode(37) = HC00B1000
AsmCode(38) = H86840F
AsmCode(39) = H45890000
AsmCode(40) = H20938DFC
AsmCode(41) = H52100010
AsmCode(42) = H1493FF50
AsmCode(43) = HB100010
AsmCode(44) = H506674C0
AsmCode(45) = H52EC558D
AsmCode(46) = HFF0C75FF
AsmCode(47) = H101C93
AsmCode(48) = H558D5810
AsmCode(49) = H938D52D8
AsmCode(50) = H10001042
AsmCode(51) = HEC558D52
AsmCode(52) = HBD0FF52
AsmCode(53) = H8D3E75C0
AsmCode(54) = HFF52DC55
AsmCode(55) = H93FF1075
AsmCode(56) = H1000101C
AsmCode(57) = HD8558B50
AsmCode(58) = H8D54128B
AsmCode(59) = H6A50DC45
AsmCode(60) = HD875FF00
AsmCode(61) = HB0C52FF
AsmCode(62) = H8B1575C0
AsmCode(63) = H4D8BFC45
AsmCode(64) = H59018914
AsmCode(65) = H8BD18B51
AsmCode(66) = H52FF5112
AsmCode(67) = H14EB5804
AsmCode(68) = HEB06EB58
AsmCode(69) = HEB02EB0F
AsmCode(70) = HFC75FF0B
AsmCode(71) = H101893FF
AsmCode(72) = HC0331000
AsmCode(73) = H10C2C95B
AsmCode(74) = H6C6C4400
AsmCode(75) = H556E6143
AsmCode(76) = H616F6C6E
AsmCode(77) = H776F4E64
AsmCode(78) = H0
AsmCode(79) = H53EC8B55 ''尝试卸载DLL函数入口
AsmCode(80) = HE8
AsmCode(81) = HEB815B00
AsmCode(82) = H10001155
AsmCode(83) = H1139938D
AsmCode(84) = HFF521000
AsmCode(85) = H93FF0875
AsmCode(86) = H10001014
AsmCode(87) = H1374C00B
AsmCode(88) = HC00BD0FF
AsmCode(89) = H75FF0E74
AsmCode(90) = H1893FF08
AsmCode(91) = H33100010
AsmCode(92) = H4801EBC0
AsmCode(93) = H10C2C95B
AsmCode(94) = H90909000
End Sub
(2)vb.net 通过类厂创建com对象的方法
vb.net中要创立com对象可以用new或者CreateObject,就像C++中的CoCreateInstance,一步调用就直接创建了com对象。在它们之中包括了对CoGetClassObject和CreateInstance的调用。默认使用的类厂是IClassFactory。
一般情况下,new或CreateObject已经够用了。但是如果我们的com组件增加了授权,也就是使用了IClassFactory2接口。那么在vb中继续用这种方法就不能创建我们所需要的组件了。我们必须使用IClassFactory2。
在C++中实现很简单。只需要通过CoGetClassObject获得IClassFactory2接口,然后调用它的CreateInstanceLic来创建组件。
因此,在vb.net中,我们就可以仿照c++中的方法。下面是相关代码:
首先声明IClassFactory2的接口。IClassFactory2.idl如下:
[
uuid(6ED6AF97-F279-4d57-A392-0B8ACF89426C),
version(1.0),
helpstring("INVENTOROCIDL Type Library 1.0")
]
library INVENTOROCIDL
{
interface IClassFactory2;
typedef enum enuCLSCTX
{
enuCLSCTX_INPROC_SERVER = 1,
enuCLSCTX_INPROC_HANDLER = 2,
enuCLSCTX_LOCAL_SERVER = 4,
enuCLSCTX_REMOTE_SERVER = 16,
enuCLSCTX_NO_CODE_DOWNLOAD = 400,
enuCLSCTX_NO_FAILURE_LOG = 4000,
enuCLSCTX_SERVER = (1 | 4 | 16),
enuCLSCTX_ALL = (2 | 1)
} CLSCTX;
[
object,
uuid(B196B28F-BAB4-101A-B69C-00AA00341D07),
pointer_default(unique)
]
interface IClassFactory2 : IClassFactory
{
typedef IClassFactory2 * LPCLASSFACTORY2;
typedef struct tagLICINFO {
LONG cbLicInfo;
BOOL fRuntimeKeyAvail;
BOOL fLicVerified;
} LICINFO;
typedef struct tagLICINFO * LPLICINFO;
HRESULT GetLicInfo(
[out, retval] LICINFO * pLicInfo
);
HRESULT RequestLicKey(
[in] LONG dwReserved,
[out, retval] BSTR * pBstrKey
);
[local]
HRESULT CreateInstanceLic(
[in] IUnknown * pUnkOuter,
[in] IUnknown * pUnkReserved,
[in] GUID* riid,
[in] BSTR bstrKey,
[out, retval, iid_is(riid)] PVOID * ppvObj
);
}
}
用midl编译成IClassFactory2.tlb,并导入到vb.net的工程当中。
声明如下api用于得到IClassFactory2接口:
Declare Function CoGetClassObject Lib "ole32.dll" (ByRef rclsid As Guid, ByVal context As Short, ByRef serverInfo As IntPtr, ByRef riid As Guid, ByRef ppv As IntPtr) As Integer
'添加如下代码:
Const bstrInventorApplication As String = "{B6B5DC40-96E3-11d2-B774-0060B0F159EF}"
Const bstrIClassFactory2 As String = "{B196B28F-BAB4-101A-B69C-00AA00341D07}"
Const bstrIDispatch As String = "{00020400-0000-0000-C000-000000000046}"
Private IClsFry2 As INVENTOROCIDL.IClassFactory2
Dim bstrLicence As String = "12345678" 'licence key
Dim guidInventorApplication As Guid = New Guid(bstrInventorApplication)
Dim guidIClassFactory2 As Guid = New Guid(bstrIClassFactory2)
Dim guidIDispatch As Guid = New Guid(bstrIDispatch)
Dim InventorGuid As INVENTOROCIDL.GUID 'used by CreateInstanceLic,defined in INVENTOROCIDL
'transform Guid of IDispatch to INVENTOROCIDL.GUID
Dim byteArry() As Byte = guidIDispatch.ToByteArray()
Dim MyGC As GCHandle = GCHandle.Alloc(byteArry, GCHandleType.Pinned)
InventorGuid = CType(Marshal.PtrToStructure(MyGC.AddrOfPinnedObject, InventorGuid.GetType()), INVENTOROCIDL.GUID)
'get the IClassFactory2 Interface
Dim obj As IntPtr
CoGetClassObject(guidInventorApplication, CInt(INVENTOROCIDL.enuCLSCTX.enuCLSCTX_LOCAL_SERVER), Nothing, guidIClassFactory2, obj)
IClsFry2 = CType(Marshal.GetTypedObjectForIUnknown(obj, System.Type.GetTypeFromCLSID(guidIClassFactory2)), INVENTOROCIDL.IClassFactory2)
'create Inventor Instance by using Licence
obj = IClsFry2.CreateInstanceLic(Nothing, Nothing, InventorGuid, bstrLicence)
InvApp = CType(Marshal.GetTypedObjectForIUnknown(obj, System.Type.GetTypeFromCLSID(guidInventorApplication)), Inventor.Application)
致此创建成功!
My Project.a这个应该是完整的命名空间.类名
你在a的构造函数里面用string s=typeof(a).ToString()就知道了。
VB最终的版本是VB6.0。微软早都不支持了。在Win7上VB6.0是可以用的,但是之前在XP下写好的VB程序有些可能会不能用,原因是WIN7中取消了许多之前的DLL。
VS2012上就是VB.Net了,语法和VB类似。
VB6.0是面向过程开发,VB.net是面向对象开发,但是是为了照顾VB6.0程序员的开发习惯,所以有点不伦不类。