473,882 Members | 1,546 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Me vs. MyClass

What is the difference between the Me and the MyClass keywords? I
understand Me that it represents the specific instance of the class.
When you're inside the object instance, it refers to itself using the
Me keyword. What is the MyClass keyword used for?

Nov 21 '05
24 3597
On 2005-09-04, Jay B. Harlow [MVP - Outlook] <Ja************ @tsbradley.net> wrote:
dfoster,
| I struggle to think of a use for MyClass that doesn't represent horrible
| design. Does anyone know if this is actually used in the framework
| anywhere?
Chaining constructors, such as:
I meant an example where a call used MyClass functionality rather
than Me functionality. ctors are never called virtually in VB.Net.
In the code below, Me.New and MyClass.New are the exact same thing.
The MyClass keyword doesn't achieve anything here.

The only thing I can think of is that it's used in the VB libraries for
performance reasons to get away from the fact that the VBC compiler
doesn't distinguish virtual functions, but I had always assumed (hoped)
that this problem was jitted away in practice.

Public Class SomeCollection

Public Const DefaultValue As String = ""
Public Const DefaultCapacity As Integer = 16

Public Sub New()
MyClass.New(Def aultValue, DefaultCapacity )
End Sub

Public Sub New(ByVal capacity As Integer)
MyClass.New(Def aultValue, capacity)
End Sub

Public Sub New(ByVal value As String)
MyClass.New(val ue, DefaultCapacity )
End Sub

Public Sub New(ByVal value As String, ByVal capacity As Integer)

End Sub

...

End Class

Granted you could use Optional parameters instead at the expense of being
friendly to C#...

For methods other then constructors, I cannot think of a good example where
I would, much less have, used it. However I expect if it was not available,
then we would all have good examples of needing it ;-)

Hope this helps
Jay

"dfoster" <df*****@woofix .local.dom> wrote in message
news:sl******** ************@lo calhost.localdo main...
| On 2005-09-04, Sathyaish <sa*******@gmai l.com> wrote:
| > dfoster,
| >
| > I think your understanding is right. Scott M. probably did not word it
| > correctly.
| >
| > And now, that link you have mentioned from the MSDN brings us to a very
| > important question.
| >
| > What if I changed the interface/pointer type of the object from whom I
| > invoke the MyMethod sub to that of the base class.
|
| It wouldn't matter.
|
| >
| > So, instead of:
| >
| > Dim TestObj As DerivedClass = New DerivedClass
| > TestObj.UseMe() ' Displays "Derived class string".
| > TestObj.UseMyCl ass() ' Displays "Base class string".
| > TestObj.MyMetho d()
|
| That last displays "Derived Class string"
|
| >
| > that the documentation has, what if I do this:
| >
| > Dim Base As BaseClass = New DerivedClass
| > Base.MyMethod()
|
| Still displays "Derived class string"
|
| > Base.UseMe()
| > Base.UseMyClass ()
| >
| > It nullifies the v-table entries and shadows the derived class
| > overrides just as in the previous example. This proves that one must
| > not expect polymorphic behaviour from your class if you invoke a method
| > with the MyClass keyword.
|
| I'd word that differently. From the callers POV, you still get
| polymorphism. MyMethod will display differently depending on whether the
| caller created a BaseClass or a DerivedClass, regardless of whether the
| variable is declared as a BaseClass or a DerivedClass. A DerivedClass
| instance acts the same way even if you call methods through a variable
| declared as BaseClass.
|
|
| The variable declaration simply doesn't matter, only the definition does,
| and that's the essence of polymorphism.
|
| I struggle to think of a use for MyClass that doesn't represent horrible
| design. Does anyone know if this is actually used in the framework
| anywhere?
|
|

Nov 21 '05 #11
On 2005-09-04, Scott M. <s-***@nospam.nosp am> wrote:
No, your understanding is wrong and my wording is right.
I never said your wording was wrong, just that I was confused by it.
And with this example, it certainly appears that we both have the same
understanding of the concept.

You don't declare something as MyClass (nor, did I say that you do), you
reference something that way. An example is best...

'************** *************** *************** **
Public Class A
Public Overridable Function DoFoo() As String
'Do something here
End Function

Public Overridable Sub Foo()
MyClass.DoFoo()
End Sub
End Class
'************** *************** *************** **
Public Class B
Inherits A

Public Overrides Function DoFoo() As String
'Do something here
End Function

Public Sub SomethingElse
Foo()
End Sub
End Class
'************** *************** *************** **

If I were to now make an instance of class "B" and call its "SomethingE lse"
method the SomethingElse method from the "B" class then calls the "Foo"
method of the base class.

In the base class (A), the "Foo" method calls an overridable method called
"DoFoo". There is a "DoFoo" method in BOTH classes. Without the "MyClass"
keyword in the base class, the child class would wind up using the
overridden "DoFoo" method in the child class, but because of the "MyClass"
keyword, calling "SomethingE lse" in the child class calls "Foo" in the base
class and "Foo" in the base class uses "DoSomethin g" from the SAME CLASS as
"DoSomethin g" is running (which is the base class).

It is a bit confusing, but if you trace my explanation, you will see that
without the "MyClass" keyword, the base class method "Foo" would use the
child class method "DoFoo" instead of its own.

-Scott


"Sathyaish" <sa*******@gmai l.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
dfoster,

I think your understanding is right. Scott M. probably did not word it
correctly.

And now, that link you have mentioned from the MSDN brings us to a very
important question.

What if I changed the interface/pointer type of the object from whom I
invoke the MyMethod sub to that of the base class.

So, instead of:

Dim TestObj As DerivedClass = New DerivedClass
TestObj.UseMe() ' Displays "Derived class string".
TestObj.UseMyCl ass() ' Displays "Base class string".
TestObj.MyMetho d()
that the documentation has, what if I do this:

Dim Base As BaseClass = New DerivedClass
Base.MyMethod()
Base.UseMe()
Base.UseMyClass ()

It nullifies the v-table entries and shadows the derived class
overrides just as in the previous example. This proves that one must
not expect polymorphic behaviour from your class if you invoke a method
with the MyClass keyword.
Thanks for all your replies.

Regards,
Sathyaish
PS: I am Water Cooler v2. My posting quota on this machine is over, so
I have switched accounts.


Nov 21 '05 #12
My reply was to Sathyaish, not you.
"dfoster" <df*****@woofix .local.dom> wrote in message
news:sl******** ************@lo calhost.localdo main...
On 2005-09-04, Scott M. <s-***@nospam.nosp am> wrote:
No, your understanding is wrong and my wording is right.


I never said your wording was wrong, just that I was confused by it.
And with this example, it certainly appears that we both have the same
understanding of the concept.

You don't declare something as MyClass (nor, did I say that you do), you
reference something that way. An example is best...

'************** *************** *************** **
Public Class A
Public Overridable Function DoFoo() As String
'Do something here
End Function

Public Overridable Sub Foo()
MyClass.DoFoo()
End Sub
End Class
'************** *************** *************** **
Public Class B
Inherits A

Public Overrides Function DoFoo() As String
'Do something here
End Function

Public Sub SomethingElse
Foo()
End Sub
End Class
'************** *************** *************** **

If I were to now make an instance of class "B" and call its
"SomethingE lse"
method the SomethingElse method from the "B" class then calls the "Foo"
method of the base class.

In the base class (A), the "Foo" method calls an overridable method
called
"DoFoo". There is a "DoFoo" method in BOTH classes. Without the
"MyClass"
keyword in the base class, the child class would wind up using the
overridden "DoFoo" method in the child class, but because of the
"MyClass"
keyword, calling "SomethingE lse" in the child class calls "Foo" in the
base
class and "Foo" in the base class uses "DoSomethin g" from the SAME CLASS
as
"DoSomethin g" is running (which is the base class).

It is a bit confusing, but if you trace my explanation, you will see that
without the "MyClass" keyword, the base class method "Foo" would use the
child class method "DoFoo" instead of its own.

-Scott


"Sathyaish" <sa*******@gmai l.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
dfoster,

I think your understanding is right. Scott M. probably did not word it
correctly.

And now, that link you have mentioned from the MSDN brings us to a very
important question.

What if I changed the interface/pointer type of the object from whom I
invoke the MyMethod sub to that of the base class.

So, instead of:

Dim TestObj As DerivedClass = New DerivedClass
TestObj.UseMe() ' Displays "Derived class string".
TestObj.UseMyCl ass() ' Displays "Base class string".
TestObj.MyMetho d()
that the documentation has, what if I do this:

Dim Base As BaseClass = New DerivedClass
Base.MyMethod()
Base.UseMe()
Base.UseMyClass ()

It nullifies the v-table entries and shadows the derived class
overrides just as in the previous example. This proves that one must
not expect polymorphic behaviour from your class if you invoke a method
with the MyClass keyword.
Thanks for all your replies.

Regards,
Sathyaish
PS: I am Water Cooler v2. My posting quota on this machine is over, so
I have switched accounts.


Nov 21 '05 #13
dfoster,
| I meant an example where a call used MyClass functionality rather
| than Me functionality. ctors are never called virtually in VB.Net.
| In the code below, Me.New and MyClass.New are the exact same thing.
I agree, they call the same routine.

| The MyClass keyword doesn't achieve anything here.
However! IMHO MyClass is the more correct as you are explicitly stating you
are calling a routine in the current class.

In other words I use MyClass in the constructor as I would in other methods,
to indicate I am wanting to only consider methods directly in the current
class.

I don't use Me to chain a constructor: I use either MyClass or MyBase as
they "better" state what is being called.

Hope this helps
Jay

"dfoster" <df*****@woofix .local.dom> wrote in message
news:sl******** ************@lo calhost.localdo main...
| On 2005-09-04, Jay B. Harlow [MVP - Outlook]
<Ja************ @tsbradley.net> wrote:
| > dfoster,
| >| I struggle to think of a use for MyClass that doesn't represent
horrible
| >| design. Does anyone know if this is actually used in the framework
| >| anywhere?
| > Chaining constructors, such as:
|
| I meant an example where a call used MyClass functionality rather
| than Me functionality. ctors are never called virtually in VB.Net.
| In the code below, Me.New and MyClass.New are the exact same thing.
| The MyClass keyword doesn't achieve anything here.
|
| The only thing I can think of is that it's used in the VB libraries for
| performance reasons to get away from the fact that the VBC compiler
| doesn't distinguish virtual functions, but I had always assumed (hoped)
| that this problem was jitted away in practice.
|
| >
| > Public Class SomeCollection
| >
| > Public Const DefaultValue As String = ""
| > Public Const DefaultCapacity As Integer = 16
| >
| > Public Sub New()
| > MyClass.New(Def aultValue, DefaultCapacity )
| > End Sub
| >
| > Public Sub New(ByVal capacity As Integer)
| > MyClass.New(Def aultValue, capacity)
| > End Sub
| >
| > Public Sub New(ByVal value As String)
| > MyClass.New(val ue, DefaultCapacity )
| > End Sub
| >
| > Public Sub New(ByVal value As String, ByVal capacity As Integer)
| >
| > End Sub
| >
| > ...
| >
| > End Class
| >
| > Granted you could use Optional parameters instead at the expense of
being
| > friendly to C#...
| >
| > For methods other then constructors, I cannot think of a good example
where
| > I would, much less have, used it. However I expect if it was not
available,
| > then we would all have good examples of needing it ;-)
| >
| > Hope this helps
| > Jay
| >
| > "dfoster" <df*****@woofix .local.dom> wrote in message
| > news:sl******** ************@lo calhost.localdo main...
| >| On 2005-09-04, Sathyaish <sa*******@gmai l.com> wrote:
| >| > dfoster,
| >| >
| >| > I think your understanding is right. Scott M. probably did not word
it
| >| > correctly.
| >| >
| >| > And now, that link you have mentioned from the MSDN brings us to a
very
| >| > important question.
| >| >
| >| > What if I changed the interface/pointer type of the object from whom
I
| >| > invoke the MyMethod sub to that of the base class.
| >|
| >| It wouldn't matter.
| >|
| >| >
| >| > So, instead of:
| >| >
| >| > Dim TestObj As DerivedClass = New DerivedClass
| >| > TestObj.UseMe() ' Displays "Derived class string".
| >| > TestObj.UseMyCl ass() ' Displays "Base class string".
| >| > TestObj.MyMetho d()
| >|
| >| That last displays "Derived Class string"
| >|
| >| >
| >| > that the documentation has, what if I do this:
| >| >
| >| > Dim Base As BaseClass = New DerivedClass
| >| > Base.MyMethod()
| >|
| >| Still displays "Derived class string"
| >|
| >| > Base.UseMe()
| >| > Base.UseMyClass ()
| >| >
| >| > It nullifies the v-table entries and shadows the derived class
| >| > overrides just as in the previous example. This proves that one must
| >| > not expect polymorphic behaviour from your class if you invoke a
method
| >| > with the MyClass keyword.
| >|
| >| I'd word that differently. From the callers POV, you still get
| >| polymorphism. MyMethod will display differently depending on whether
the
| >| caller created a BaseClass or a DerivedClass, regardless of whether the
| >| variable is declared as a BaseClass or a DerivedClass. A DerivedClass
| >| instance acts the same way even if you call methods through a variable
| >| declared as BaseClass.
| >|
| >|
| >| The variable declaration simply doesn't matter, only the definition
does,
| >| and that's the essence of polymorphism.
| >|
| >| I struggle to think of a use for MyClass that doesn't represent
horrible
| >| design. Does anyone know if this is actually used in the framework
| >| anywhere?
| >|
| >|
| >
| >
Nov 21 '05 #14
On 2005-09-04, Jay B. Harlow [MVP - Outlook] <Ja************ @tsbradley.net> wrote:
dfoster,
| I meant an example where a call used MyClass functionality rather
| than Me functionality. ctors are never called virtually in VB.Net.
| In the code below, Me.New and MyClass.New are the exact same thing.
I agree, they call the same routine.

| The MyClass keyword doesn't achieve anything here.
However! IMHO MyClass is the more correct as you are explicitly stating you
are calling a routine in the current class.

In other words I use MyClass in the constructor as I would in other methods,
to indicate I am wanting to only consider methods directly in the current
class.


Mkay... I find that practice awfully goofy and confusing, but to each
his own.

To the point though, I don't think you're seriously arguing that the
keyword is in the language so that developers could have two different
syntaxes with which to chain constructors. So we're still left with the
original question as to why this was seen as a needed keyword.


Nov 21 '05 #15
dfoster,
| Mkay... I find that practice awfully goofy and confusing, but to each
| his own.
How so? Or did you not follow the "simplicity " of my statement? :-|

What I stated can be demonstrated by:

Sub New()
MyClass.New(... ) ' calls a method in this specific class
End Sub

Sub Other()
MyClass.Other(. ..) ' calls a method in this specific class
End Sub

Where as:

Sub New()
Me.New(...) ' calls a method in this specific class
Me.Other(...) ' calls a possibly overridden method
End Sub

Sub Other()
Me.Other(...) ' calls a possibly overridden method
End Sub

I hope you see in the above that using Me, I need to remember an exception
to a rule, that is that Me.New calls a method in this specific class. Using
MyClass I don't need to remember an exception to a rule.

I can see more potential for confusion with using Me, then when using
MyClass! Especially for beginner's.

Of course if you simply ban the use of MyClass, then yes my convention may
seem "goofy & confusing".

| So we're still left with the
| original question as to why this was seen as a needed keyword.
As you are aware, it allows you to defeat polymorphism & explicitly call a
method of the specific class, rather then the (potentially) overriding
method of a derived class.

I think the real question we're after is: when would we really need to do
this. Like I stated earlier I don't have a good example. I can come up with
a couple "suspect" examples (a potentially horrible design as you state),
however they could be solved without MyClass... Granted without MyClass
involves a few more lines of code & an extra method or two...

Hope this helps
Jay

"dfoster" <df*****@woofix .local.dom> wrote in message
news:sl******** ************@lo calhost.localdo main...
| On 2005-09-04, Jay B. Harlow [MVP - Outlook]
<Ja************ @tsbradley.net> wrote:
| > dfoster,
| >| I meant an example where a call used MyClass functionality rather
| >| than Me functionality. ctors are never called virtually in VB.Net.
| >| In the code below, Me.New and MyClass.New are the exact same thing.
| > I agree, they call the same routine.
| >
| >| The MyClass keyword doesn't achieve anything here.
| > However! IMHO MyClass is the more correct as you are explicitly stating
you
| > are calling a routine in the current class.
| >
| > In other words I use MyClass in the constructor as I would in other
methods,
| > to indicate I am wanting to only consider methods directly in the
current
| > class.
|
| Mkay... I find that practice awfully goofy and confusing, but to each
| his own.
|
| To the point though, I don't think you're seriously arguing that the
| keyword is in the language so that developers could have two different
| syntaxes with which to chain constructors. So we're still left with the
| original question as to why this was seen as a needed keyword.
|
|
|
|
Nov 21 '05 #16
I think it needs to be said that the use of MyClass is in no way meant
exclusively for (or even directly intended for) constructors. As in the
documentation and the sample I showed, MyClass is simply there to explicitly
call a method from a base class on overridable methods.
"Jay B. Harlow [MVP - Outlook]" <Ja************ @tsbradley.net> wrote in
message news:ej******** *****@TK2MSFTNG P12.phx.gbl...
dfoster,
| Mkay... I find that practice awfully goofy and confusing, but to each
| his own.
How so? Or did you not follow the "simplicity " of my statement? :-|

What I stated can be demonstrated by:

Sub New()
MyClass.New(... ) ' calls a method in this specific class
End Sub

Sub Other()
MyClass.Other(. ..) ' calls a method in this specific class
End Sub

Where as:

Sub New()
Me.New(...) ' calls a method in this specific class
Me.Other(...) ' calls a possibly overridden method
End Sub

Sub Other()
Me.Other(...) ' calls a possibly overridden method
End Sub

I hope you see in the above that using Me, I need to remember an exception
to a rule, that is that Me.New calls a method in this specific class.
Using
MyClass I don't need to remember an exception to a rule.

I can see more potential for confusion with using Me, then when using
MyClass! Especially for beginner's.

Of course if you simply ban the use of MyClass, then yes my convention may
seem "goofy & confusing".

| So we're still left with the
| original question as to why this was seen as a needed keyword.
As you are aware, it allows you to defeat polymorphism & explicitly call a
method of the specific class, rather then the (potentially) overriding
method of a derived class.

I think the real question we're after is: when would we really need to do
this. Like I stated earlier I don't have a good example. I can come up
with
a couple "suspect" examples (a potentially horrible design as you state),
however they could be solved without MyClass... Granted without MyClass
involves a few more lines of code & an extra method or two...

Hope this helps
Jay

"dfoster" <df*****@woofix .local.dom> wrote in message
news:sl******** ************@lo calhost.localdo main...
| On 2005-09-04, Jay B. Harlow [MVP - Outlook]
<Ja************ @tsbradley.net> wrote:
| > dfoster,
| >| I meant an example where a call used MyClass functionality rather
| >| than Me functionality. ctors are never called virtually in VB.Net.
| >| In the code below, Me.New and MyClass.New are the exact same thing.
| > I agree, they call the same routine.
| >
| >| The MyClass keyword doesn't achieve anything here.
| > However! IMHO MyClass is the more correct as you are explicitly
stating
you
| > are calling a routine in the current class.
| >
| > In other words I use MyClass in the constructor as I would in other
methods,
| > to indicate I am wanting to only consider methods directly in the
current
| > class.
|
| Mkay... I find that practice awfully goofy and confusing, but to each
| his own.
|
| To the point though, I don't think you're seriously arguing that the
| keyword is in the language so that developers could have two different
| syntaxes with which to chain constructors. So we're still left with the
| original question as to why this was seen as a needed keyword.
|
|
|
|

Nov 21 '05 #17
Scott,
| MyClass is simply there to explicitly
| call a method from a base class on overridable methods.
"from a base class" sounds like the description of MyBase, not MyClass!

I hope you meant "MyClass is simply there for a base class to explicitly
call a method from the same base class on overridable methods". I would add
"and in constructors".

| exclusively for (or even directly intended for) constructors. As in the
It appears we may need to agree to disagree on MyClass & constructors... :-|
For overridden methods:
MyBase calls the method of a class I inherit from (a base class)
MyClass calls the method of the current class
Me calls the method of a class that inherits from me (a derived class)
For example:

Public Class SomeBase

Public Overridable Sub Print()
Debug.WriteLine ("Print", "SomeBase")
End Sub

End Class

Public Class Something
Inherits SomeBase

Public Sub PrintSomething( )
MyBase.Print() ' call SomeBase.Print
MyClass.Print() ' call Something.Print
Me.Print() ' call SomeDerived.Pri nt
End Sub

Public Overrides Sub Print()
Debug.WriteLine ("Print", "Something" )
End Sub

End Class

Public Class SomeDerived
Inherits Something

Public Overrides Sub Print()
Debug.WriteLine ("Print", "SomeDerive d")
End Sub

End Class

Public Shared Sub Main()

Dim anything As New SomeDerived
anything.PrintS omething()

End Sub

Notice that when you run the sample it prints:

SomeBase: Print
Something: Print
SomeDerived: Print

Of course the above breaks down if the derived method calls the base method,
which is commonly the case.

Public Overrides Sub Print()
MyBase.Print()
Debug.WriteLine ("Print", "SomeDerive d")
End Sub

Now the question remains, when would one really need to define a routine
similar to Something.Print Something above? I can partially see using
MyClass on templated methods (Template Method Pattern) when I need to be
certain to call my class's method in some cases & the overriding method in
other cases. However I cannot come up with a viable example (one that could
just as easily be solved without MyClass).

Hope this helps
Jay
"Scott M." <s-***@nospam.nosp am> wrote in message
news:Of******** ******@tk2msftn gp13.phx.gbl...
|I think it needs to be said that the use of MyClass is in no way meant
| exclusively for (or even directly intended for) constructors. As in the
| documentation and the sample I showed, MyClass is simply there to
explicitly
| call a method from a base class on overridable methods.
|
|
<<snip>>
Nov 21 '05 #18
On 2005-09-05, Jay B. Harlow [MVP - Outlook] <Ja************ @tsbradley.net> wrote:
dfoster,
| Mkay... I find that practice awfully goofy and confusing, but to each
| his own.
How so? Or did you not follow the "simplicity " of my statement? :-|
No, I followed your statement completely and I understand why you prefer
this syntax. And like I said, to each his own, it's not a syntax that
bothers me significantly. But if you really want to know why I find it
goofy...

1. It implies that a special syntax is necessary to reference ctors,
whereas virtually every other OOP language I know uses the standard
instance pointer if they allow chaining overloaded ctors.

2. It uses a very rare keyword to perform an ordinary task.

3. It requires explaning a simple and common topic, chaining
constructors, in terms of a fairly complex and very rare topic,
bypassing polymorphic behavior.

4. Because you've used a unique syntax, it appears to a casual reader
that VB.Net ctors do not behave as an OOP-experienced developer would
expect them to behave.

5. Because this is the only place a developer is likely to encounter
MyClass, it implies that this is the ordinary behavior of MyClass, when
in fact it is not (as we see below).

What I stated can be demonstrated by:

Sub New()
MyClass.New(... ) ' calls a method in this specific class
End Sub

Sub Other()
MyClass.Other(. ..) ' calls a method in this specific class
End Sub

Where as:

Sub New()
Me.New(...) ' calls a method in this specific class
Me.Other(...) ' calls a possibly overridden method
End Sub

Sub Other()
Me.Other(...) ' calls a possibly overridden method
End Sub

I hope you see in the above that using Me, I need to remember an exception
to a rule, that is that Me.New calls a method in this specific class. Using
MyClass I don't need to remember an exception to a rule.
Actually, you still need to remember an exception to a rule.

Sub New()
MyClass.New(5) ' will work only if THIS class defines the method
MyClass.Other(5 ) ' is fine if a base class defines the method
End Sub

ctors are special. 'Me' works differently with them, and 'MyClass'
works differently with them. They have all kinds of special behavior,
like the automatic calling of the base class, and no syntactical sugar
is going to cover that up.

I can see more potential for confusion with using Me, then when using
MyClass! Especially for beginner's.
Well, there's two kinds of beginners. For experienced developers
moving to VB.Net the 'Me' syntax works just like it does in other OOP
languages, so that's simple and doesn't really require an explanation.
For new developers, you can either go into all the weirdness of what
MyClass does, and then explain that calling MyClass.New works kinda like
MyClass.OtherMe thod, but not quite. Or you can simply explain the ctors
are special in a number of ways and show how.

The ctor information is useful and important in a number of ways, and
you're going to have to cover it anyway. The MyClass information pretty
much has no usefulness in practice.

BTW, I'm tempted to say that it's Me.New(...) that acts normally here,
since it will only call a derived member if that member is declared
Overridable and Sub New can never be declared Overridable. So there's
really no exception to remember. But that would be as wrong as your
description above trying to claim that MyClass works normally in this
case. The fact is that both exhibit nonstandard behavior.
Of course if you simply ban the use of MyClass, then yes my convention may
seem "goofy & confusing".
Despite the length of the post above (hey, you asked for my reasons),
"goofy" is a pretty weak epithet to me. There are a lot of programming
practices I feel strongly about, but this isn't one of them. And the
docs seem ambivalent on the subject, sometimes using MyClass.New and
sometimes using Me.New, so developers have to pick up both syntaxes
(syntaxi?) at some point.

| So we're still left with the
| original question as to why this was seen as a needed keyword.
As you are aware, it allows you to defeat polymorphism & explicitly call a
method of the specific class, rather then the (potentially) overriding
method of a derived class.

I think the real question we're after is: when would we really need to do
this.


Kind of, but I'm also wondering if this feature is there because the dev
team thought it would be useful to developers, or if they really needed
the feature for something internal (is anything internal actually
written in VB.Net, I have no idea) or for some VB6 conversion issue.

I googled this earlier cuz I was curious, and I came across a couple of
old links that implied that in very early betas all methods were virtual
in VB.Net. That would explain the strange behavior of the compiler
which seems to assume this, and also the need of the MyClass keyword.
But I'm just guessing at that...
Nov 21 '05 #19
On 2005-09-05, Jay B. Harlow [MVP - Outlook] <Ja************ @tsbradley.net> wrote:
For overridden methods:
MyBase calls the method of a class I inherit from (a base class)
MyClass calls the method of the current class
Me calls the method of a class that inherits from me (a derived class)


Well, if we're going to be precise, then let's be precise

For Overridable methods:

MyBase calls the method defined in the nearest ancestor that defines the
method. This is also what it does for NonOverridable methods.

MyClass calls the method in the current class if the current class
defines the method, otherwise it behaves like MyBase

Me calls the method from the vtable, i.e., the most derived class of the
current instance that implements the overridden method. For
NonOverridable methods, Me and MyClass are functionally equivalent
(although they result in different IL).

Of course, Shadows confuses the whole thing, but let's not go there.
And this only applies to standard Overridable methods, these keywords
all have different behavior when applied to Sub New. For Sub New,

MyBase will only call the immediate base class.

MyClass will only call the current class.

Me will only call the current class. Again, Me and MyClass have
identical behavior (but this time result in the same IL).
Nov 21 '05 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

32
2753
by: Mario Fratelli | last post by:
MyClass *p = new MyClass; MyClass *p = new MyClass(); do they mean something different? Thanks a lot, Mario Fratelli.
11
2591
by: modemer | last post by:
If I define the following codes: void f(const MyClass & in) {cout << "f(const)\n";} void f(MyClass in) {cout<<"f()\n";} MyClass myclass; f(myclass); Compiler complain that it can't find the best match. Anyone could give a detail explanation in theory? Which one is good?
4
2411
by: John | last post by:
I have a function declaration that gives an error while compiling. Can anyone help me figure this one out? inline void create(const std::vector< myclass >& plist, std::vector< myclass >::iterator left, std::vector< myclass >::iterator right); x.hpp:153: error: `class std::vector<myclass, D>, std::alloca
5
1514
by: Gunnar G | last post by:
What is the difference between the two lines in the main function? class MyClass{ ....}; int main(){ MyClass a(); MyClass a; }
3
1535
by: janzon | last post by:
Hi! When defining a new object m of class MyClass, with no parameters to the constructor, one is supposed to write "MyClass m;". If one wants to pass a parameter p to the constructor, one is supposed to write "MyClass m(p);". It doesn't work if one writes "MyClass m();" when one wants to pass zero parameters. Why is that? It seems inelegant and non-symmetric to me. What reason is there behind this? / Me, seeking insights into C++
9
7008
by: Stephan Steiner | last post by:
Hi I seem to have a bit of trouble understanding one bit of how generics work: In C#, every class automatically derives from object, and inherits a bunch of properties (i.e. ToString()). Thus, (MyClass is object) should always evaluate as true. However, if I have a method with the following signature:
4
11440
by: shapper | last post by:
Hello, I am getting various records from a database: Dim ds As DataSet = db.ExecuteDataSet(dbc) Each record has 3 fields: Id, Name and Text. I have a class named MyClass with 3 properties Id, Name and Text.
9
2378
by: devloop | last post by:
Hi, I am a JAVA developer who just started with php5 (on a WAMPP with php5.2.1). I´ve the following problem with a little class (I wanted to put objects into the array later but even with simple strings it doesn´t work!!!!!!!!!). The output doesn´t show any letters/strings when I call the method toHtml().
0
1307
by: Curious | last post by:
Hi, I've created a windows form application. I have a form and a .cs file in the application, and I've renamed them to MyDialog and MyClass.cs. In MyClass.cs, I have a member called "Publisher" and I assign value to this member in MyClass.cs. In MyDialog (upon a button click), I'll need to use the current value of "Publisher" from MyClass.cs. But I don't know how. Anyone can
0
9931
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9777
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10725
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10830
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10403
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9557
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5978
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4601
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4198
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.