Solved

Question on MVC

Posted on 2013-12-17
3
263 Views
Last Modified: 2013-12-17
Hi Experts,

We have MVC4 Application and am going through the code and found following statement in index.cshtml file to get login ID.

var loginID='@((User as Sample.Project.Principal.Administrator).Name);

Open in new window

1)can anyone briefly explain User as in  the above statement

2)How can I write above statement without using "User as "

Thanks in Advance
0
Comment
Question by:ksd123
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 100 total points
ID: 39723788
1) That line casts the User variable/property to a Sample.Project.Principal.Administrator object and then gets the value of its Name property.

2) That depends on the native type of User and if it natively supports a Name property.
0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 50 total points
ID: 39724836
To add to carl_tawn's #1, there is an (possibly valid) assumption here that User *is* or *descends from* the Sample.Project.Principal.Administrator class. If it does not, then the as operator will return null, and the access of the Name property will throw a NullReferenceException. The as operator will successfully cast if the type is compatible, or it will return null. Generally speaking, you should be checking for the null return when using as.

See:  http://msdn.microsoft.com/en-us/library/cscsdfbt.aspx
0
 

Author Closing Comment

by:ksd123
ID: 39725903
Thank you guys.
0

Featured Post

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question