Casting unknown type

  • Thread starter Thread starter John
  • Start date Start date
J

John

How do I cast a type that can be a number of different calling pages in
asp.net. _Default is used in the example but it could be another page type.

Example :

_Default myPage = (_Default)this.Page;
myPage.Master.SearchMake = e.Item.ToString();
myPage.Master.SearchType= e.Item.ToString();
myPage.Master.SearchText = e.Item.ToString();
Server.Transfer("Search.aspx",true);

If the running pages is not of _Default, how do I do this?
 
John said:
How do I cast a type that can be a number of different calling pages in
asp.net. _Default is used in the example but it could be another page type.

Example :

_Default myPage = (_Default)this.Page;
myPage.Master.SearchMake = e.Item.ToString();
myPage.Master.SearchType= e.Item.ToString();
myPage.Master.SearchText = e.Item.ToString();
Server.Transfer("Search.aspx",true);

If the running pages is not of _Default, how do I do this?

Well, what *do* you know about the type? It looks like you know it will
have a Master property of a particular type. So encapsulate that in an
interface, make all the relevant pages implement that interface, and
cast to that interface.
 
Jon Skeet said:
Well, what *do* you know about the type? It looks like you know it will
have a Master property of a particular type. So encapsulate that in an
interface, make all the relevant pages implement that interface, and
cast to that interface.

Well, I implemented the interface in the master page so that other pages can
implement this interface. I have to say I'm not that experienced with
interfaces and what I read isn't shedding any light on this for me.

Master Page :

interface ISearchMake
{
string GetSearchMake();
}
public partial class Main : System.Web.UI.MasterPage
{
string _SearchMake = "";

public string GetSearchMake
{
get {return _SearchMake;}
}

Single Page :

public partial class Search : System.Web.UI.Page, ISearchMake
{
protected void Page_Load(object sender, EventArgs e)
{
// Do stuff
}
}

My Control (can appear on any page) :

public partial class Search : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
CF.ISearchMake MySource;
MySource = (CF.ISearchMake)Context.Handler;
Literal1.Text = MySource.GetSearchMake();
}
}
}

Gives me an error :

Error 1 'CF.Search' does not implement interface member
CF.ISearchMake.GetSearchMake()
 
John said:
Well, I implemented the interface in the master page so that other pages can
implement this interface. I have to say I'm not that experienced with
interfaces and what I read isn't shedding any light on this for me.

It's the pages that you'll be casting that need to implement an
interface first - and that's an interface that just says that their
MasterPage property returns a page of a certain type.

Unfortunately, it's not terribly clear all the types that are involved
in your question. Still, think about what they have in common - that's
what should be in the interface.
 
Back
Top