Solved

Inheriting a class to call a function unknown until runtime.

Posted on 2011-09-22
10
280 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

895 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

14 Experts available now in Live!

Get 1:1 Help Now