The importance of classes & when to use one.

Johnson

Well-known member
Joined
Mar 6, 2009
Messages
158
Programming Experience
Beginner
Because i am really new to vb i have always just bundled code into my main form. I am trying to tidy this up now and start using classes. But when should you use a class?

For example. Say we have a function thats called by a button.

VB.NET:
    Private Function UrlIsValid(ByVal url As String) As Boolean
        Dim is_valid As Boolean = False
        If url.ToLower().StartsWith("www.") Then url = "http://" & url
        Dim web_response As HttpWebResponse = Nothing
        Try
            Dim web_request As HttpWebRequest = _
                HttpWebRequest.Create(url)
            web_response = DirectCast(web_request.GetResponse(), HttpWebResponse)
            Return True
        Catch ex As Exception
            Return False
        Finally
            If Not (web_response Is Nothing) Then web_response.Close()
        End Try
    End Function

this is more clutter to the main form, so would i add this to a class? or am i going about this all wrong?

If the answer is yes. Then im not quite sure how you call a class function.

How would i have this function in a class and be able to call it from formMain?
 
The purpose of a class is to represent an object. Sometimes you add Shared methods to a class for utilities that is not tied to a specific object instance. Understanding Classes
#Region Directive can also be used to organize collapsible sections of code.

About the function, if you don't intend to use the url content only request the headers:
VB.NET:
req.Method = Net.WebRequestMethods.Http.Head
 
Back
Top