VB dll 中需要声明

发布于 2024-10-13 09:27:22 字数 1389 浏览 1 评论 0原文

首先感谢大家....

我目前正在做VB。我正在使用 Visual Studio 2008。

下面的代码是一个控制台应用程序,构建时没有任何错误。

    Imports System.Net


Module Module1

    Public Sub Main()

        Dim address As IPAddress
        Dim remoteIP As System.Net.IPEndPoint
        Dim socketAddress As System.Net.SocketAddress

        Try
            address = IPAddress.Parse("192.168.0.187")
            remoteIP = New System.Net.IPEndPoint(address, 0)

            socketAddress = remoteIP.Serialize()

            Console.WriteLine("Address Family :" & remoteIP.AddressFamily.ToString())
            Console.WriteLine("IP :" & remoteIP.Address.ToString() & "Port :" & remoteIP.Port.ToString())
            Console.WriteLine("Socket address :" & socketAddress.ToString())


        Catch ex As Exception
            Console.WriteLine(ex.StackTrace.ToString())


        End Try

    End Sub


End Module

在下一个 dll 程序中,同样给出错误“Declaration Expected for addr、remoteIP 和 socketAddr”

Imports System.Net

Public Class Class1


End Class
Public Class ethernet

    Dim addr As IPAddress
    Dim remoteIP As System.Net.IPEndPoint
    Dim socketAddr As System.Net.SocketAddress

    addr = IPAddress.Parse("192.168.0.187")
    remoteIP = New System.Net.IPEndPoint(addr,0)

    socketAddr = remoteIP.Serialize()



End Class

有人能告诉我为什么会发生这种情况吗?

First of all thanking everyone....

I am currently working on VB. I am using Visual Studio 2008.

The piece of code below is a console application which builds without any error.

    Imports System.Net


Module Module1

    Public Sub Main()

        Dim address As IPAddress
        Dim remoteIP As System.Net.IPEndPoint
        Dim socketAddress As System.Net.SocketAddress

        Try
            address = IPAddress.Parse("192.168.0.187")
            remoteIP = New System.Net.IPEndPoint(address, 0)

            socketAddress = remoteIP.Serialize()

            Console.WriteLine("Address Family :" & remoteIP.AddressFamily.ToString())
            Console.WriteLine("IP :" & remoteIP.Address.ToString() & "Port :" & remoteIP.Port.ToString())
            Console.WriteLine("Socket address :" & socketAddress.ToString())


        Catch ex As Exception
            Console.WriteLine(ex.StackTrace.ToString())


        End Try

    End Sub


End Module

In the next program which is a dll the same gives error saying "Declaration Expected for addr, remoteIP and socketAddr"

Imports System.Net

Public Class Class1


End Class
Public Class ethernet

    Dim addr As IPAddress
    Dim remoteIP As System.Net.IPEndPoint
    Dim socketAddr As System.Net.SocketAddress

    addr = IPAddress.Parse("192.168.0.187")
    remoteIP = New System.Net.IPEndPoint(addr,0)

    socketAddr = remoteIP.Serialize()



End Class

Can anybody tell me why is this happening...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终难遇 2024-10-20 09:27:22

第二个类 ethernet 中的代码不包含在方法中,因此您仅声明 addr、remoteIPsocketAddr 变量。

要实现这一点,只需将代码放入方法中,例如:

Public Class ethernet
    Public Function SerializeSocket(address As String) As System.Net.SocketAddress
        Dim addr As IPAddress = IPAddress.Parse("192.168.0.187")
        Dim remoteIP As System.Net.IPEndPoint = New System.Net.IPEndPoint(addr,0)
        Return remoteIP.Serialize()
    End Sub
End Class

Your code in the second class ethernet is not contained within a Method, therefore you are only declaring the addr, remoteIP and socketAddr variables.

To make that work just put the code in a method, like:

Public Class ethernet
    Public Function SerializeSocket(address As String) As System.Net.SocketAddress
        Dim addr As IPAddress = IPAddress.Parse("192.168.0.187")
        Dim remoteIP As System.Net.IPEndPoint = New System.Net.IPEndPoint(addr,0)
        Return remoteIP.Serialize()
    End Sub
End Class
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文