C# Equivalent of VB.NET MyClass???

  • Thread starter Thread starter FDude
  • Start date Start date
F

FDude

In otherwords, how do I force a method in my class to call the method
implementations in the SAME class and not use any potentially overriden
methods in derived classes?
 
I believe the keyword you seek is "this".
If you are in a class and type "this." (this and a period)
you should see the methods/ members in that class.
If you wish to refer to a method in a derived class,
use the "base" keyword.

Does that help?
Peter
 
Peter Bromberg said:
I believe the keyword you seek is "this".

this in C# == me is VB. I think base is MyBase is vb. I think MyClass can be used by a base class to find what has inherited it. I
don't think C# has this.
 
FDude said:
In otherwords, how do I force a method in my class to call the method
implementations in the SAME class and not use any potentially
overriden methods in derived classes?

Decorate your methods with the virtual keyword and the proper method will be
called every time assuming it's implemented in the current type.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
 
There is no equivalent to MyClass in C#.

Me = this
MyBase = base
MyClass has no equivalent
 
Hey Peter,

The VB equivalent to "this" is "Me". It works exactly the same way.

On the other hand, MyClass says to always call this classes implementation
of a method or property even if it is overriden by a derived class.

Class Foo
Public Overridable Function Bar(ByVal x as Integer) As Integer
Return x + x
End Function

Public Sub Test()
MsgBox(Me.Bar(10))
MsgBox(MyClass.Bar(10))
End Sub
End Class

Class Foo2 : Inherits Foo
Public Overrides Function Bar(ByVal x as Integer) As Integer
Return x * x
End Function
End Class

Sub Main()
Dim f2 as New Foo2()
f2.Test()
' First MsgBox shows 100 - Foo2's implementation of Bar
' Second MsgBox shows 20 - Foo's implementation of Bar
End Sub

--
Rob Windsor [MVP-VB]
G6 Consulting
Toronto, Canada
 
So how do you ensure you are always calling the same function even if it is overriddden? I guess you could create a private function
and call that, but maybe there is an easier way.

--
Michael Culley


Rob Windsor said:
There is no equivalent to MyClass in C#.

Me = this
MyBase = base
MyClass has no equivalent

--
Rob Windsor [MVP-VB]
G6 Consulting
Toronto, Canada


FDude said:
In otherwords, how do I force a method in my class to call the method
implementations in the SAME class and not use any potentially overriden
methods in derived classes?
 
Michael Culley said:
So how do you ensure you are always calling the same function even if it
is overriddden? I guess you could create a private function
and call that, but maybe there is an easier way.
Usually you don't worry about it, you should design in such a way that you
don't need to, however a private method is more or less your only option if
you *have* to call your local method.
Can I ask why you need to use a local method without virtuality?
--
Michael Culley


There is no equivalent to MyClass in C#.

Me = this
MyBase = base
MyClass has no equivalent

--
Rob Windsor [MVP-VB]
G6 Consulting
Toronto, Canada


FDude said:
In otherwords, how do I force a method in my class to call the method
implementations in the SAME class and not use any potentially overriden
methods in derived classes?
 
Back
Top