471,350 Members | 1,957 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

dynamic type at runtime?

I have a asp.net page with a method to track down a dynamic control....

private void findAndSetTheControl(Control theControl, object theType, string
nameOfControl)
{
theControl = (theType)(PH.FindControl(nameOfControl));
}
It gets called here and a couple of other places:

public void findAndSetThePaymentControl()
{
findAndSetTheControl(thePaymentControl, PaymentControl,
nameOfPaymentControl);
}

What I would like to do is dynamically type (theType) it at runtime. Is
this possible without using some sort of abstract class and doing the
kabookie dance? I know that GetType() wiill return the objects type but im
not really sure how to use it at runtime.

Thanks in advance for the feedback.
Sean
Feb 3 '06 #1
6 1278
> I have a asp.net page with a method to track down a dynamic
control....

private void findAndSetTheControl(Control theControl, object theType,
string
nameOfControl)
{
theControl = (theType)(PH.FindControl(nameOfControl));
}
It gets called here and a couple of other places:

public void findAndSetThePaymentControl()
{
findAndSetTheControl(thePaymentControl, PaymentControl,
nameOfPaymentControl);
}

What I would like to do is dynamically type (theType) it at runtime.
Is this possible without using some sort of abstract class and doing
the kabookie dance? I know that GetType() wiill return the objects
type but im not really sure how to use it at runtime.

Thanks in advance for the feedback.

Sean


Since you know the type of the control in your calling method, why not just
return the control from FindTheControl()
and cast it in your calling method?

Control FindTheControl(string nameOfControl)
{
return PH.FindControl(nameOfControl);
}

void FindAndSetThePaymentControl()
{
PaymentControl control = FindTheControl("paymentControl") as PaymentControl;

...yada yada yada...and no kabookie dance.
}
Feb 3 '06 #2
Sean,

All you could do is use reflection, and trust that the methods have the
name and signature you want. Of course, this is a horrific practice, and
you should just use an interface which all of the objects implement.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"netnet" <ne********@comcast.net> wrote in message
news:u3*************@TK2MSFTNGP12.phx.gbl...
I have a asp.net page with a method to track down a dynamic control....

private void findAndSetTheControl(Control theControl, object theType,
string nameOfControl)
{
theControl = (theType)(PH.FindControl(nameOfControl));
}
It gets called here and a couple of other places:

public void findAndSetThePaymentControl()
{
findAndSetTheControl(thePaymentControl, PaymentControl,
nameOfPaymentControl);
}

What I would like to do is dynamically type (theType) it at runtime. Is
this possible without using some sort of abstract class and doing the
kabookie dance? I know that GetType() wiill return the objects type but
im not really sure how to use it at runtime.

Thanks in advance for the feedback.
Sean

Feb 3 '06 #3
Thanks for the quick feedback guys. Good point Chris. Smalltalk has ruined
me, some things are just sooooo much simpler. Nicholas, are you feelings
on reflection due to the performance problems I have read about? It doesnt
really seem like there is a clean way to a number of things in C#? I havent
found a really nice way to do a vistor pattern without it? If anyone has an
example please share.

Thanks again.

Sean

"chris martin" <chris_m|NOSPAM|@caliber|SPAM|web.com> wrote in message
news:44**************************@news.easynews.co m...
I have a asp.net page with a method to track down a dynamic
control....

private void findAndSetTheControl(Control theControl, object theType,
string
nameOfControl)
{
theControl = (theType)(PH.FindControl(nameOfControl));
}
It gets called here and a couple of other places:

public void findAndSetThePaymentControl()
{
findAndSetTheControl(thePaymentControl, PaymentControl,
nameOfPaymentControl);
}

What I would like to do is dynamically type (theType) it at runtime.
Is this possible without using some sort of abstract class and doing
the kabookie dance? I know that GetType() wiill return the objects
type but im not really sure how to use it at runtime.

Thanks in advance for the feedback.

Sean


Since you know the type of the control in your calling method, why not
just return the control from FindTheControl()
and cast it in your calling method?

Control FindTheControl(string nameOfControl)
{
return PH.FindControl(nameOfControl);
}

void FindAndSetThePaymentControl()
{
PaymentControl control = FindTheControl("paymentControl") as
PaymentControl;

...yada yada yada...and no kabookie dance.
}

Feb 3 '06 #4
Sean,

Well, performance has something to do with it, but it's not just that.

Basically, you have an expectation that these objects should fufill, and
that is most easily defined by an interface. This also allows you to
perform compile-time checks to ensure that anyone calling your method abides
by the contract. This is VASTLY better than having it blow up in your face
at run-time.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"netnet" <ne********@comcast.net> wrote in message
news:OD**************@tk2msftngp13.phx.gbl...
Thanks for the quick feedback guys. Good point Chris. Smalltalk has
ruined me, some things are just sooooo much simpler. Nicholas, are you
feelings on reflection due to the performance problems I have read about?
It doesnt really seem like there is a clean way to a number of things in
C#? I havent found a really nice way to do a vistor pattern without it?
If anyone has an example please share.

Thanks again.

Sean

"chris martin" <chris_m|NOSPAM|@caliber|SPAM|web.com> wrote in message
news:44**************************@news.easynews.co m...
I have a asp.net page with a method to track down a dynamic
control....

private void findAndSetTheControl(Control theControl, object theType,
string
nameOfControl)
{
theControl = (theType)(PH.FindControl(nameOfControl));
}
It gets called here and a couple of other places:

public void findAndSetThePaymentControl()
{
findAndSetTheControl(thePaymentControl, PaymentControl,
nameOfPaymentControl);
}

What I would like to do is dynamically type (theType) it at runtime.
Is this possible without using some sort of abstract class and doing
the kabookie dance? I know that GetType() wiill return the objects
type but im not really sure how to use it at runtime.

Thanks in advance for the feedback.

Sean


Since you know the type of the control in your calling method, why not
just return the control from FindTheControl()
and cast it in your calling method?

Control FindTheControl(string nameOfControl)
{
return PH.FindControl(nameOfControl);
}

void FindAndSetThePaymentControl()
{
PaymentControl control = FindTheControl("paymentControl") as
PaymentControl;

...yada yada yada...and no kabookie dance.
}


Feb 3 '06 #5
> Thanks for the quick feedback guys. Good point Chris. Smalltalk has
ruined me, some things are just sooooo much simpler. Nicholas, are
you feelings on reflection due to the performance problems I have read
about? It doesnt really seem like there is a clean way to a number of
things in C#? I havent found a really nice way to do a vistor pattern
without it? If anyone has an example please share.

Thanks again.

Sean

http://www.dofactory.com/Patterns/Pa...or.aspx#_self2

On that page there is a nice, clean example of the Visitor pattern in .NET
or any OOL at that.

Chris
Feb 3 '06 #6
Thanks for the link Chris. Good point Nicholas. This is an example of the
concept I seem to be having a tough time w in C#:

Payment
-amount

Check : Payment
-checkNumber

CreditCard : Payment
-ccNumber
-ccExpiration

Now when I go to use this class structure .....I have to create a container,
populate it, AND all methods have to be implemented on payment at least as
abstracts...which seems like a lot of work.

I dont really need Payment or Check to know anything about ccExpiration for
example.

This is how I have been doing it ...Please let me know if you have cleaner
approach
Payment[] aPayment = new Payment[1];

if(someCondition)
aPayment[0] = new CreditCard(info);
else
aPayment[0] = new Check(info);
Theres a better way right?

Thanks again

Sean
Feb 3 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by Jason Tesser | last post: by
3 posts views Thread by prashna | last post: by
3 posts views Thread by Stephen Gennard | last post: by
2 posts views Thread by Luis Arvayo | last post: by
13 posts views Thread by DaTurk | last post: by
28 posts views Thread by sturlamolden | last post: by
reply views Thread by EDBrian | last post: by
reply views Thread by XIAOLAOHU | last post: by

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.