Conversion from string to type 'Boolean' problem

danfloun

Member
Joined
Apr 5, 2010
Messages
23
Programming Experience
Beginner
Hi,

I've got a routine that checks a website my my external address.
What I'd like to do is loop through the string just to get the first 15 numbers and full stops (periods).

But that isn't my question, my problem is I've got this routine that isn't building because of a string conversion issue.

I don't know how to correct it.

The error is;
VB.NET:
Conversion from string "121.22.98.22" to type 'Boolean' is not valid.

VB.NET:
    Sub CheckIP()
        Dim URL As String = "http://whatismyip.com/automation/n09230945.asp"
        Dim client As WebClient = New WebClient()
        Dim data As Stream = client.OpenRead(URL)
        Dim reader As StreamReader = New StreamReader(data)
        Do While reader.ReadToEnd()
            lblGmip.Text = reader.ReadToEnd()
        Loop
        data.Close()
        reader.Close()
    End Sub

I know that's a simple issue to fix, but it's nearly 4am in the morning and this coding stuff is ruining my life..... and I've only just started learning :mad:

Cheers
Danny
 
This your code:
VB.NET:
        Do While [COLOR="Red"]reader.ReadToEnd()[/COLOR]
            lblGmip.Text = [COLOR="Blue"]reader.ReadToEnd()[/COLOR]
        Loop
The blue one is correct. It reads the file to the end and displays the result in a TextBox. Obviously you expect ReadToEnd to return a String. That being the case, what's the red one for? You are telling the code to Do something While some condition is True. That condition, according to your code, is the String returned by ReadToEnd. How can a String be True or False?

Also, if you're reading the entire file in one go, which ReadToEnd does, what's the loop for at all?
 
This your code:
VB.NET:
        Do While [COLOR="Red"]reader.ReadToEnd()[/COLOR]
            lblGmip.Text = [COLOR="Blue"]reader.ReadToEnd()[/COLOR]
        Loop
The blue one is correct. It reads the file to the end and displays the result in a TextBox. Obviously you expect ReadToEnd to return a String. That being the case, what's the red one for? You are telling the code to Do something While some condition is True. That condition, according to your code, is the String returned by ReadToEnd. How can a String be True or False?

Also, if you're reading the entire file in one go, which ReadToEnd does, what's the loop for at all?

Oops, I don't know how that happened lol.
I apologise, I must have inadvertently pasted something completely wrong as I had code that actually worked before, that would never work, I realise that.

I've worked it out anyway.

I changed ReadToEnd to Read and specified the length of characters I wanted to receive in the buffer.
Why? The reason for doing this is one of the safeguards I'm working into the app. The website http://whatismyip.com/automation/n09230945.asp contains nothing more than your own ip address so as to allow Dynamic DNS update clients to read the ip easily into their apps.

However, if that website goes down permanently, temporarily or is redirected to another webpage, using the methods readtoend or readline would end up with a huge string in some instances and it would fetch the whole webpage.

I therefore specified the amount of characters to receive in the buffer.
I will now use some expression to check the format of the ip and make sure it is an actually address.

Here's my code now;

VB.NET:
   Sub GetIP()
        Dim URL As String = "http://whatismyip.com/automation/n09230945.asp"
        Dim client As New WebClient()
        Dim data As Stream = client.OpenRead(URL)
        Dim reader As New StreamReader(data)
        Dim buffer(15) As Char
        Do While reader.Peek() >= 0
            reader.Read(buffer, 0, buffer.Length)
            lblGmip.Text = buffer
        Loop
        data.Close()
        reader.Close()
    End Sub

Thanks for looking anyway.

Danny
 
Back
Top