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

The actual handling of the VB.NET constructor

2025-01-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the actual processing of the VB.NET constructor, the content is very detailed, interested friends can refer to, hope to be helpful to you.

In the field of development, many development languages have emerged for programmers to choose applications according to their own needs. For example, the VB.NET language can help developers with specific needs. When dealing with the VB.NET constructor, there are two statements worth noting:

Analysis of Solutions to VB.NET Control Array problems

Chat about the performance characteristics of VB.NET cstr function

Introduction of operation skills related to VB.NET connecting to database

In-depth Analysis of VB.NET Verification of LDAP user identity

Detailed interpretation of the operation steps of VB.NET to implement Singleton mode

(1) if the type does not contain any instance constructor declaration, VB.NET provides the default constructor

The default parameter calls the parameterless constructor of the direct base class. If the base class does not have an accessible no-argument constructor, a compile-time error occurs.

(2) Constructors cannot inherit

Statement (1) contains two aspects of information:

① is that if the type does not contain any instance constructor declaration, VB.NET provides the default constructor, that is, the following declaration is allowed. We do not explicitly declare any constructor in the class Shape, but we can still instantiate the class through the no-argument constructor New () provided automatically by VB.NET.

Module TestModule Test

Public Sub Main () Sub Main ()

Dim shape1 As Shape = New Shape

End Sub

End Module

Public Class ShapeClass Shape

Public Sub Draw () Sub Draw ()

'some codes here.

End Sub

End Class

② is that if the type contains a declaration of the instance constructor, then VB.NET no longer provides the default constructor. For example, we added a constructor with parameters to the class Shape.

Public Class ShapeClass Shape Private _ pt As Point Public Sub New () Sub New (p As Point) _ pt = p End Sub Public Sub Draw () Sub Draw () 'some codes here. End Sub End Class

Then, if you instantiate the class directly, an error will occur

Dim shape1 As Shape = New Shape

A class can only be instantiated by calling a constructor with parameters explicitly declared in the Shape class, as follows

Dim shape1 As Shape = New Shape (New Point (0,0))

Therefore, if you want to use both unparameterized and parameterized constructors in your class, you must explicitly declare both constructors.

Public Class ShapeClass Shape Private _ pt As Point Public Sub New () Sub New () 'some codes here. End Sub Public Sub New () Sub New (p As Point) _ pt = p End Sub Public Sub Draw () Sub Draw ()' some codes here. End Sub End Class

State (2) that the constructor cannot be inherited, that is, if you want the subclass to have the same constructor as the base class, you must explicitly declare the same constructor in each subclass as the base class, for example:

Public MustInherit Class

ShapeClass Shape

Private _ pt As Point

Public Sub New () Sub New ()

'some codes here.

End Sub

Public Sub New () Sub New (p As Point)

_ pt = p

End Sub

Public MustOverride Sub Draw () Sub Draw ()

End Class

Public Class LineClass Line

Inherits Shape

Public Sub New () Sub New ()

End Sub

Public Sub New () Sub New (p As Point)

MyBase.New (p)

End Sub

Public Overrides Sub Draw () Sub Draw ()

'some codes here.

End Sub

End Class

This is very important and meaningful. Because we tend to think that subclasses inherit all the properties and methods of the base class, this often leads to compilation errors in our programs.

Combining the above two statements, one principle should be followed in actual programming: in a class, whether it is a base class or a subclass, explicitly declare all required no-parameter or parameter constructors. Following this principle will not only improve the speed of compiling the correct code, but also help yourself and other programmers to understand the code.

So much for the actual handling of the VB.NET constructor. I hope the above can be of some help to you and learn more. 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: 261

*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