#Visual basic net constructor code#
The main form will then appear.Īdd a button to the main form and double-click it to display the code for the Button1_Click event handler. By default, the startup form for Windows Application projects is named Form1.vb.
This constructor sets the value of the field that holds the user name.Ĭhange the startup form to design mode by right-clicking its name in Solution Explorer and then clicking View Designer. The Sub New constructor is called automatically when an object based on this class is created. For more information, see Access levels in Visual Basic.ĭefine a property for the class by adding the following code: Public Property UserName() As Stringĭefine a method for the class by adding the following code: Public Sub Capitalize()ĭefine a parameterized constructor for the new class by adding a procedure named Sub New: Public Sub New(ByVal UserName As String) You can make fields available from outside a class by using access modifiers such as Public that provide more access.
The Code Editor provides a corresponding End Class statement for you.ĭefine a private field for the class by adding the following code between the Class and End Class statements: Private userNameValue As Stringĭeclaring the field as Private means it can be used only within the class. You can use the Visual Basic Code Editor to add a class to your startup form by typing the Class keyword followed by the name of the new class.