VB.net对非共享成员的引用需要对象引用

时间:2015-07-30 02:23:51

标签: vb.net

因此,在对程序进行编程的过程中,我经过大约6个小时的研究和摆弄后才陷入困境。我无法解决此错误。

Public Class Main




Private Sub txt1_TextChanged(sender As Object, e As EventArgs) Handles mstag.TextChanged

End Sub

Private Sub Label1_Click(sender As Object, e As EventArgs) Handles lbl1.Click

End Sub

Private Sub Main_Load(sender As Object, e As EventArgs) Handles MyBase.Load

End Sub

Private Sub devicepage_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles devicepage.DocumentCompleted

End Sub
Public Class myform

    Private SIS As System.Net.Sockets.TcpClient
    Private netdata As NetworkStream
    Private Sub Run_Click(sender As Object, e As EventArgs) Handles Run.Click


        Dim msnumber As String = mstag.Text

        'Ping Code

        Try
            Dim ping As Ping = New Ping()
            Dim ping2 As Ping = New Ping()
            Dim ping3 As Ping = New Ping()
            Dim ping4 As Ping = New Ping()
            Dim pingreply As PingReply = ping.Send(mstag.Text + ".regent.edu")
            Dim pingreply2 As PingReply = ping2.Send(mstag.Text + ".regent.edu")
            Dim pingreply3 As PingReply = ping3.Send(mstag.Text + ".regent.edu")
            Dim pingreply4 As PingReply = ping4.Send(mstag.Text + ".regent.edu")
            Dim avgadd As Integer = pingreply.RoundtripTime + pingreply2.RoundtripTime + pingreply3.RoundtripTime + pingreply4.RoundtripTime
            Dim avg As Integer = avgadd / 4
            pingresults.Text &= "Ping Time: " & pingreply.RoundtripTime & "ms" & vbNewLine
            pingresults.Text &= "Ping Time: " & pingreply2.RoundtripTime & "ms" & vbNewLine
            pingresults.Text &= "Ping Time: " & pingreply3.RoundtripTime & "ms" & vbNewLine
            pingresults.Text &= "Ping Time: " & pingreply4.RoundtripTime & "ms" & vbNewLine
            pingresults.Text &= "Average Time: " & (avg) & "ms"

        Catch err As Exception
            pingresults.Text = "Ping Timeout"
        End Try


        'NSLOOKUP Code

        Try
            Dim p As New Process
            With p.StartInfo
                .FileName = "nslookup.exe"
                .Arguments = mstag.Text
                .CreateNoWindow = True
                .RedirectStandardOutput = True
                .RedirectStandardError = True
                .UseShellExecute = False
            End With

            p.Start()
            If p.WaitForExit(5000) Then
                'Read Output
                Dim reader As StreamReader = p.StandardOutput
                Dim output As String = reader.ReadToEnd()
                nslookupresults.Text = output

            Else
                'Failed
                nslookupresults.Text = ("Failed: Timeout")
            End If

        Catch err As Exception
            nslookupresults.Text = "Timeout. :("
        End Try

        'Telnet

        SIS = New System.Net.Sockets.TcpClient
        SIS.Connect(mstag.Text, 23)
        netdata = SIS.GetStream()

        If netdata.CanWrite And netdata.CanRead Then
            Dim bytes(SIS.ReceiveBufferSize) As Byte
            netdata.Read(bytes, 0, CInt(SIS.ReceiveBufferSize))
            Dim returndata As String = Encoding.ASCII.GetString(bytes)
            telnetout.Text = returndata
        End If

    End Sub


    Public Sub sissend_Click(sender As Object, e As EventArgs) Handles sissend.Click


        Dim myWriteBuffer As Byte() = Encoding.ASCII.GetBytes(telnetinput.Text)
        netdata.Write(myWriteBuffer, 0, myWriteBuffer.Length)

        Dim bytes(SIS.ReceiveBufferSize) As Byte
        netdata.Read(bytes, 0, CInt(SIS.ReceiveBufferSize))
        Dim returndata As String = Encoding.ASCII.GetString(bytes)
        telnetout.Text = returndata
    End Sub
End Class

加星标/加粗是有问题的作品。

已更新为整个项目的最新代码。

1 个答案:

答案 0 :(得分:1)

您最好永远不要为变量指定代码可访问的类的名称。

此代码块已根据评论进行更新。

 Public Sub Run_Click(sender As Object, e As EventArgs) Handles Run.Click
    'Telnet

    Dim MYtcpClient As New System.Net.Sockets.TcpClient
    MytcpClient.Connect(mstag.Text, 23)
    Dim MyNetworkStream As NetworkStream = MYtcpClient.GetStream()


    If MyNetworkStream.CanWrite And MyNetworkStream.CanRead Then
        Dim bytes(MYtcpClient.ReceiveBufferSize) As Byte
        MyNetworkStream.Read(bytes, 0, CInt(MYtcpClient.ReceiveBufferSize))
        Dim returndata As String = Encoding.ASCII.GetString(bytes)
        telnetout.Text = returndata
    End If

End Sub

此外,下面的代码没有Networkstream或tcpclient的新实例,并且正在尝试直接引用该类。当我第一次发布答案时,我错过了。您需要为每个实例创建一个新实例。

Public Sub sissend_Click(sender As Object, e As EventArgs) Handles sissend.Click

 Dim MYtcpClient As New System.Net.Sockets.TcpClient
    MytcpClient.Connect(mstag.Text, 23)
    Dim MyNetworkStream As new NetworkStream
    MyNetworkStream.Write(myWriteBuffer, 0, myWriteBuffer.Length)

    Dim bytes(MYtcpClient.ReceiveBufferSize) As Byte
    MyNetworkStream.Read(bytes, 0, CInt(MYtcpClient.ReceiveBufferSize))
    Dim returndata As String = Encoding.ASCII.GetString(bytes)
    telnetout.Text = returndata
End Sub

对于长期连接的评论

Public Class myForm
   Private MYtcpClient As System.Net.Sockets.TcpClient
   Private MyNetworkStream As NetworkStream
   Public Sub Run_Click(sender As Object, e As EventArgs) Handles Run.Click
    'Telnet

    MYtcpClient = New System.Net.Sockets.TcpClient
    MytcpClient.Connect(mstag.Text, 23)
    MyNetworkStream = MYtcpClient.GetStream()


    If MyNetworkStream.CanWrite And MyNetworkStream.CanRead Then
        Dim bytes(MYtcpClient.ReceiveBufferSize) As Byte
        MyNetworkStream.Read(bytes, 0, CInt(MYtcpClient.ReceiveBufferSize))
        Dim returndata As String = Encoding.ASCII.GetString(bytes)
        telnetout.Text = returndata
    End If

End Sub

Public Sub sissend_Click(sender As Object, e As EventArgs) Handles sissend.Click

 MyNetworkStream.Write(myWriteBuffer, 0, myWriteBuffer.Length)

    Dim bytes(MYtcpClient.ReceiveBufferSize) As Byte
    MyNetworkStream.Read(bytes, 0, CInt(MYtcpClient.ReceiveBufferSize))
    Dim returndata As String = Encoding.ASCII.GetString(bytes)
    telnetout.Text = returndata
End Sub
End Class

删除了公共类Myform

Public Class Main




Private Sub txt1_TextChanged(sender As Object, e As EventArgs) Handles mstag.TextChanged

End Sub

Private Sub Label1_Click(sender As Object, e As EventArgs) Handles lbl1.Click

End Sub

Private Sub Main_Load(sender As Object, e As EventArgs) Handles MyBase.Load

End Sub

Private Sub devicepage_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles devicepage.DocumentCompleted

End Sub


    Private SIS As System.Net.Sockets.TcpClient
    Private netdata As NetworkStream
    Private Sub Run_Click(sender As Object, e As EventArgs) Handles Run.Click


        Dim msnumber As String = mstag.Text

        'Ping Code

        Try
            Dim ping As Ping = New Ping()
            Dim ping2 As Ping = New Ping()
            Dim ping3 As Ping = New Ping()
            Dim ping4 As Ping = New Ping()
            Dim pingreply As PingReply = ping.Send(mstag.Text + ".regent.edu")
            Dim pingreply2 As PingReply = ping2.Send(mstag.Text + ".regent.edu")
            Dim pingreply3 As PingReply = ping3.Send(mstag.Text + ".regent.edu")
            Dim pingreply4 As PingReply = ping4.Send(mstag.Text + ".regent.edu")
            Dim avgadd As Integer = pingreply.RoundtripTime + pingreply2.RoundtripTime + pingreply3.RoundtripTime + pingreply4.RoundtripTime
            Dim avg As Integer = avgadd / 4
            pingresults.Text &= "Ping Time: " & pingreply.RoundtripTime & "ms" & vbNewLine
            pingresults.Text &= "Ping Time: " & pingreply2.RoundtripTime & "ms" & vbNewLine
            pingresults.Text &= "Ping Time: " & pingreply3.RoundtripTime & "ms" & vbNewLine
            pingresults.Text &= "Ping Time: " & pingreply4.RoundtripTime & "ms" & vbNewLine
            pingresults.Text &= "Average Time: " & (avg) & "ms"

        Catch err As Exception
            pingresults.Text = "Ping Timeout"
        End Try


        'NSLOOKUP Code

        Try
            Dim p As New Process
            With p.StartInfo
                .FileName = "nslookup.exe"
                .Arguments = mstag.Text
                .CreateNoWindow = True
                .RedirectStandardOutput = True
                .RedirectStandardError = True
                .UseShellExecute = False
            End With

            p.Start()
            If p.WaitForExit(5000) Then
                'Read Output
                Dim reader As StreamReader = p.StandardOutput
                Dim output As String = reader.ReadToEnd()
                nslookupresults.Text = output

            Else
                'Failed
                nslookupresults.Text = ("Failed: Timeout")
            End If

        Catch err As Exception
            nslookupresults.Text = "Timeout. :("
        End Try

        'Telnet

        SIS = New System.Net.Sockets.TcpClient
        SIS.Connect(mstag.Text, 23)
        netdata = SIS.GetStream()

        If netdata.CanWrite And netdata.CanRead Then
            Dim bytes(SIS.ReceiveBufferSize) As Byte
            netdata.Read(bytes, 0, CInt(SIS.ReceiveBufferSize))
            Dim returndata As String = Encoding.ASCII.GetString(bytes)
            telnetout.Text = returndata
        End If

    End Sub


    Public Sub sissend_Click(sender As Object, e As EventArgs) Handles sissend.Click


        Dim myWriteBuffer As Byte() = Encoding.ASCII.GetBytes(telnetinput.Text)
        netdata.Write(myWriteBuffer, 0, myWriteBuffer.Length)

        Dim bytes(SIS.ReceiveBufferSize) As Byte
        netdata.Read(bytes, 0, CInt(SIS.ReceiveBufferSize))
        Dim returndata As String = Encoding.ASCII.GetString(bytes)
        telnetout.Text = returndata
    End Sub
End Class