In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how to define VB.NET classes. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
I. VB.NET class definition
1. Click New Project on the File menu to create a project. The New Project dialog box appears.
two。 Select Windows Application from the list of Visual Basic project templates to display the new project.
3. Click add Class on the Project menu to add a new class to the project. The add New item dialog box appears.
4. Select the Class template.
5. Name the new class UserNameInfo.vb, and then click add to display the code for the new class.
Visual Basic copy code Public Class UserNameInfo End Class
Note that you can use the Visual Basic Code Editor to add the class to the startup form by typing the Class keyword before the name of the new class. The Code Editor provides the corresponding End Class statement.
6. Add the following code between the Class and End Class statements to define private fields for the class:
Visual Basic copy code Private userNameValue As String
Declaring a field as Private means that the field can only be used within the class. Fields can be accessed from outside the class by using access modifiers that provide greater access, such as Public. For more information, see access levels in Visual Basic.
7. Define properties for the class by adding the following code:
Visual Basic copies the code Public Property UserName () As String Get 'Gets the property value. Return userNameValue End Get Set (ByVal Value As String) 'Sets the property value. UserNameValue = Value End Set End Property
8. Define methods for the class by adding the following code:
Visual Basic copies the code Public Sub Capitalize () 'Capitalize the value of the property. UserNameValue = UCase (userNameValue) End Sub
9. Define a parameterized constructor for the new class by adding a procedure named Sub New:
Visual Basic copies the code Public Sub New (ByVal UserName As String) 'Set the property value. Me.UserName = UserName End Sub
When you create an object based on this class, the Sub New constructor is automatically called. This constructor sets the field value at which the user name is saved.
two。 Create a button for the test class
1. Right-click the name of the startup form in solution Explorer, change the startup form to design mode, and then click View designer. By default, the startup form for the Windows Application project is named Form1.vb. The main form appears.
two。 Add a button to the main form, and then double-click the button to display the code for the Button1_Click event handler. Add the following code to invoke the test process:
Visual Basic copies the code 'Create an instance of the class. Dim user As New UserNameInfo ("Moore, Bobby") 'Capitalize the value of the property. User.Capitalize () 'Display the value of the property. MsgBox ("The original UserName is:" & user.UserName) 'Change the value of the property. User.UserName = "Worden, Joe" 'Redisplay the value of the property. MsgBox ("The new UserName is:" & user.UserName)
VB.NET class definition to run the application
Press F5 to run the application. Click the button on the form to invoke the test process. It displays a message stating that the original UserName is "MOORE, BOBBY" because the procedure calls the object's Capitalize method.
Click OK to close the message box. The Button1 Click process changes the value of the UserName property and displays a message stating that the new value of UserName is "Worden, Joe".
Thank you for reading! This is the end of this article on "how to define VB.NET classes". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.