0

Trying to resolve getting IP from host name but I get this warning on RetIP: (on Return RetIP line) "Variable 'RetIP' is used before it has been assigned a value. A null reference exception could result at runtime."

Public Function GetIPFromHostname(ByRef Host As String) As System.Net.IPAddress()
    Dim IP As System.Net.IPHostEntry
    'Dim host As System.Net.IPHostEntry
    Dim RetIP As System.Net.IPAddress()
    Try
        'Host = System.Net.Dns.GetHostByAddress(IP)
        IP = System.Net.Dns.GetHostEntry(Host)
        RetIP = IP.AddressList()
    Catch ex As Exception
        Console.WriteLine("Cannot resolve IP address from hostname ({0}) - {1}", Host, ex.Message)
    End Try

    Return RetIP
End Function

This code used to work in VB 2008 and 2010 but now doesn't work. Any idea what I'm missing here?

4

1 回答 1

0

警告是完全正确的;如果你在 2008 年没有得到它,那是因为微软修复了一个错误。

如果抛出异常,则不会设置该变量。

于 2013-03-04T22:38:16.550 回答