thx0125
thx0125

Reputation: 127

What's the difference between these two ways to code in vb .Net

I have this code

    Dim oCliente As New Net.WebClient()
    Dim bHTML As Byte() = oCliente.DownloadData(Me.tbURL.Text)
    Dim oUTF8 As New UTF8Encoding()
    Me.tbShowArea.Text = oUTF8.GetString(bHTML)

and this one

Me.tbShowArea.Text = UTF8Encoding.UTF8.GetString(New Net.WebClient().DownloadData(Me.tbURL.Text))

They both do the same thing. What I want to know is, what are the pros and the cons of using either?

Upvotes: 1

Views: 49

Answers (4)

Enigmativity
Enigmativity

Reputation: 117175

A very important aspect missing in both sets of code is the fact that WebClient implements IDisposable so it must be disposed of after use by calling .Dispose(). This is to prevent memory leaks from resources that do not get de-allocated. Remember the garbage collector does not call .Dispose() for you. You must explicitly call it in your code.

The first block of code can be written as:

Dim oCliente As New Net.WebClient()
Dim bHTML As Byte() = oCliente.DownloadData(Me.tbURL.Text)
Dim oUTF8 As New UTF8Encoding()
Me.tbShowArea.Text = oUTF8.GetString(bHTML)
oCliente.Dispose()

Or, even better, using a Using as:

Using oCliente As New Net.WebClient()
    Dim bHTML As Byte() = oCliente.DownloadData("Me.tbURL.Text")
    Dim oUTF8 As New UTF8Encoding()
    Dim Me_tbShowArea_Text = oUTF8.GetString(bHTML)
End Using

The second block of code is unable to be written to be able to call .Dispose() so it should be avoided.

Upvotes: 2

Racil Hilan
Racil Hilan

Reputation: 25371

Basically, the only difference is that the second one is chaining the functions so each one is getting the result of the previous one processing it and passing it to the next one, while the in first one each function is assigning the result to a variable.

There is no real difference in your code, but in other scenarios you may need to do more processing on the same result, in which case you use the variable. Let's say you want the WebClient to do more than one thing. Using the first, you can do something like:

Dim oCliente As New Net.WebClient()
Dim bHTML As Byte() = oCliente.DownloadData(Me.tbURL.Text)
Dim oUTF8 As New UTF8Encoding()
Me.tbShowArea.Text = oUTF8.GetString(bHTML)

bHTML = oCliente.DownloadData(Me.tbURL2.Text)
Me.tbShowArea2.Text = oUTF8.GetString(bHTML)

As you can see, we reused the variables to do the additional task. You cannot do that in the second way, you will have to repeat the whole thing:

Me.tbShowArea.Text = UTF8Encoding.UTF8.GetString(New Net.WebClient().DownloadData(Me.tbURL.Text))

Me.tbShowArea2.Text = UTF8Encoding.UTF8.GetString(New Net.WebClient().DownloadData(Me.tbURL2.Text))

Now imagine that you have to put this in a loop for example. You can only do that with the first way.

Upvotes: 0

Slava Knyazev
Slava Knyazev

Reputation: 6073

There is no advantage in using the second apart from maybe barely reduce project size and/or make a part if the code which you know you won't touch again more compact.

That's it.

As for cons it makes debugging and tweaking far more difficult than it should be.

Upvotes: 0

OneFineDay
OneFineDay

Reputation: 9024

If your looking for one line commands then go with a function that returns that value, but leaves the ability for debugging with development.

Function:

Private Function GetOnlineData(url As String) As String
  Dim oCliente As New Net.WebClient()
  Dim bHTML As Byte() = oCliente.DownloadData(url)
  Dim oUTF8 As New UTF8Encoding()
  Return oUTF8.GetString(bHTML)
End Function

Usage:

Me.tbShowArea.Text = GetOnlineData(Me.tbURL.Text)

Upvotes: 2

Related Questions