I have created a project of fetching data from a Url in VB.NET. Now I want that code to called in function Get data() in the class.
How can I do that??
PLZ help soon
:icon_rolleyes:

Dani AI

Generated

Short answer: put the URL-fetch code inside a reusable class (or a Module if you really want a global function) and expose a public method GetData(url As String) or an async GetDataAsync(url As String). That answers 's module vs class question and builds on 's suggestion to create a class file for a generic routine.

A minimal synchronous example using WebClient (simple, available in older frameworks):

Public Class WebFetcher
    Public Shared Function GetData(url As String) As String
        Using wc As New System.Net.WebClient()
            wc.Encoding = System.Text.Encoding.UTF8
            Return wc.DownloadString(url)
        End Using
    End Function
End Class

Call it directly:

Dim html As String = WebFetcher.GetData("http://example.com")

A modern, non-blocking approach uses HttpClient and async/await (preferred for UI/ASP.NET):

Imports System.Net.Http
Imports System.Threading.Tasks

Public Class WebFetcherAsync
    Private Shared ReadOnly client As New HttpClient()

    Public Shared Async Function GetDataAsync(url As String) As Task(Of String)
        Dim resp = Await client.GetAsync(url)
        resp.EnsureSuccessStatusCode()
        Return Await resp.Content.ReadAsStringAsync()
    End Function
End Class

Call from an Async method: Dim html As String = Await WebFetcherAsync.GetDataAsync("https://example.com")

Troubleshooting notes: wrap calls in Try/Catch and handle WebException/HttpRequestException; watch encoding and timeouts; avoid creating many HttpClient instances (use a shared singleton); do not block the UI thread—use async or background work. For more details on the APIs see the WebClient and HttpClient docs: WebClient class (System.Net) and HttpClient class (System.Net.Http).

Recommended Answers

All 2 Replies

Hi,

I didn't get it you want to place the function in a module or calling it from there??

Please explain your self. If you want the Get_Data() function to be a generic public function then you can create a class file and place the code there.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.