我正在尝试将某个变量(dfgt
)设置为某人计算机的默认网关。单击按钮后,通过调用函数(getDF
)设置变量。到目前为止,我已经找到了如何获取值并输出它,但每当我尝试将变量设置为返回的结果时,我都会收到错误
An unhandled exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll
Additional information: Conversion from string "192.168.1.254" to type 'Integer' is not valid.
我的功能如下
Private Function getDG() As Integer
For Each adapter As NetworkInterface In myNetworkAdapters
myAdapterProps = adapter.GetIPProperties
myGateways = myAdapterProps.GatewayAddresses
For Each Gateway As GatewayIPAddressInformation In myGateways
'MessageBox.Show(Gateway.Address.ToString)
Return Gateway.Address.ToString
Next
Next
Return True
End Function
这是我设置变量
的方法Private Sub Unblock_Click(sender As Object, e As EventArgs) Handles Unblock.Click
Dim dfgt As String
dfgt = getDG()
End Sub
我是.NET的新手,如果我没有向您提供足够的信息,我会提前道歉。
答案 0 :(得分:1)
你声明这样的函数:
私人功能getDG() As Integer
但尝试返回这样的值:
返回Gateway.Address。 ToString
在这里查看问题?