Share mybase vb.net

## mybase trong vb.net

** mybase trong vb.net?**

MyBase là một từ khóa trong vb.net đề cập đến lớp cơ sở ngay lập tức của lớp hiện tại.Nó có thể được sử dụng để truy cập vào các thành viên của lớp cơ sở, ngay cả khi chúng được ẩn bởi các thành viên của lớp hiện tại.

Ví dụ: mã sau đây cho thấy cách sử dụng mybase để truy cập phương thức `toString ()` của lớp `object`:

`` `VBNet
Dim MyObject như MyClass mới ()
Console.WriteLine (myObject.Mybase.ToString ())
`` `

Đầu ra của mã này sẽ là biểu diễn chuỗi của lớp `Object`, đó là` "System.Object" `.

** Khi nào nên sử dụng mybase? **

Bạn nên sử dụng mybase khi bạn cần truy cập vào một thành viên của lớp cơ sở được ẩn bởi một thành viên của lớp hiện tại.Ví dụ: nếu lớp hiện tại có một phương thức có cùng tên với phương thức trong lớp cơ sở, bạn có thể sử dụng mybase để gọi phương thức trong lớp cơ sở.

Bạn cũng có thể sử dụng mybase để gọi các hàm tạo của lớp cơ sở.Điều này có thể hữu ích nếu bạn cần khởi tạo lớp cơ sở trước khi khởi tạo lớp hiện tại.

** Ví dụ về việc sử dụng mybase **

Mã sau đây hiển thị một ví dụ về việc sử dụng mybase để gọi phương thức `toString ()` của lớp `object`:

`` `VBNet
Lớp học công khai MyClass
Công khai Sub mới ()
Mybase.new ()
Kết thúc phụ

Công khai ghi đè chức năng toString () dưới dạng chuỗi
Trả về mybase.toString () & ", myClass"
Hàm cuối
Kết thúc lớp học
`` `

Khi mã này được thực thi, đầu ra sau được hiển thị:

`` `
System.Object, MyClass
`` `

## hashtags

* #vb.net
* #oop
* #Di sản
* #BASECLASS
* #Lớp có nguồn gốc
=======================================
## MyBase in VB.NET

**What is MyBase in VB.NET?**

MyBase is a keyword in VB.NET that refers to the immediate base class of the current class. It can be used to access the members of the base class, even if they are hidden by members of the current class.

For example, the following code shows how to use MyBase to access the `ToString()` method of the `Object` class:

```vbnet
Dim myObject As New MyClass()
Console.WriteLine(myObject.MyBase.ToString())
```

The output of this code would be the string representation of the `Object` class, which is `"System.Object"`.

**When to use MyBase?**

You should use MyBase when you need to access a member of the base class that is hidden by a member of the current class. For example, if the current class has a method with the same name as a method in the base class, you can use MyBase to call the method in the base class.

You can also use MyBase to call the constructors of the base class. This can be useful if you need to initialize the base class before initializing the current class.

**Example of using MyBase**

The following code shows an example of using MyBase to call the `ToString()` method of the `Object` class:

```vbnet
Public Class MyClass
Public Sub New()
MyBase.New()
End Sub

Public Overrides Function ToString() As String
Return MyBase.ToString() & ", MyClass"
End Function
End Class
```

When this code is executed, the following output is displayed:

```
System.Object, MyClass
```

## Hashtags

* #vb.net
* #oop
* #Inheritance
* #BASECLASS
* #DerivedClass
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top