Wrox Programmer Forums
|
BOOK: Beginning Visual C# 2012
This is the forum to discuss the Wrox book Beginning Visual C# 2012 by Karli Watson, Jacob Vibe Hammer, Jon Reid, Morgan Skinner, Daniel Kemper, Christian Nagel, ; ISBN: 978-1-118-31441-8
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Visual C# 2012 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 June 2nd, 2014, 10:59 PM
Authorized User
 
Join Date: Nov 2012
Posts: 25
Thanks: 3
Thanked 0 Times in 0 Posts
Question Unknown syntax

I'm reading Chapter 11 and there was syntax introduced by the book that I am unfamiliar with. there is an extra set of parathesis used in the syntax in question. I have a problem with the following line:
"((Animal)myEntry.Value).Name"
you don't have to explain everything in the line. Just explain to me why the extra parathesis was used.
Here is the line used in the code:
Code:
foreach (DictionaryEntry myEntry in animalCollection)
{
     Console.WriteLine("New {0} object added to custom collection, " +
                                "Name = {1}", myEntry.Value.ToString(),
                                 ((Animal)myEntry.Value).Name); //This is the line in 
                                                                                     question
}
Thanks in advanced,
Truck35
 
Old June 3rd, 2014, 01:01 AM
Registered User
 
Join Date: Jun 2014
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Parenthesis problem solved

If you see the statement carefully, you will find that the parenthesis used in "((Animal)myEntry.Value).Name);"
is opened in the beginning of the function Console.WriteLine();
Console.WriteLine ("New {0} object added to custom collection, " + // parenthesis opens here
"Name = {1}", myEntry.Value.ToString(),
((Animal)myEntry.Value).Name); //<= it closes here

I hope the problem is solved; :-)
 
Old June 3rd, 2014, 11:09 AM
Authorized User
 
Join Date: Nov 2012
Posts: 25
Thanks: 3
Thanked 0 Times in 0 Posts
Question Re: Parenthesis problem solved

I appreciate you response but you misunderstood my question. I know that all arguments of a method must be enclosed in parenthesis. But if you look at my question I am asking why the last argument has parenthesis around it. I know that the myEntry object must be casted to the animal typed so, that explains the parenthesis around the word Animal. But, why is there parenthesis around the cast to the word value. Please explain if you can. I haven't seen this syntax before.
here and here
|| ||
V V
((Animal)myEntry.Value).Name);
 
Old June 5th, 2014, 10:57 PM
Friend of Wrox
 
Join Date: Feb 2014
Posts: 136
Thanks: 1
Thanked 10 Times in 10 Posts
Default

lets unwrap the code and color code the parens here to get a better view

Code:

Console.WriteLine( "New {0} object added to custom collection, Name = {1}", myEntry.Value.ToString(), ((Animal)myEntry.Value).Name );
the syxtax you are seeing

Code:
((Animal)myEntry.Value).Name
is the value of myEntry.Value being type casted to an animal type then from that animal type the Name property is being de-referenced.

the last set of parens is forcing the evaluation of the type cast first then the de-reference operation of the Name property on the animal type is performed.

Then the last thing to happen is passing the value of the animal Name to the console.WriteLine method

Last edited by mmorgan30; June 5th, 2014 at 11:09 PM..





Similar Threads
Thread Thread Starter Forum Replies Last Post
Unknown Server Tag sharon5469 ASP.NET 1.0 and 1.1 Basics 3 June 18th, 2008 03:03 PM
Unknown Error!!!! arun_sunmic Pro Java 2 July 25th, 2006 11:24 PM
Unknown Gaz LG Danha Pro VB Databases 0 October 27th, 2004 02:21 AM
Unknown reason arnab2410 Pro VB Databases 0 February 9th, 2004 05:09 AM
Unknown function name khun Access VBA 4 October 23rd, 2003 05:23 AM





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