
正文
vb.net类的使用案例的简单介绍
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
vb.net 调用其他文件 如 dll
DLL(动态链接库)是一个很有用的东西,在开发大项目的时候显得非常重要,因为多人合作开发时,可以给每个人分配一个任务,用DLL完成,最后组合起来,就不会出现互相冲突的问题。这里给出最简单的DLL编写与调用的示例
首先,我们打开VB.NET,选择类库,名称改为test
然后输入以下代码
Public Class test
Public Function test(ByVal a As Long, ByVal b As Long) As Long
Return a + b
End Function
End Class
保存后,生成DLL文件。
这就是最简单的一个DLL,下面是调用该DLL的示例
新建一个工程,单击“项目” -- 添加引用
找到刚才生成的DLL,双击它
添加引用以后,似乎什么也没发生,这时我们输入以下代码:
Imports test.test
这样,就包含了该DLL的类。
然后我们定义一个类
Dim test As New test.test
这样,就可以使用里面的函数了,下面是程序示例
Imports test.test
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim test As New test.test
MsgBox(test.test(1, 2))
End Sub
End Class
运行该程序,可以看到,调用了DLL内的函数。
这就是最简单的DLL示例,可以将一些复杂的代码集成到DLL里,以后升级或重用都比较方便。
相关问答
Q1: vb.net中如何用事件和委托,会C#中的事件和委托,但不知VB.net中的语法,望给个简单的例子熟悉语法。
一委托vb.net类的使用案例:此示例演示如何将方法与委托关联然后通过委托调用该方法。
创建委托和匹配过程
创建一个名为 MySubDelegate 的委托。
Delegate Sub MySubDelegate(ByVal x As Integer)
声明一个类vb.net类的使用案例,该类包含与该委托具有相同签名的方法。
Class class1
Sub Sub1(ByVal x As Integer)
MsgBox("The value of x is: " CStr(x))
End Sub
End Class
定义一个方法vb.net类的使用案例,该方法创建该委托的实例并通过调用内置的 Invoke 方法调用与该委托关联的方法。
Protected Sub DelegateTest()
Dim c1 As New class1
' Create an instance of the delegate.
Dim msd As MySubDelegate = AddressOf c1.Sub1
' Call the method.
msd.Invoke(10)
End Sub
二、事件
下面的示例程序阐释如何在一个类中引发一个事件vb.net类的使用案例,然后在另一个类中处理该事件。AlarmClock 类定义公共事件 Alarmvb.net类的使用案例,并提供引发该事件的方法。AlarmEventArgs 类派生自 EventArgs,并定义 Alarm 事件特定的数据。WakeMeUp 类定义处理 Alarm 事件的 AlarmRang 方法。AlarmDriver 类一起使用类,将使用 WakeMeUp 的 AlarmRang 方法设置为处理 AlarmClock 的 Alarm 事件。
该示例程序使用事件和委托和引发事件中详细说明的概念。
示例
' EventSample.vb.
'
Option Explicit
Option Strict
Imports System
Imports System.ComponentModel
Imports Microsoft.VisualBasic
Namespace EventSample
' Class that contains the data for
' the alarm event. Derives from System.EventArgs.
'
Public Class AlarmEventArgs
Inherits EventArgs
Private _snoozePressed As Boolean
Private nrings As Integer
'Constructor.
'
Public Sub New(snoozePressed As Boolean, nrings As Integer)
Me._snoozePressed = snoozePressed
Me.nrings = nrings
End Sub
' The NumRings property returns the number of rings
' that the alarm clock has sounded when the alarm event
' is generated.
'
Public ReadOnly Property NumRings() As Integer
Get
Return nrings
End Get
End Property
' The SnoozePressed property indicates whether the snooze
' button is pressed on the alarm when the alarm event is generated.
'
Public ReadOnly Property SnoozePressed() As Boolean
Get
Return _snoozePressed
End Get
End Property
' The AlarmText property that contains the wake-up message.
'
Public ReadOnly Property AlarmText() As String
Get
If _snoozePressed Then
Return "Wake Up!!! Snooze time is over."
Else
Return "Wake Up!"
End If
End Get
End Property
End Class
' Delegate declaration.
'
Public Delegate Sub AlarmEventHandler(sender As Object, _
e As AlarmEventArgs)
' The Alarm class that raises the alarm event.
'
Public Class AlarmClock
Private _snoozePressed As Boolean = False
Private nrings As Integer = 0
Private stopFlag As Boolean = False
' The Stop property indicates whether the
' alarm should be turned off.
'
Public Property [Stop]() As Boolean
Get
Return stopFlag
End Get
Set
stopFlag = value
End Set
End Property
' The SnoozePressed property indicates whether the snooze
' button is pressed on the alarm when the alarm event is generated.
'
Public Property SnoozePressed() As Boolean
Get
Return _snoozePressed
End Get
Set
_snoozePressed = value
End Set
End Property
' The event member that is of type AlarmEventHandler.
'
Public Event Alarm As AlarmEventHandler
' The protected OnAlarm method raises the event by invoking
' the delegates. The sender is always this, the current instance
' of the class.
'
Protected Overridable Sub OnAlarm(e As AlarmEventArgs)
RaiseEvent Alarm(Me, e)
End Sub
' This alarm clock does not have
' a user interface.
' To simulate the alarm mechanism it has a loop
' that raises the alarm event at every iteration
' with a time delay of 300 milliseconds,
' if snooze is not pressed. If snooze is pressed,
' the time delay is 1000 milliseconds.
'
Public Sub Start()
Do
nrings += 1
If stopFlag Then
Exit Do
Else
If _snoozePressed Then
System.Threading.Thread.Sleep(1000)
If (True) Then
Dim e As New AlarmEventArgs(_snoozePressed, nrings)
OnAlarm(e)
End If
Else
System.Threading.Thread.Sleep(300)
Dim e As New AlarmEventArgs(_snoozePressed, nrings)
OnAlarm(e)
End If
End If
Loop
End Sub
End Class
' The WakeMeUp class has a method AlarmRang that handles the
' alarm event.
'
Public Class WakeMeUp
Public Sub AlarmRang(sender As Object, e As AlarmEventArgs)
Console.WriteLine((e.AlarmText + ControlChars.Cr))
If Not e.SnoozePressed Then
If e.NumRings Mod 10 = 0 Then
Console.WriteLine(" Let alarm ring? Enter Y")
Console.WriteLine(" Press Snooze? Enter N")
Console.WriteLine(" Stop Alarm? Enter Q")
Dim input As String = Console.ReadLine()
If input.Equals("Y") Or input.Equals("y") Then
Return
Else
If input.Equals("N") Or input.Equals("n") Then
CType(sender, AlarmClock).SnoozePressed = True
Return
Else
CType(sender, AlarmClock).Stop = True
Return
End If
End If
End If
Else
Console.WriteLine(" Let alarm ring? Enter Y")
Console.WriteLine(" Stop Alarm? Enter Q")
Dim input As String = Console.ReadLine()
If input.Equals("Y") Or input.Equals("y") Then
Return
Else
CType(sender, AlarmClock).Stop = True
Return
End If
End If
End Sub
End Class
' The driver class that hooks up the event handling method of
' WakeMeUp to the alarm event of an Alarm object using a delegate.
' In a forms-based application, the driver class is the
' form.
'
Public Class AlarmDriver
Public Shared Sub Main()
' Instantiates the event receiver.
Dim w As New WakeMeUp()
' Instantiates the event source.
Dim clock As New AlarmClock()
' Wires the AlarmRang method to the Alarm event.
AddHandler clock.Alarm, AddressOf w.AlarmRang
clock.Start()
End Sub
End Class
End Namespace
Q2: 400分求vb.net,求一个socket的编程实例,详细见问题补充:
至少需要
Imports System
Imports System.Net
Imports System.Net.Sockets
Imports System.Threading
Imports System.Text
还要了解异步与委托
简单点的话
sub tcplisten() '监听过程
Const LPort As Integer = 6850 '本地监听端口
Dim IPadd As IPAddress = Dns.GetHostEntry(Dns.GetHostName()).AddressList(1) ’本地IP
'addresslist里面包括IPV6和IPV4
Dim IPEP As New IPEndPoint(IPadd, LPort) '结点
TCPL = New TcpListener(IPEP) ’建立监听实例
TCPL.Start() '开始监听
If TCPL.Pending = True Then '如果有连接接入
Dim TCPLX As New Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp) '建立一个新的SOCKET
TCPLX=TCPL.Accept() ’将第一个接入挂起的连接传给新建SOCKET,因为监听的SOCKET还要继续监听
dim Ripep as new ipendpoint=TCPLX.RemoteEndPoint '此时获得远程的ip与端口号
listbox.add(ripep.ip)
dim buff as byte()
buff=TCPLX.Receive() 'listenacceptrecieve这时候可以接受数据了
dim s as string=Encoding.Default.GetString(buff) '转换成文本
msg "s"
TCPLX.send(buff) '这个是发送
end if
End Sub
长时间没写,可能有点小错误,你自己调试调试,大概步骤就是listenacceptrecieve,最好看看msdn,那个学起来才系统
Q3: 如何使用VB.NET FileInfo类对文件操作
在网上收到一些关于文件操作的列子,现在和大家分享一下,以下的示例代码将向您展示如何使用VB.NET FileInfo来拷贝、移动和删除文件,以及如何使用DirectoryInfo来移动和删除文件夹。(注意:为了运行这些示例,您需要将这条语句:Imports System.IO,添加到您的表单或模块的最顶部。)
VB.NET FileInfo示例 拷贝一个文件 1. Dim fFile1 As New FileInfo("C:abc1.txt")2.3. fFile1.CopyTo("C:abc2.txt", True)4. 我们将fFile1变量定义为一个FileInfo对象并设定它指向文件系统中的一个目录,为了拷贝一个文件,我们使用FileInfo对象中的CopyTo方法并指明我们计划要拷贝的目标文件的全名。 移动一个文件 1. Dim fFile1 As New FileInfo("C:abc1.txt")2.3. fFile1.MoveTo("C:abc3.txt")4. 我们将fFile1变量定义为一个FileInfo对象并设定它指向文件系统中的一个目录,为了拷贝一个文件,我们使用FileInfo对象中的CopyTo方法并指明我们计划要移动的目标文件的全名。 删除一个文件 1. Dim fFile1 As New FileInfo("C:abc1.txt")2.3. fFile1.Delete()4. 我们将fFile1变量定义为一个FileInfo对象并设定它指向文件系统中的一个目录,为了删除一个文件,我们使用FileInfo对象中的Delete方法。 VB.NET FileInfo DirectoryInfo示例 移动一个文件夹 1. Dim dDir1 As New DirectoryInfo("C:Folder1")2.3. dDir1.MoveTo("C:Folder2")4. 我们将dDir1变量定义为一个DirectoryInfo对象并设定它指向文件系统中的一个目录,为了移动一个文件夹,我们使用DirectoryInfo对象的MoveTo方法,并指明我们所移动的文件夹的完整目标路径。 删除一个文件夹 1. Dim dDir1 As New DirectoryInfo("C:Folder1")2.3. dDir1.Delete()4. 我们将dDir1变量定义为一个DirectoryInfo对象并设定它指向文件系统中的一个目录,为了删除一个文件夹,我们使用DirectoryInfo对象的Delete方法。) 作者:未知 来源:网络
Q4: vb.net中创建类
不熟悉VB,如有不妥的地方请包涵!
Public Class Stack
Dim aryData() As Integer
Sub New(ByVal Num As Integer)
Dim aryData(Num) As Integer
End Sub
Function Pop() As Integer
If (aryData.Length = 0) Then
Return 0
Else
Dim a As Integer
a = aryData(aryData.Length)
aryData(aryData.Length) = Convert.ToInt32(DBNull.Value)
Return a
End If
End Function
Sub Push(ByVal n As Integer)
For Each i As Integer In aryData
If (aryData(i) = Convert.ToInt32(DBNull.Value)) Then
aryData(i) = n
End
Else
Continue For
End If
Next
End Sub
Sub PrintStack()
For Each i As Integer In aryData
If (aryData(i) = Convert.ToInt32(DBNull.Value)) Then
End
Else
Print(aryData(i))
End If
Next
End Sub
End Class
Q5: VB中控件数组在VB.NET中用法,请给一个例子
VB.net中没有控件数组vb.net类的使用案例的说法。
替代方法vb.net类的使用案例:
·创建一个控件的类型数组: Button[]vb.net类的使用案例,将所有的button控件创建好后存进去,可以遍历它;
·或将所有要遍历的控件放在一个容器如Panel中,以后遍历这个容器的子控件即可。
----------
你的情况,推荐放在容器中。比如下面就是一个遍历容器的控件,然后找出所有的文本框并修改内容的程序:
//pn 是个 Panel 控件
foreach (Control item in pn.Controls)
{
if (typeof(TextBox) == item.GetType())
{
((TextBox)item).Text = "vb.net类的使用案例我是动态修改的!";
}
}
vb.net类的使用案例的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于、vb.net类的使用案例的信息别忘了在本站进行查找喔。







