BladeBrown
BladeBrown

Reputation: 31

Why is my class nothing?

I have a .NET 2010 project. In it, I create a basic user class. Instantiate it and successfully fill it with data. The entire time it is doing this, if I hover over the class, it says it is nothing, even as it fills its properties. Later on, it hoses me in the UI, even though the property has a value it says the class is nothing.

I DO instatiate the class...

Dim oExtendedUser As New ExtendedUser

and here is the classs definition...

Public Class ExtendedUser
    Inherits System.Web.Security.MembershipUser

    Public Sub New()
        _Role = New Role

    End Sub

    Public Property ExtendedUserID As Int32
    Public Property FirstName As String
    Public Property LastName As String
    Public Property Phone As String
    Public Property UserID As Guid
    Public Property Role As Role
    Public Property UserName() As String
    Public Property Password() As String
    Public Property SecurityQuestion() As String
    Public Property SecurityAnswer() As String
End Class

I changed the class. I added MyBase.New() but the problem persists. On the UI, here is the code that executes when the button is clicked. Director has, as a proprety, ExtendedUSer

Dim oCase As New BE.Case
 Dim oDirector As New BE.Director
 oDirector = SessionManager.Director 'the values are here
 oCase.Investigator.ExtendedUserID = oDirector.ExtendedUser.ExtendedUserID

And here is the Director...

Public Class Director

    Public Sub New()
        _ExtendedUser = New ExtendedUser

    End Sub

    Public Property ID As Int32
    Public Property ExtendedUser As ExtendedUser

End Class

Upvotes: 3

Views: 548

Answers (1)

Matthieu
Matthieu

Reputation: 4620

You got tricked by the ToString Override. Your object exists but it overrides the ToString Method : MembershipUser.ToString

To validate this behavior, try it with a simple class :

VB.NET

Public Class Test
   Public Property TestString As String 

   Public Overrides Function ToString() As String
      Return Me.TestString
   End Function

End Class

C#

public class Test
{
    public string TestString { get; set; }

    public override string ToString()
    {
        return this.TestString;
    }
}

With this code, the Watch will show you an instanciated Test to Nothing, because ToString value will be Nothing. The object exist, but Visual Studio is using the ToString Method to populate the value field, and at this point it is Nothing.

VB.NET

Public Class Test
   Public Property TestString As String = ""

   Public Overrides Function ToString() As String
      Return Me.TestString
   End Function

End Class

C#

public class Test
{
      public Test()
      {
          this.TestString = "";
      }

    public string TestString { get; set; }

    public override string ToString()
    {
        return this.TestString;
    }

}

With this code you'll get an empty string.

To get back to your code, you cannot extend the MembershipUser so simply, you have to follow this guideline : How to: Implement a Custom Membership User. As many things won't work with your actual extension (For example, your username shadowing the base one).

See this question as well. There are easier ways to extend your user "entity" than inheritance.

Upvotes: 4

Related Questions