473,569 Members | 2,611 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

dynamic type at runtime?

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

private void findAndSetTheCo ntrol(Control theControl, object theType, string
nameOfControl)
{
theControl = (theType)(PH.Fi ndControl(nameO fControl));
}
It gets called here and a couple of other places:

public void findAndSetThePa ymentControl()
{
findAndSetTheCo ntrol(thePaymen tControl, PaymentControl,
nameOfPaymentCo ntrol);
}

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 1337
> I have a asp.net page with a method to track down a dynamic
control....

private void findAndSetTheCo ntrol(Control theControl, object theType,
string
nameOfControl)
{
theControl = (theType)(PH.Fi ndControl(nameO fControl));
}
It gets called here and a couple of other places:

public void findAndSetThePa ymentControl()
{
findAndSetTheCo ntrol(thePaymen tControl, PaymentControl,
nameOfPaymentCo ntrol);
}

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 FindAndSetThePa ymentControl()
{
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.co m
"netnet" <ne********@com cast.net> wrote in message
news:u3******** *****@TK2MSFTNG P12.phx.gbl...
I have a asp.net page with a method to track down a dynamic control....

private void findAndSetTheCo ntrol(Control theControl, object theType,
string nameOfControl)
{
theControl = (theType)(PH.Fi ndControl(nameO fControl));
}
It gets called here and a couple of other places:

public void findAndSetThePa ymentControl()
{
findAndSetTheCo ntrol(thePaymen tControl, PaymentControl,
nameOfPaymentCo ntrol);
}

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.easyne ws.com...
I have a asp.net page with a method to track down a dynamic
control....

private void findAndSetTheCo ntrol(Control theControl, object theType,
string
nameOfControl)
{
theControl = (theType)(PH.Fi ndControl(nameO fControl));
}
It gets called here and a couple of other places:

public void findAndSetThePa ymentControl()
{
findAndSetTheCo ntrol(thePaymen tControl, PaymentControl,
nameOfPaymentCo ntrol);
}

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 FindAndSetThePa ymentControl()
{
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.co m

"netnet" <ne********@com cast.net> wrote in message
news:OD******** ******@tk2msftn gp13.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.easyne ws.com...
I have a asp.net page with a method to track down a dynamic
control....

private void findAndSetTheCo ntrol(Control theControl, object theType,
string
nameOfControl)
{
theControl = (theType)(PH.Fi ndControl(nameO fControl));
}
It gets called here and a couple of other places:

public void findAndSetThePa ymentControl()
{
findAndSetTheCo ntrol(thePaymen tControl, PaymentControl,
nameOfPaymentCo ntrol);
}

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 FindAndSetThePa ymentControl()
{
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...whi ch 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(someConditio n)
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
2158
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 registration to business office. We are considering useing Java or Python. I for one don't like Java because I feel the GUI is clunky. I also think...
3
1624
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 fun(){printf("HIIIIII\n");}
3
1312
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 below... --
4
2025
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 = .LoadFrom("D:\vb7Project\dynamicCall\prj01\prj01\bin\prj01.dll") Dim ty As Type = asm.GetType("prj01.dbLib")
2
3625
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
3027
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 have two object which have a common base class, they can both be cast up to the base class, but if either of the child classes have unuque methods...
28
2558
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 languages like Python or Lisp (thus the cross-posting). Apparently is is distributed under a BSD-like open-source license. I am curious to know how it...
3
18689
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, working with complex reports is tricky Assumption: Reader of this article have basic knowledge of creating data reports. Creating a Parent-Child...
0
3179
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: http://shrinkster.com/tzp So I have a class called "ClientTable". All tables have some common fields, but I need to add more at runtime. Now I have a class that...
0
7614
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8125
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7676
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7974
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5513
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3642
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1221
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.