Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Object-oriented instantiation Analysis of VB

2025-03-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "VB object-oriented instantiation analysis". In daily operation, I believe many people have doubts about VB object-oriented instantiation analysis. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "VB object-oriented instantiation analysis". Next, please follow the editor to study!

VB getting started tutorial object instance

A class is a template or a blueprint that represents an entity. Because you need to change the concept of a class into an object because you want to use a domain, method, or other member of a class, it is useless to say that it is just a blueprint for a sports car, unless the engineer makes the blueprint into a car, on the other hand, you're driving a car, not a blueprint.

In object-oriented programming, an object is called an instance of a class, so the process of creating an object is also called instantiation.

VB getting started tutorial list1 demonstrates instantiating employee classes

Listing 1: object initialization

Class Employee Dim salary As Decimal = 40000 Dim yearlyBonus As Decimal = 4000 Public Sub PrintSalary () 'print the salary to the Console System.Console.Write (salary) End Sub End Class Module Module1 Public Sub Main () Dim anEmployee As Employee anEmployee = New Employee () anEmployee.PrintSalary () End Sub End Module

Main sub is provided in the module Module1 in the VB introductory tutorial Listing 1. Main Sub is the entrance to the entire program in the vb.NET program. To compile the source program, you must provide access to Main Sub. If you are not using Visual Studio.NET, you can use vbc.exe to compile the VB.NET source program, and vbc.exe is installed automatically when you install the .NET Framework. For example, when you save the source code to the file Employee.vb, type vbc Employee.vb in the same directory as Employee.vb.

Now let's go back to the list1 code and declare the object variable of the Employee class in Main sub, which is called anEmployee.

The keyword new. Dim anEmployee As Employee anEmployee must be used when initializing Employeer. AnEmployee = New Employee ()

Now that we have an Employeer object, you can use its functionality. In our example, the PrintSalary method is called.

AnEmployee.PrintSalary ()

You can also put Main Sub in a class, which eliminates the need for modules, as shown in list2

Listing 5: Moving the Main sub to the class itself Class Employee Dim salary As Decimal = 40000 Dim yearlyBonus As Decimal = 4000 Public Sub PrintSalary () 'print the salary to the Console System.Console.Write (salary) End Sub Public Shared Sub Main () Dim employee As Employee employee = New Employee () employee.PrintSalary () End Sub End Class Note that calling System.Console.Write in the PrintSalary method means that the write method of the Console class is called and the Console class is part of the System namespace

At this point, the study of "VB object-oriented instantiation Analysis" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report