Inheritance of base classes in Microsoft. NET (from Microsoft MSDN) (continued)

Source: Internet
Author: User

MyBase keyword
You can use the MyBase keyword from any subclass to call any attribute or method in the base class. You can use this keyword to call a base class method even if the method of the base class has been overwritten in the subclass. For example, if there is a ReplaceAll method in the base class, but this method has been overwritten in the subclass, you can call the ReplaceAll method of the base class from the ReplaceAll method of the subclass. Try opening the frmLineTest. vb form.
Double-click Replace to call up the click event process.
Write the following code in the Click Event of the btnReplace button: Protected Sub btnReplace_Click (_ ByVal sender As Object, _ ByVal e As System. eventArgs) Handles btnReplace. click Dim oLine As LineDelim = New LineDelim () oLine. delimiter = txtDelim. text oLine. line = txtLine. text txtReplace. text = oLine. replaceAll () End Sub
This Code sets the Delimiter attribute to the value entered in the txtDelimiter text box of the example form. Then you can call the ReplaceAll method to change all spaces in the text line to new delimiter characters.
Press F5 to run the project.
Click Replace ). You will see that in the text box next to this button, each word in the sentence has a comma.
Overwrite Method
After the Delimiter attribute is added, you may want to change the GetWord method in the LineDelim class so that the corresponding separator can be used to replace a single space used by the Line class. Because you do not have to change the base class, you need to override the function of the GetWord method in the LineDelim class. Before creating a new GetWord method in the LineDelim class, you must add a keyword in the GetWord method declaration of the Line class.
In the Solution Explorer window, open the Line. vb class code window.
Find the declaration of the GetWord method (the Declaration does not contain parameters), As shown below: Public Overloads Function GetWord () As String adds the keyword Overridable in the Function declaration, As shown below (without this keyword, ). Public Overridable Overloads Function GetWord () As String open the LineDelim. vb class, and use the following code to add a new GetWord method. Public Overloads Overrides Function GetWord () As String Dim astrWords () As String astrWords = MyBase. Line. Split (mstrDelim. ToCharArray () Return astrWords (0) End Function

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.