none
converting from a string to an ip address

    Question

  • how would i go from a string to an ip address:

    Dim ipadd As Net.IPAddress

    'i cant do this part
    ipadd = console.readline
    'the console.readline is by default a string


    how would i do this?

    Wednesday, March 14, 2007 2:14 AM

Answers

  • Did you do any validation on the code to even verify that a string was present. 

    The following works just fine taking a string such as 1.2.3.4 and using it to establish a variable of type IPAddress called hostIPAddress,

     

    Module Module1

        Sub Main()
            Try
                Dim strIP As String = Console.ReadLine
                If strIP.Trim.Length > 0 Then
                    Dim hostIPAddress As System.Net.IPAddress = System.Net.IPAddress.Parse(strIP)
                Else
                    MsgBox("No String specified")
                End If

            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End Sub

    End Module

    Wednesday, March 14, 2007 5:50 PM

All replies

  • Using the parse method.

    Something like

    Dim s as string = "1.2.3.4"

    Dim hostIPAddress As System.Net.IPAddress = System.Net.IPAddress.Parse(s)

     

     

    Wednesday, March 14, 2007 5:53 AM
  • same error:

    Error 1 Value of type 'System.Net.IPAddress' cannot be converted to 'String'.

    Wednesday, March 14, 2007 6:00 AM
  • i dont know why it says it cant go from an ipadd to a string....im not trying to do that

    it is the other way
    Wednesday, March 14, 2007 1:33 PM
  • use spottys code with yours

     

    Dim hostIPAddress As System.Net.IPAddress = System.Net.IPAddress.Parse(console.readline)

    'IPaddres         Returns a String      = error
    ipadd          =    console.readline

    Wednesday, March 14, 2007 2:58 PM
  • Hi dakota367, can you post your updated code?
    Wednesday, March 14, 2007 3:02 PM
  • Did you do any validation on the code to even verify that a string was present. 

    The following works just fine taking a string such as 1.2.3.4 and using it to establish a variable of type IPAddress called hostIPAddress,

     

    Module Module1

        Sub Main()
            Try
                Dim strIP As String = Console.ReadLine
                If strIP.Trim.Length > 0 Then
                    Dim hostIPAddress As System.Net.IPAddress = System.Net.IPAddress.Parse(strIP)
                Else
                    MsgBox("No String specified")
                End If

            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End Sub

    End Module

    Wednesday, March 14, 2007 5:50 PM