473,396 Members | 1,748 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,396 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 1334
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Jason Tesser | last post by:
I work for at a college where I am one of 2 full-time developers and we are looking to program a new software package fro the campus. This is a huge project as it will include everything from...
3
by: prashna | last post by:
Hi all, Is'nt a function invocation through a function pointer is dynamic binding? For example consider the following program 1 int main() 2 { 3 int (*fun_ptr)(); 4 int...
3
by: Stephen Gennard | last post by:
Hello, I having a problem dynamically invoking a static method that takes a reference to a SByte*. If I do it directly it works just fine. Anyone any ideas why? I have include a example...
4
by: Tom | last post by:
Hi .Net Professional : In recent , I starting to learn about dynamic call .net object in vb.net. Before, some IT people teach me use the following method and its work !! Dim asm As =...
2
by: Luis Arvayo | last post by:
Hi, In c#, I need to dynamically create types at runtime that will consist of the following: - inherits from a given interface - will have a constructor with an int argument
13
by: DaTurk | last post by:
Hi, This is a question brought about by a solution I came up with to another question I had, which was "Dynamic object creation". So, I'm curious if you can dynamically cast an object. If you...
28
by: sturlamolden | last post by:
On Monday Microsoft announced a new runtime for dynamic languages, which they call "DLR". It sits on top of the conventional .NET runtime (CLR) and provides services for dynamically typed...
3
by: creative1 | last post by:
Here is how you create a complex data report that involves parent and child commands and you can update information at runtime. Its pretty straight forward to work with simple queries; however,...
0
by: EDBrian | last post by:
Problem: We enable our clients to create custom fields they wish to collect. We want to use Dynamic LINQ to query this table (Just one). My initial post and suggestion(s) are here:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.