Skip to Main Content
Professional Visual Studio® 2008
book

Professional Visual Studio® 2008

by Nick Randolph, David Gardner
July 2008
Intermediate to advanced content levelIntermediate to advanced
1026 pages
27h 59m
English
Wrox
Content preview from Professional Visual Studio® 2008

14.2. Using My in Code

Using the My objects in your application code is straightforward in most Windows and web-based projects. Because the underlying real namespace is implicitly referenced and any necessary objects are created for you automatically, all you need to do is reference the object property or method you wish to use. As an example, consider the following code snippet that evaluates the user identity and role attached to the thread running an application:

Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
   Handles OK.Click
    If My.User.IsAuthenticated Then
        If My.User.IsInRole("Administrators") Then
            My.Application.Log.WriteEntry("User " & My.User.Name & _
                " logged in as Administrator", TraceEventType.Information)
        Else
            My.Application.Log.WriteEntry("User " & My.User.Name & _
                " does not have correct privileges.", TraceEventType.Error)
        End If
    End If
    ...
End Sub

The code is fairly straightforward, with the various My object properties and methods defined with readable terms such as IsAuthenticated and WriteEntry. However, before the introduction of My to the developer's toolbox, it was no trivial task to write the code to attach to the current principal, extract the authentication state, determine what roles it belongs to, and then write to an application log.

Every My object provides vital shortcuts to solve scenarios commonly faced by both Windows and web developers, as this example shows. Microsoft did a great job in creating this namespace ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Professional Visual Studio® 2010

Professional Visual Studio® 2010

Nick Randolph, David Gardner, Michael Minutillo, Chris Anderson

Publisher Resources

ISBN: 9780470229880Purchase book