Jump to content
Xtreme .Net Talk

Is it possible for a Base Class to call a function in a Child Class?


Recommended Posts

Posted

Seems funny, but Typed TableAdapters inherit from the Component Class. But, they offer the functionalty for you to choose another Base Class that also inherits from Component.

 

So, knowing there will always be an Update Method in a Typed TableAdapter, is there a way to call it from the Base Class?

  • Administrators
Posted
If you have control over the base class then you can either provide a virtual method yourself that can be overrriden in a derived class, or make the base class abstract and provide an abstract method that must then be overrriden in the derived class.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted
If you have control over the base class then you can either provide a virtual method yourself that can be overrriden in a derived class' date=' or make the base class abstract and provide an abstract method that must then be overrriden in the derived class.[/quote']

 

That looks like an awful lot of text to say no, but I don't see you mentioning any way to call a procedure in a child class, so I guess it is.

 

See, the problem here is that you're talking about changing or customizing the child class to allow for this. A Typed DataSet does let you alter the code, though it's dynamically generated and changes would be overrwritten by Visual Studio.

 

The Typed Dataset lets you choose a Base Class, not change the autogenerated code. I'm sure I could rig something up with partial classes, but then I'm back to adding code to every dataset file rather than using OOP techniques to save code/time.

Posted

If you've no aversion to black arts there is always reflection:

 

using System.Reflection;

//(Untested, but should be ok)
private void InvokeUpdate(...)
{
   MethodInfo method;

   try {
       method = this.GetType().GetMethod("Update");  //Get the method
       method.Invoke(this, ...);

   } catch (Exception e) {
       //Do something with e, please
   }
}

 

Note that ... is a placeholder for whatever parameters this method takes. Also, if you're going to be calling this method many times, it might be advisable to store the MethodInfo in a class-level variable (though I'm not sure if this is possible in a constructor...).

 

Good luck! :cool:

Never trouble another for what you can do for yourself.
Posted

I've always wanted to dabble in the Dark Arts of Reflection. It looks extremely useful.

 

I'll try to hack that to VB later on today and see how that flies :D

Posted (edited)

Okay, got it working flawlessly. MrPaul had me about 90% the way there and on the path, just some hitches I had to work through :D

 

Imports System.Reflection

''' <summary>
''' BaseDataAdapter is inherits from ComponentModel.Component and will be set as BaseClass for all
''' Typed TableAdapters to allow a common code base so generic procedures can be created. 
''' </summary>
''' <remarks>
''' Created by: Denaes
''' Created on: 07/19/2006
''' </remarks>
Public Class BaseDataAdapter
   Inherits System.ComponentModel.Component
   Implements ITableAdapter

   Public Sub InvokeUpdate(ByVal Table As DataTable) Implements ITableAdapter.InvokeUpdate
       Debug.Print("Invoke Update procedure for " & Table.TableName & " begun.")
       If Table IsNot Nothing Then
           '  Method to invoke.
           Dim UpdateMethod As MethodInfo
           '  Array of Methods parameter signiture types.
           Dim UpdateTypes As Type() = {Table.GetType}
           '  Not used, just a dummy value.
           Dim UpdateModifiers() As ParameterModifier = {New ParameterModifier}
           '  Array of Parameters to pass into the Method being Invoked.
           Dim arrUpdateParameters() As Object = {Table}

           Try
               '  Assigns the Method matching the given signature to UpdateMethod.
               UpdateMethod = Me.GetType.GetMethod("Update", UpdateTypes, UpdateModifiers)

               '  Invokes the UpdateMethod with the the Array of Parameters given.
               UpdateMethod.Invoke(Me, arrUpdateParameters)
           Catch ex As Exception

               '  This is a serious error and needs attention if something here fails.
               MessageBox.Show("Error with InvokeUpdate: " & vbNewLine & ex.ToString)

           End Try
       Else
           Debug.Print("Invoke Update for " & Table.TableName & " canceled. Table is nothing.")
       End If

   End Sub
End Class

 

Sorry, it stripped out all the whitespace.

 

Well this shows how to create a TableAdapter Base Class that you can set for your Typed DataAdapters. This lets you pass a ITableAdapter Type into a generic procedure which can call the InvokeUpdate procedure which in turn will Invoke the Update method of the type of table you passed in.

 

Reflection isn't the quickest thing, but so far it's the only way I've found to do this. It's not really impacting my performance since this application is doing updates one at a time... maybe batch deletes at times. Any other batch updates will be performed on the database side as management.

 

I have my theories about how this might be sped up for a large batch of updates, primarily by moving UpdateMethod out of the procedure and only setting its value if it's nothing. That would remove most of the code from the equation on multiple calls.

Edited by Denaes

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...