Wrox Programmer Forums
|
BOOK: Beginning ASP.NET 4 : in C# and VB
This is the forum to discuss the Wrox book Beginning ASP.NET 4: in C# and VB by Imar Spaanjaars; ISBN: 9780470502211
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 4 : in C# and VB 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 May 24th, 2012, 03:42 AM
ich ich is offline
Registered User
 
Join Date: May 2012
Posts: 1
Thanks: 2
Thanked 0 Times in 0 Posts
Question Chapter 14 Exercise 4

Can anyone tell me the difference between these two lines of code:

Code:
Picture myPicture = e.Entity as Picture;
and

Code:
Picture myPicture = (Picture)e.Entity;
The first is the answer code give for this exercise, the second is the code given earlier in the chapter for the EntityDataSOurce_Inserting event.

Thanks.
 
Old May 24th, 2012, 08:19 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

Both do more or less the same thing: they cast e.Entity to a Picture type.

The difference is that the "as" operator does not throw an exception but simply returns null in ase e.Entity is null or not a Picture. Casting using (Picture) does crash when the value you try to cast is null or not a Picture.

Both code example can be interchanged in these examples as e.Entity should contain a valid Picture.

For more details see page 146.

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:
ich (May 24th, 2012)





Similar Threads
Thread Thread Starter Forum Replies Last Post
ch. 14 exercise 3 question on hyperlink syntax Notso BOOK: Beginning ASP.NET 4 : in C# and VB 1 March 26th, 2012 02:49 PM
Chapter 14 Exercise 3 Solution pg 747 arodrigu12 BOOK: Beginning ASP.NET 4 : in C# and VB 2 April 21st, 2011 07:58 PM
Chapter 14 Exercise 3 and solution. UncleJeff BOOK: Beginning ASP.NET 4 : in C# and VB 2 March 14th, 2011 02:15 PM
Chap. 14 Exercise 2 C#Book stoverje BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8 0 July 1st, 2008 09:41 AM
C#: Chapter 14 - Exercise 3 Tchami BOOK: Beginning ASP.NET 1.0 0 November 26th, 2003 02:48 PM





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