473,789 Members | 2,668 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need help with casting

I have a file that provides details on which user control to populate a web
page with at runtime. I'm able to create and load the control successfully,
but now I need to cast it from Control to its specific type so I can access
it's specific members. I have the type as a string ("MyUserControl " for
example). So, having the name, how do I cast it from Control to this type?
Thanks...
Keith
Nov 16 '05 #1
6 1220
Keith Selbee wrote:
I have a file that provides details on which user control to populate a web
page with at runtime. I'm able to create and load the control successfully,
but now I need to cast it from Control to its specific type so I can access
it's specific members. I have the type as a string ("MyUserControl " for
example). So, having the name, how do I cast it from Control to this type?
Thanks...

If you know up front what types the control could be an instance of, you
could use something along the lines of:

if (control is MyUserControl) {
MyUserControl myUserControl = (MyUserControl) control;
// Do MyUserControl specific stuff
}

if (control is AnotherControl) {
AnotherControl anotherControl = (AnotherControl )control;
// Do AnotherControl specific stuff
}

Keith

Nov 16 '05 #2
Check out the Assembly.GetTyp e method. If you are already in the
corresponding assembly, use

Assembly.GetExe cutingAssembly method to get instance of Assembly and then
call GetType method by passing type name. You may need full type name (with
namespace and all).

"Keith Selbee" <ks*****@nospam .neo.rr.com> wrote in message
news:uM******** ******@TK2MSFTN GP09.phx.gbl...
I have a file that provides details on which user control to populate a web page with at runtime. I'm able to create and load the control successfully, but now I need to cast it from Control to its specific type so I can access it's specific members. I have the type as a string ("MyUserControl " for
example). So, having the name, how do I cast it from Control to this type? Thanks...
Keith

Nov 16 '05 #3
Keith Selbee <ks*****@nospam .neo.rr.com> wrote:
I have a file that provides details on which user control to populate a web
page with at runtime. I'm able to create and load the control successfully,
but now I need to cast it from Control to its specific type so I can access
it's specific members. I have the type as a string ("MyUserControl " for
example). So, having the name, how do I cast it from Control to this type?


If you only have the type as a string, how do you know at compile time
what its specific members are? If you don't know at compile time what
its specific members are, what good do you believe casting would give
you? If you know its specific members at compile time, you'd be best
off encapsulating them in an interface which your control implements,
and then casting to that interface.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
I'm not 100% sure what you're trying to do, but I think you can cast your
control to whatever the type you want to cast to as long as you inherit the
control from the type you want to cast to. The object hierarchy would be
like...

Control
|---- Your abstract class
|-------------Your control
"Keith Selbee" <ks*****@nospam .neo.rr.com> wrote in message
news:uM******** ******@TK2MSFTN GP09.phx.gbl...
I have a file that provides details on which user control to populate a web page with at runtime. I'm able to create and load the control successfully, but now I need to cast it from Control to its specific type so I can access it's specific members. I have the type as a string ("MyUserControl " for
example). So, having the name, how do I cast it from Control to this type? Thanks...
Keith

Nov 16 '05 #5
My hope is that I can have an xml file specifying the type of the user
control and a list of properties specific to that type and then populate
them at run time. I just asked for the first part though.

Keith

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Keith Selbee <ks*****@nospam .neo.rr.com> wrote:
I have a file that provides details on which user control to populate a web page with at runtime. I'm able to create and load the control successfully, but now I need to cast it from Control to its specific type so I can access it's specific members. I have the type as a string ("MyUserControl " for
example). So, having the name, how do I cast it from Control to this
type?
If you only have the type as a string, how do you know at compile time
what its specific members are? If you don't know at compile time what
its specific members are, what good do you believe casting would give
you? If you know its specific members at compile time, you'd be best
off encapsulating them in an interface which your control implements,
and then casting to that interface.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #6
Keith Selbee <ks*****@nospam .neo.rr.com> wrote:
My hope is that I can have an xml file specifying the type of the user
control and a list of properties specific to that type and then populate
them at run time. I just asked for the first part though.


But if you've got the list of properties as strings as well, rather
than hard-coded, you'll be using reflection to set them - so you don't
need to cast anyway.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7

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

Similar topics

13
1935
by: JustSomeGuy | last post by:
I have two object types ClassA and ClassB class ClassA { public: int data; operator ClassB() { ClassB b; b.data = data + 1; return (b);
2
2277
by: Don | last post by:
Can someone help me fix my DLookup problem. I'm far from proficiency with Access. I've been creating databases for several years for work with the help of many of you and trial and error. I have used DLookup several times and I've actually re-created the same lookup just in different forms. Here's my problem. I'm using dlookup when I enter a part description it adds the part number for me. I choose the part description from a combo...
1
1215
by: madumm | last post by:
Hi all I really need a solution for the following:: ------------------------------------------------------------------------------------- Say i have a font object called 'myFont' as shown below; Font myFont= domainAxis.getTickLabelFont(); I need to cast "myFont" object into a "Element" object as i shown below
0
7192
by: Tarik Monem | last post by:
I have been working on an all AJAX/DOM web site which is set to go live today and I thought I'd share my discoveries with all of you whom have helped me when I have encountered different issues along the way. First, deep linking is not something that a completely AJAX web site should be able to do by it's very nature of everything being on one page basically. So how can a person deep link to something that is on one page? This question...
0
1806
by: akshaycjoshi | last post by:
I am reading a book which says Even though unboxed value types don't have a type object pointer, you can still call virtual methods (such as Equals, GetHashCode, or ToString) inherited or overridden by the type. The reason is because the CLR can just call these methods nonvirtually and System.ValueType overrides all of these virtual methods and expects the value in the this argument to refer to an unboxed value type instance. Remember, a...
0
9661
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9506
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10193
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7524
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6755
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5414
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5546
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4087
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2904
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.