473,320 Members | 1,744 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Casting unknown type

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?
Feb 28 '07 #1
3 1562
John <jh******@msn.comwrote:
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 - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 28 '07 #2

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
>
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()
Feb 28 '07 #3
John <jh******@msn.comwrote:
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.
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 28 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jacob Jensen | last post by:
This question has probably been asked a million time, but here it comes again. I want to learn the difference between the three type cast operators: static_cast, reinterpret_cast, dynamic_cast. A...
3
by: Rob Jackson | last post by:
HiI've got a struct, known by file A.c, which contains a pointer to struct B. Struct B is unknown by file A.c (it is declared in C.h), and contains a typedef enum, which is declared in a file B.h,...
6
by: Philipp Schumann | last post by:
Hi, I have a need for "dynamic type casting": in other words, in a "MyConvert" method I get passed an Object "value" and a Type "type" and the method should attempt to convert value into type. ...
2
by: Tom Larard | last post by:
Hi, We have an app which uses the latest version of perl DBI and DBD::Pg to execute a query, using placeholders. It fails to cast the float I send into a number and generates the following...
31
by: Twister | last post by:
Hi All, I have a question which might sound very basic. I have a simple structure: struct simple{ void *buffer; }; typedef struct simple Simple;
4
by: ranjeet.gupta | last post by:
Dear All Please check the below code: UINT8 MsgLength = 0; MsgLength = strlen((char *)msg); if ( MsgLength == 0 || MsgLength 64) {
6
by: Spoon | last post by:
Hello everyone, I'm writing code where I can receive two kinds of packets. The first 12 octets are common to both packet types, i.e. they have the same semantics. The 1st type has 4 more...
7
by: S. Lorétan | last post by:
Hi guys, Sorry for this stupid question, but I don't know why it isn't working. Here is my (example) code: namespace Test { class A { public string Label1; }
29
by: Tony Johansson | last post by:
Hello! Here I have two different way to use casting. Is any of these any better then the other? Or is it just a question of taste. newCards.Add((Card)sourceCard.Clone());...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.