Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 19th, 2004, 10:46 AM
Registered User
 
Join Date: Mar 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Dynamic Casting?

Is there a way to do dynamic casting in C#. For instance what I would like to do is use the getType() method or some alternative and use it in cast.

Basic Sample of what I am thinking. Note this does not work.

String myString, myString2;
int myInt;

myString2 = ( (myString2.GetType() ) (myInt);

Thanks...

 
Old April 19th, 2004, 10:50 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

I understand what you are driving at, but with the code you have, it doesn't make sense. If myString2 is a String in the code, why would you need to cast it dynamically?

Maybe a better approach is to use the generic Object type and handle them with explicit casts based on the Objects types. I assume that at some point you will have to know what type they are and you will know how you are going to handle each different type. If not, then perhaps a more comprehensive explanation of the final goal would be helpful to solving the problem.

Peter
-------------------------
Work smarter, not harder
 
Old July 1st, 2004, 04:01 PM
Authorized User
 
Join Date: Jul 2004
Posts: 69
Thanks: 0
Thanked 1 Time in 1 Post
Default

if i understand correctly:

if( myString2.GetType() == typeof(string) )
   myInt = int.Parse(myString2);

www.CoderForRent.com
Get A Computer Job!
 
Old February 11th, 2008, 06:11 PM
Registered User
 
Join Date: Feb 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hey gman997, I know this is 1393 days later now, but.. I've got an answer for you, as well as anyone else that happens to Google as I did, looking for more dynamic ways of interfacing using C#.

the Convert object has a method which will allow you to pass the type you want to convert any value TO! Yippie!

check out Convert.ChangeType()

It works wonderfully by the way...

Cheers man... hope it wasn't too late... hehe

Perry








Similar Threads
Thread Thread Starter Forum Replies Last Post
Casting pfrigo BOOK: Professional .NET 2.0 Generics 3 November 19th, 2007 04:49 PM
Casting - Can you help koco C# 1 September 24th, 2007 08:41 AM
IEnumerator & Dynamic (run-time) Casting. braindead C# 1 May 11th, 2006 05:17 PM
casting ravibodani Access 1 April 4th, 2006 01:19 PM
Dynamic Casting pareshsoft C# 2 December 1st, 2004 12:12 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.