MyClass on VB.Net

What is the realistic use of the VB.Net MyClass keyword?

I understand the technical use of MyClass ; I do not understand its practical use in the real world.

Using MyClass only makes sense if you have virtual (redefined) elements. But it also means that you want to ignore overridden implementations in subclasses. This seems controversial.

I can come up with some far-fetched examples, but they are just poor design, not practical use.

+6
source share
4 answers

MyClass , from a compiler point of view, is a way to omit the callvirt statement in favor of the call statement. In fact, when you call a method with virtual semantics ( callvirt ), you indicate that you want to use the most derived variations. In cases where you want to omit derived variations, you use MyClass ( call ). Although you stated that you understood the basic concept, I thought that this could help describe it from a functional point of view, rather than imply understanding. It is functionally identical to MyBase if the scope clause is the base type of MyBase instead of the active type with MyClass .

Overriding the semantics of virtual calls at the current point in the hierarchy is usually a poor design choice, the only times when this is true is when you have to rely on certain functionality in your object hierarchy and cannot rely on the heir to invoke your variant with using a basic call in their implementation. He can also rely on you as a designer, who decided that this is the only alternative, since you have redefined functionality in the hierarchy of objects, and you must make sure that in this corner case this particular method should be called at the current level of the inheritance tree.

All about design, understanding the overall design and corner cases. This is probably the reason why Cโ™ฏ does not include such functionality, since in these corner cases you can divide the method into a private variable, the current current level in the hierarchy calls, and just refer to this private implementation when necessary. It is my personal opinion that using the segmentation approach is an ideal tool to achieve the goal, as it clearly concerns your design choice and is easier to track (and it is also the only valid tool in languages โ€‹โ€‹without the functional equivalent of MyClass .)

+1
source share

Polymorphism

I'm sorry that there is no clear code example here, but you can follow the link below and I don't like copying the description of the MSDN library, but it is so good that it is actually difficult to rewrite it more clearly.

"MyClass provides a way to refer to the current members of an instance of a class without replacing them with overrides of derived classes. The MyClass keyword behaves like an object variable related to the current instance of the class, as originally implemented."

Also note that you cannot use MyClass in a shared method.

A good example of implementing polymorphism through MyClass is http://www.devarticles.com/c/a/VB.Net/Implementing-Polymorphism-in-VB.Net/

+1
source share

I suppose that the only case I could use for this would be if you would like a base condition and an inherited condition at the same time? I.E. where do you want to inherit a member but want to access the value for that member that has not been changed by inheritance?

-one
source share

You need this if you want to call a chain constructor.

 Public Sub New(ByVal accountKey As Integer) MyClass.New(accountKey, Nothing) End Sub Public Sub New(ByVal accountKey As Integer, ByVal accountName As String) MyClass.New(accountKey, accountName, Nothing) End Sub Public Sub New(ByVal accountKey As Integer, ByVal accountName As String, ByVal accountNumber As String) m_AccountKey = accountKey m_AccountName = accountName m_AccountNumber = accountNumber End Sub 
-3
source share

All Articles