Solved

Inheriting a class to call a function unknown until runtime.

Posted on 2011-09-22
10
286 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

635 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