views:

67

answers:

2

Is it possible to add user-defined properties to the My.Application or My.User objects?

I've got some properties defined currently that tell me what environment the app is running in (i.e. "Development", "Testing", "Production", etc.), whether the user is an administrator, etc. It would seem logical to get this info from something like

Dim current = My.Application.EnviornmentName

or

If My.User.IsAdministrator Then ...

instead of having them defined in a separate class as they are now. Is there any way to add my own properties to these objects?

+2  A: 

I am not sure about VB.NET but can VB.NET use extensions? if so, use that to add on extra functionality to the Environment.

Hope this helps, Best regards, Tom

tommieb75
yes it can see http://msdn.microsoft.com/en-us/library/bb384936.aspx
ScottS
Exactly what I was looking for. I'm using extension methods elsewhere in my project, and it didn't even dawn on me to apply them in this instance. Thanks for the nudge in the right direction.
gfrizzle
You're welcome! Good luck in your coding, gfrizzle. :)
tommieb75
A: 

I'm pretty sure you can do something like this, you'll need to repeat the namespace, and I don't know what kind of trouble you'll get into.

Namespace My
Public Class User
    Inherits Microsoft.VisualBasic.ApplicationServices.User
    Public Shared Function IsAdministrator() As Boolean
        Dim myUser As New Microsoft.VisualBasic.ApplicationServices.User
        If myUser.IsAuthenticated Then
            If myUser.IsInRole("Administrators") Then
                Return True
            End If
            Return False
        End If
    End Function
End Class
End Namespace
zimmer62
How would you get the application to use your new class instead of the standard user class?
ScottS
I'll have to double check, but when I tried this it looks like both were available. By inheriting the standard ApplicationServices.User, it will get all the properties of orig class.This is basically extending this class
zimmer62