What is the constructor method in Visual Basic?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    What is the constructor method in Visual Basic?

    Visual Basic is a programming language from Microsoft used with the .NET framework, commonly to create desktop and web applications (web applications using ASP.NET (Visual Basic and the .NET framework)).

    Just quickly, what is the .NET framework?
    A programming framework provides a pre-created set of libraries and tools to make it easier to create applications for whatever the specific purpose of that framework. There are many frameworks for different programming languages, such as CodeIgniter for PHP, Rails for the Ruby language (Ruby on Rails), and so on.

    The .NET framework is incredibly extensive, so you can't possibly remember and learn everything the .NET framework has to offer. Essentially, the most important thing is to learn parts of the .NET framework you'll be using. For example, if you want to connect to and use databases in an ASP.NET application, you'll be wanting to look at the SqlClient class that is found in the System.Data namespace (namespaces are merely conceptual - they organise different kinds of classes, essentially).

    What is a class constructor?
    A constructor in the context of a class in object oriented programming, is a kind of method that is executed as soon as an object is created from a class (an instance of a class is created - "instantiation"). For example:

    Code:
    Imports Microsoft.VisualBasic
    Imports System.Data.SqlClient
    
    Public Class Database
    
        Public connString As String = GetConnectionString()
        Public connection As New SqlConnection(connString)
    
        Public Sub New() 'This is the constructor
            connection.Open()
        End Sub
    
        Public Function GetConnectionString() As String
            Return "Persist Security Info=False;Integrated Security=false;User ID=example_uid;Password=pw_here;Database=example_db;server=example.com"
        End Function
    
    End Class
    The New() method is the constructor. This method is executed as soon as an instance of the Database class (above) is created. However, with the GetConnectionString() method, that would otherwise need to be called after class instantiation.

    What is "instantiation"? (in-stan-shee-ation)
    "to provide an instance of or concrete evidence in support of (a theory, concept, claim, or the like). " (dictionary.com)

    In context, it means "creating an instance of a class" (or creating an object). Essentially, creating an instance is allocating the class to memory to use.
Working...
X