Solved

Inheriting a class to call a function unknown until runtime.

Posted on 2011-09-22
10
282 Views
Last Modified: 2012-05-12
Hi,

I am loading in usercontrols dynamically at runtime. Each UserControl inherits from baseUC. The baseUC has I made called InitializeMe()...
public Overridable Sub InitializeMe()
End Sub
...and each UserControl that inherits baseUC has...
public sub Overrides InitializeMe()
end sub
...and I want to make a call to InitializeMe in the UserControl not baseUC...
CType(uc, baseUC).InitializeMe()
...however it just calls the baseUC.InitializeMe().

I put the baseUC.InitializeMe sub in there so VWD likes it at design time. I have tried using...
Public MustOverride Sub InitializeMe but it says I must have MustInherit on baseUC but I don't want to do that as there are other functions that are used in baseUC.

I am not sure how to tackle this code and your help is greatly appreciated.
0
Comment
Question by:MonCapitan
10 Comments
 
LVL 25

Expert Comment

by:Luis Pérez
ID: 36579580
Try calling InitializeMe without casting:

CallByName(uc, "InitializeMe", CallType.Method)

Hope that helps.
0
 
LVL 16

Expert Comment

by:Easwaran Paramasivam
ID: 36579735
Replace CType(uc, baseUC).InitializeMe() with  baseUC.InitializeMe()
0
 
LVL 8

Expert Comment

by:jagrut_patel
ID: 36579765
Since uc reference variable is of type UserControl which is derived from baseUC it will always call UserControl's version of InitializeMe if UserControl has overridden it.

I think only way here is
1. Define another Overridable function in baseUC with empty body.
baseUC
{
  public virtual void InitializeBaseOnly()
 {
  //No code here.
 }
}
2. In UserControl override InitializeBaseOnly to only call baseUC's InitializeMe. Like this

UserControl: baseUC
{
   public override void InitializeBaseOnly()
    {
       base.InitializeMe()
    }
}

Thus, when we call

uc.InitializeBaseOnly()

it invokes baseUC's InitializeMe().
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:MonCapitan
ID: 36580080
Thanks for the replies.

By doing any of the preceeding two suggestions, I will be executing the baseUC.InitializeMe rather than the usercontrol that is loaded in at runtime, i.e. UCLoadedAtRuntime.InitializeMe().

I wanted to use class hierarchy and oop to run UCLoadedAtRuntime.InitializeMe() but I am not doing it right and need help with that (not that I am completely dismissing RolandDeschain suggestion)

How can I do this in an oop way?
0
 
LVL 8

Accepted Solution

by:
jagrut_patel earned 500 total points
ID: 36580211
Sorry I am not understanding why your code isn't calling UserControl's InitializeMe(). Let me put one simple OOP example to better understand your issue.

public class baseUC
{
    public virtual void InitializeMe()
    {
        Console.WriteLine("baseUC initialized");
    }
}

public class MyUserControl : baseUC
{
    public override void InitializeMe()
    {
        Console.WriteLine("MyUserControl initialized");
    }
}

Open in new window



Now if we do,

baseUC UCLoadedAtRuntime = new MyUserControl(); //UserControl loaded at run-time
UCLoadedAtRuntime.InitializeMe();

Open in new window


The last line "UCLoadedAtRuntime.InitializeMe();" will always call InitializeMe() version of MyUserControl class.

Can you please help me understand how this is different from your problem?
0
 

Author Comment

by:MonCapitan
ID: 36580386
Yes this is the same code I have and it is showing the result from the baseUC.InitializeMe() class.

 
Public Class baseUC
    Inherits UserControl

    Public Overridable Sub InitializeMyControl()
	Console.WriteLine("baseUC initialized");
    End Sub
End Class

Public Partial Class MyUserControl
    Inherits baseUC

    Public Overrides Sub InitializeMyControl()
        ' Do control specific initialization.
	Console.WriteLine("MyUserControl initialized");
    End Sub
End Class

Open in new window


...then I do this...

 
Public Class baseUC
    Inherits UserControl

    Public Overridable Sub InitializeMyControl()
	Console.WriteLine("baseUC initialized");
    End Sub
End Class

Public Partial Class MyUserControl
    Inherits baseUC

    Public Overrides Sub InitializeMyControl()
        ' Do control specific initialization.
	Console.WriteLine("MyUserControl initialized");
    End Sub
End Class

Open in new window


...and the result is this...

"baseUC initialized"
Dim MyRunTimeUC As baseUC = MyPage.LoadControl("~/PathToUserControl.ascx")
MyRunTimeUC.InitializeMyControl()

Open in new window

0
 

Author Comment

by:MonCapitan
ID: 36580399
Sorry I showed two lots of one code. Forget the second code block
0
 

Author Comment

by:MonCapitan
ID: 36580911
Ok. As it turns out, I was doing something wrong and would have never of realized if it wasn't for jagrut_patel.

Thanks for your time with this.
0
 
LVL 40
ID: 36580941
You are loading the base control instead of the inherited control

Dim MyRunTimeUC As baseUC = MyPage.LoadControl("~/PathToUserControl.ascx")

Do this instead

Dim MyRunTimeUC As MyUserControl = MyPage.LoadControl("~/PathToUserControl.ascx")
0
 
LVL 8

Expert Comment

by:jagrut_patel
ID: 36584958
Happy to know it helped. My pleasure!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question