Solved

Inheriting a class to call a function unknown until runtime.

Posted on 2011-09-22
10
279 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
 

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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now