问题描述
我有一个.NET 2010项目。在这里面,我创建了一个基本的用户类。实例并成功地用数据填充它。它是这样做的,如果我悬停在类的整个时间,它说,这不算什么,即使它填补其属性。后来,它软管我的UI,尽管酒店有一个值,它说的类是什么。
I DO instatiate类...
昏暗oExtendedUser作为新ExtendedUser
和这里是班组长定义...
公共类ExtendedUser
继承System.Web.Security.MembershipUser 的Public Sub New()
_Role =新角色 结束小组 公共属性ExtendedUserID作为的Int32
公共属性名字作为字符串
公共属性名字作为字符串
公共属性电话作为字符串
公共财产的userid的Guid
公共属性角色作为角色
公共属性用户名()作为字符串
公共财产密码()作为字符串
公共财产SecurityQuestion()作为字符串
公共财产SecurityAnswer()作为字符串
末级
我改变了类。我加MyBase.New(),但问题仍然存在。在用户界面上,这里是code即单击按钮时执行。署长已作为一个亲preTY,ExtendedUSer
昏暗oCase作为新BE.Case
昏暗oDirector作为新BE.Director
oDirector = SessionManager.Director'的值是这里
oCase.Investigator.ExtendedUserID = oDirector.ExtendedUser.ExtendedUserID
这里是主任...
公共班主任 的Public Sub New()
_ExtendedUser =新ExtendedUser 结束小组 公共属性ID作为的Int32
公共属性ExtendedUser作为ExtendedUser末级
您得到了由覆盖的ToString欺骗。
你的对象存在,但覆盖ToString方法:
要验证这一行为,用一个简单的类试试吧:
VB.NET
公共类测试
公共属性TestString视为字符串 公共覆盖功能的ToString()作为字符串
返回Me.TestString
结束功能末级
C#
公共类测试
{
公共字符串的TestString {搞定;组; } 公共重写字符串的ToString()
{
返回this.TestString;
}
}
有了这个code,手表将告诉你一个实例化测试不了了之,因为的ToString值将一无所获。对象存在,但Visual Studio是使用ToString方法来填充值字段,而在这一点上是没有。
VB.NET
公共类测试
公共财产的TestString的String = 公共覆盖功能的ToString()作为字符串
返回Me.TestString
结束功能末级
C#
公共类测试
{
公开测试()
{
this.TestString =;
} 公共字符串的TestString {搞定;组; } 公共重写字符串的ToString()
{
返回this.TestString;
}}
有了这个code,你会得到一个空字符串。
要回到你的code,你不能扩展的MembershipUser如此简单,你必须遵循这一准则:的。由于很多事情不会与您的实际推广工作(例如,您的用户名阴影的基地之一)。
请参阅为好。还有更简单的方法来扩展你的用户实体不是继承。
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
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.
这篇关于为什么我的课什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!