אירוע
17 במרץ, 21 - 21 במרץ, 10
הצטרף לסידרה של פגישות כדי לבנות פתרונות מדרגיים של בינה מלאכותית בהתבסס על מקרי שימוש מהעולם האמיתי עם מפתחים ומומחים אחרים.
הירשם עכשיוהדפדפן הזה אינו נתמך עוד.
שדרג ל- Microsoft Edge כדי לנצל את התכונות, עדכוני האבטחה והתמיכה הטכנית העדכניים ביותר.
This walkthrough demonstrates how to define classes, which you can then use to create objects. It also shows you how to add properties and methods to the new class, and demonstrates how to initialize an object.
הערה
Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Personalizing the IDE.
Create a project by clicking New Project on the File menu. The New Project dialog box appears.
Select Windows Application from the list of Visual Basic project templates to display the new project.
Add a new class to the project by clicking Add Class on the Project menu. The Add New Item dialog box appears.
Select the Class template.
Name the new class UserNameInfo.vb
, and then click Add to display the code for the new class.
Public Class UserNameInfo
End 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. The Code Editor provides a corresponding End Class
statement for you.
Define a private field for the class by adding the following code between the Class
and End Class
statements:
Private userNameValue As String
Declaring the field as Private
means it can be used only within the class. You can make fields available from outside a class by using access modifiers such as Public
that provide more access. For more information, see Access levels in Visual Basic.
Define a property for the class by adding the following 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
Define a method for the class by adding the following code:
Public Sub Capitalize()
' Capitalize the value of the property.
userNameValue = UCase(userNameValue)
End Sub
Define a parameterized constructor for the new class by adding a procedure named Sub New
:
Public Sub New(ByVal UserName As String)
' Set the property value.
Me.UserName = UserName
End Sub
The Sub New
constructor is called automatically when an object based on this class is created. This constructor sets the value of the field that holds the user name.
Change the startup form to design mode by right-clicking its name in Solution Explorer and then clicking View Designer. By default, the startup form for Windows Application projects is named Form1.vb. The main form will then appear.
Add a button to the main form and double-click it to display the code for the Button1_Click
event handler. Add the following code to call the test procedure:
' 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)
Run your application by pressing F5. Click the button on the form to call the test procedure. It displays a message stating that the original UserName
is "MOORE, BOBBY", because the procedure called the Capitalize
method of the object.
Click OK to dismiss the message box. The Button1 Click
procedure changes the value of the UserName
property and displays a message stating that the new value of UserName
is "Worden, Joe".
משוב של .NET
.NET הוא פרויקט קוד פתוח. בחר קישור כדי לספק משוב:
אירוע
17 במרץ, 21 - 21 במרץ, 10
הצטרף לסידרה של פגישות כדי לבנות פתרונות מדרגיים של בינה מלאכותית בהתבסס על מקרי שימוש מהעולם האמיתי עם מפתחים ומומחים אחרים.
הירשם עכשיוהדרכה
מודול
Get started with classes and objects in C# - Training
Learn how to create classes and instantiate objects that expose encapsulated field data by using class definitions, constructors, and the 'new' operator.
תיעוד
Objetos y clases - Visual Basic
Obtenga más información sobre objetos y clases de Visual Basic.
Fundamentos de la herencia - Visual Basic
Más información sobre: Fundamentos de la herencia (Visual Basic)
Module (Instrucción) - Visual Basic
Más información sobre: instrucción Module