Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Inheriting a class to call a function unknown until runtime.

Posted on 2011-09-22
10
Medium Priority
?
289 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 2000 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

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.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

824 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