Object Reference to MyBase

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

When a class (myownclass) inheirits another class, how can I get an object
reference to the underlyng MyBase class instance from within myownclass. The
base class has a method that I want to utilize in myownclass but the method
is private and can't be inheirited. I need to utilize that base class
method.
 
Can you make the base class method protected? That way only the derived
classes can access the method - in a way its 'private' to the base class and
all classes that derive from it. Then you can simply call
MyBase.BaseClassMethod or infact you can also call Me.BaseClassMethod since
the derived class inherits this method.

Apart from that, AFAIK, there's no direct way to access the private methods
of the base class. You would have to resort to using Reflection to access
the method.

hope that helps..
Imran.
 
The base class I am inheriting is a VB.Net control and I have no control over
the scope of it's methods. Do you have any idea how I can use reflection to
get at the method of the base class? It's not an exposed method that you can
call using MyBase.baseclassmethod.
 
Dennis,

Note that although the MyBase keyword is used to reference the base class,
the instance being used is the current instance of the class - meaning the
derived class instance.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/vakeymybase.asp

MyBase does not refer to an instance of the base class - it refers to the
derived class instance but in the context of the base class so that you can
access the base class methods/properties/etc (only protected, friend and
public). So if you need a reference to an instance of the base class, you'll
have to create one yourself. Since you know the class you're deriving from
at compile time itself, you can create an instance directly just as you
would for any other type at compile time and then invoke the private method
using Reflection:

Dim baseobject As New mybaseclass
baseobject.GetType.InvokeMember("privatesub", _
Reflection.BindingFlags.Instance Or _
Reflection.BindingFlags.NonPublic Or _
Reflection.BindingFlags.InvokeMethod, _
Nothing, baseobject, New Object() {})

However, you would have to consider the state of the object when trying to
invoke the private method of the base class. The executing of the private
method within the base class object would be occurring when the object has
reached a particular state. This wouldn't be the case for you when you just
create the instance and invoke the private method directly. The base class
object might not be in the right state and the execution of the private
method might not give you the right results or results that you are
expecting. But if you know that this isn't going to be an issue, then you
should be fine. Just wanted to point out the repercussions of directly
invoking a private method.

hope that helps...
Imran.
 
Dennis,

I don't think my previous comment would have answered your question
completely. As I mentioned, the MyBase keyword refers to the current
instance of the derived class. As far as private methods (or properties,
etc) go, when a class derives from a base class, the private methods are not
inherited and so there is nothing like accessing the private methods of the
base class! When you look at a derived class, there's just one class - the
derived class - that has its own members and members inherited from the base
class. Since private members are not inherited, the derived class simply
doesn't have them and is completely unaware of them. So there's no question
about executing a private method of the base class in the context of the
derived class since that doesn't make sense at all, IMO.

I hope that clears things up a bit..
Imran.
 
Imran, thank you very much for your time in answering my question. What I'm
trying to do is set the rowheights in class that inheirits the datagrid
control. The following works for the Datagrid class but doesn't work for
inheirited classes where dg is datagrid object (or my class that inheirits
the datagrid class). The "get_DataGridRows" is a private method, I'm told,
in the DataGrid control.

Dim mi As MethodInfo = dg.GetType().GetMethod("get_DataGridRows", _
BindingFlags.FlattenHierarchy Or BindingFlags.IgnoreCase Or _
BindingFlags.Instance Or BindingFlags.NonPublic Or BindingFlags.Public Or _
BindingFlags.Static)

Dim dgra As System.Array = CType(mi.Invoke(Me.dg, Nothing), System.Array)

I understand what you are saying about the private methods of the class
that's being inheirited not accessable but there must be some way to get at
the rowobject collection of the datagrid base class. I'll play around with
your reflection example some and see what I can do. Thanks again.
 
That worked GREAT. Here's the code I ended up with. Note that it's set up
in my derived DataGrid Class called mydatagrid and update the RowObject array
of rows whenever the datasource changes or a row is changed with the mouse:

Public Class mydatagrid
Private RowObjects as Arraylist = new Arraylist

'Call this whenever datasource changes or row height changed by mouse
Private Function get_RowHeights() As ArrayList
Dim rows As ArrayList = New ArrayList
Dim a As Type = Me.GetType
Dim b As Type = a.BaseType
Dim mi As MethodInfo = b.GetMethod("get_DataGridRows",
BindingFlags.FlattenHierarchy Or BindingFlags.IgnoreCase Or
BindingFlags.Instance Or BindingFlags.NonPublic Or BindingFlags.Public Or
BindingFlags.Static)
Dim dgra As System.Array = CType(mi.Invoke(Me, Nothing), System.Array)
Dim dgrr As Object
For Each dgrr In dgra
If dgrr.ToString().EndsWith("DataGridRelationshipRow") = True Then
rows.Add(dgrr)
End If
Next dgrr
Return rows
End Function 'get_RowHeights

'Use this Property to set or get rowheights
Public Property RowHeight(ByVal row As Integer) As Integer
Get
Try
Dim pi As PropertyInfo =
rowObjects(row).GetType().GetProperty("Height")
Return CInt(Fix(pi.GetValue(rowObjects(row), Nothing)))
Catch
Throw New ArgumentException("invalid row index")
End Try
End Get
Set(ByVal Value As Integer)
Try
Dim pi As PropertyInfo =
rowObjects(row).GetType().GetProperty("Height")
pi.SetValue(rowObjects(row), Value, Nothing)
Catch
Throw New ArgumentException("invalid row index")
End Try
End Set
End Property

Thanks a lot for helping me.
 
Imran, the code I posted that I said worked, works very well with derived
datagrid classes that are bound to ArrayLists. However, I've been testing it
with DataTables bound to a derived datagrid class and it doesn't work. Sorry
about the confusion.
 
Dennis said:
Imran, the code I posted that I said worked, works very well with derived
datagrid classes that are bound to ArrayLists. However, I've been testing
it
with DataTables bound to a derived datagrid class and it doesn't work.
Sorry
about the confusion.

hmmm...well...I wonder why that discrepancy. Sorry - I don't have enough
time to test it out but I hope you can work that out somehow. Good Luck !

Imran.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top