0

I'm trying to create a tool for users on our network to report their PC hostnames and IP addresses to the IT department. For assistance, we have a warehouse department that loves to shuffle around PCs and users, so I can't reliably tell who's on what PC. This is intended to be a quick way to stop them from saying "Jim's PC" and get them to give me useful information like "WAREHOUSE_WINXP_4".

I've created what I think should be a working program, and while it compiles and executes without errors, I can't seem to get the text box contents to set properly. The program is super-simple--just a form with 2 text boxes.

(Note: I'm more of a sysadmin/netadmin/infosec specialist, so I'm probably making some id10t mistakes here, but I'm kinda out of ideas on my own.)

Public Class Form1
    Dim strHostname As String
    Dim strIPAddress As String

    Public Sub getHostname()
        strHostname = System.Net.Dns.GetHostName()
        'txtHostname.Text = strHostname  Apparently putting it here won't work.  In Load() maybe?
    End Sub

    Public Sub getIPAddress()
        strIPAddress = System.Net.Dns.GetHostEntry(strHostname).AddressList(0).ToString()
        'txtIPAddress.Text = strIPAddress Apparently putting it here won't work.  In Load() maybe?
    End Sub

    Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        getIPAddress()
        getHostname()
        txtHostname.Text = strHostname
        txtIPAddress.Text = strIPAddress
    End Sub
End Class
Steven Doggart
  • 43,358
  • 8
  • 68
  • 105

3 Answers3

2

As others have mentioned, you are calling the methods in the wrong order. Your getIPAddress method uses the strHostname variable. The strHostname variable is set by the getHostname method. Therefore, you need to call getHostname first, before you can call getIPAddress.

However, the real problem is that you have designed your code in a way where it is possible to call the methods in the wrong order. It's always best to not have hidden rules like that which can easily cause bugs. For instance, if you wrote the code like this, the order in which you call the methods wouldn't matter anymore:

Imports System.Net

Public Class Form1
    Public Function GetHostName() As String
        Return Dns.GetHostName()
    End Sub

    Public Function GetIpAddress() As String
        Return Dns.GetHostEntry(GetHostname()).AddressList(0).ToString()
    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        txtHostname.Text = GetHostName()
        txtIPAddress.Text = GetIpAddress()
    End Sub
End Class
Steven Doggart
  • 43,358
  • 8
  • 68
  • 105
  • Confession time: the assignment statement inside GetIPAddress() was copypasta'd from another advice thread...I think here on Stackoverflow. It's a happy coincidence that it would work at all, as I just now noticed that strHostname was the same variable. I'll try these suggestions and let you all know what happens. Thanks VERY much for helping out a struggling novice coder - that goes for all of you. – CelticWhisper Jan 23 '13 at 18:20
0

You are setting the value of the global variable strHostname in the call to getHostname, but you are using that variable in the call to getIPAddress. However, you are calling getIPAddress before you call getHostName. Reverse the order of the calls and it should work.

Better yet, you might reconsider your use of global variables to prevent dependencies like this. A function parameter would work in a more easily debuggable manner. You could use System.Net.Dns.GetHostEntry(strHostname).AddressList(0).ToString() as a parameter to getIPAddress.

Steven Doggart
  • 43,358
  • 8
  • 68
  • 105
maelstrom
  • 1,111
  • 8
  • 7
0

Change the order of your getIPAddress and getHostname method calls. You are using the strHostname value inside the getIPAddress method, but it hasn't been assigned a value yet.

Steven Doggart
  • 43,358
  • 8
  • 68
  • 105
r91087
  • 1