Solved

Question on MVC

Posted on 2013-12-17
3
256 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
3 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 100 total points
Comment Utility
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 74

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 50 total points
Comment Utility
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
Comment Utility
Thank you guys.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now