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 November 10th, 2010, 02:09 PM
Authorized User
 
Join Date: Nov 2010
Posts: 13
Thanks: 4
Thanked 0 Times in 0 Posts
Default Explicit casting problem

Hi,
I have a problem to cast the object type variable to int type in following code:

object a = '1';
int i;
i = (int)a;
Console.WriteLine(i);
======================
O/P:

Unhandled Exception: System.InvalidCastException: Specified cast is not valid.

it give the error as above, what am I missing?

Please help me.
Thanks.
 
Old November 10th, 2010, 02:56 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

You can only 'cast' if the object is the type you are casting to or implements an implicit convertion operator (e.g. An int to a double.)

For what you want you'll want to use Convert.ToInt32().
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old November 12th, 2010, 04:49 PM
Authorized User
 
Join Date: Nov 2010
Posts: 13
Thanks: 4
Thanked 0 Times in 0 Posts
Post

Thanks for reply,
I have read in the book that object is the root of all classes and any variable which has type of object can be cast to any type.
But (The code which I have posted) I have tried to check that it can be cast or not, and it did not cast to char. since I am confused in that if object it root then it should be cast to any type.

And I have used another code which works fine:
Code:
static void Main(string[] args)
{
	object a = '1';
	------ -
	int i;
	i = (char)a;
	    -------
//The type of 'a' was object then it is casted to char and then assign to int type to make 'a' as int type.
	int ii = 0;
	char aa = 'a';
	ii = (int)aa;
	Console.WriteLine(i);
	Console.WriteLine(ii);
}
Thanks..

Last edited by smfarooq; November 12th, 2010 at 04:54 PM..
 
Old November 14th, 2010, 07:54 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Quote:
I have read in the book that object is the root of all classes and any variable which has type of object can be cast to any type.


I hink you misunderstood that somehow. The first part is true, but the second isn't. So, all objects inherit Object, and can be cast to an object. However, you cannot just cast any type to any other type. Consider these hierarchies:

Code:
 
Vehicle
  Car
  Truck
 
Animal
  Goat
  Cow
Now, a Cow is an animal, so something like this should work:

Code:
 
Cow clarabelle = new Cow(); 
Animal animal = (Animal) cow;
This casts the Cow type to a more generic Animal type.

But would you expect this to work:

Code:
 
Cow clarabelle = new Cow(); 
Truck truck = (Truck) cow;
In other words, as Sam said: "You can only 'cast' if the object is the type you are casting to or implements an implicit convertion operator (e.g. An int to a double.)" You can cast a
Cow to an Animal or to an Object (because ultimately a Cow IS an Animal and it IS an Object), but not to "any type" or arbitrary other types such as Trucks, Vehicles or ints.

Your code works because there are conversions between ints and chars available that, if I am not mistaken, call Convert.ToInt32 under the hood.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
The Following User Says Thank You to Imar For This Useful Post:
smfarooq (November 15th, 2010)
 
Old November 15th, 2010, 03:39 PM
Authorized User
 
Join Date: Nov 2010
Posts: 13
Thanks: 4
Thanked 0 Times in 0 Posts
Smile Thanks

Thank you, Now I have understood, and your given solution in this problem is very helpful for me.
 
Old December 23rd, 2010, 07:29 AM
Authorized User
 
Join Date: Nov 2010
Posts: 13
Thanks: 4
Thanked 0 Times in 0 Posts
Post Help me

Hi,
what is the difference between controls and components in c #?

Plz help me,

Thankx
 
Old December 23rd, 2010, 07:50 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

New questions under a new thread please. That helps people searching for answers among other things.
__________________
Joe
http://joe.fawcett.name/





Similar Threads
Thread Thread Starter Forum Replies Last Post
Casting Problem liorlankri ASP.NET 1.x and 2.0 Application Design 2 January 2nd, 2007 03:31 AM
FOR XML Explicit and ASP tonyellard SQL Server ASP 1 April 8th, 2005 10:41 AM
For XML Explicit bmains SQL Server 2000 5 March 8th, 2004 09:18 AM
Option Explicit and Item lcsgeek Classic ASP Basics 1 October 6th, 2003 01:54 PM





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