Vb.net 登录屏幕:堆栈溢出异常
我正在学习VB.NET,所以我想做一个简单的登录屏幕。现在,我只希望,如果我点击按钮,它会向控制台写入一些内容(我仍然不知道输出的去向),但我一点击运行就会收到堆栈溢出异常 有人能告诉我为什么这个代码不起作用吗Vb.net 登录屏幕:堆栈溢出异常,vb.net,stack-overflow,Vb.net,Stack Overflow,我正在学习VB.NET,所以我想做一个简单的登录屏幕。现在,我只希望,如果我点击按钮,它会向控制台写入一些内容(我仍然不知道输出的去向),但我一点击运行就会收到堆栈溢出异常 有人能告诉我为什么这个代码不起作用吗 Public Class Form1 Private Class Users Public Property Name() As String Get ' Gets the property value. Re
Public Class Form1
Private Class Users
Public Property Name() As String
Get
' Gets the property value.
Return Name
End Get
Set(ByVal Value As String)
' Sets the property value.
Name = Value
End Set
End Property
Public Property Password() As String
Get
' Gets the property value.
Return Password
End Get
Set(ByVal Value As String)
' Sets the property value.
Password = Value
End Set
End Property
Public Sub New(ByVal name As String, ByVal password As String)
Me.Name = name
Me.Password = password
End Sub
End Class
Private user As New Users("Matias", "Barrios")
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
End Sub
Private Sub Label2_Click(sender As Object, e As EventArgs) Handles Label2.Click
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Public Sub Validar(nombre As String, password As String)
Me.TextBox1.Text = user.Name
If nombre = user.Name And password = user.Password Then
System.Console.Write(user.Name)
Me.TextBox1.Text = "No"
End If
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Validar("Matias", "Barrios")
System.Console.Write("Click!")
End Sub
End Class
你有这个:
Public Property Name() As String
Get
' Gets the property value.
Return Name
End Get
Set(ByVal Value As String)
' Sets the property value.
Name = Value
End Set
End Property
该属性的Get
引用自身。所以Get
调用Get
,它再次调用Get
,以此类推,直到函数调用的堆栈空间用完为止<代码>设置做同样的事情
要解决此问题,该属性非常简单,可以使用:
但是,如果您想长期这样做,则需要一个具有不同名称的支持字段:
Private _Name As String
Public Property Name() As String
Get
' Gets the property value.
Return _Name
End Get
Set(ByVal Value As String)
' Sets the property value.
_Name = Value
End Set
End Property
无论您选择哪一种,您都需要对密码属性进行相同的更改。您有以下功能:
Public Property Name() As String
Get
' Gets the property value.
Return Name
End Get
Set(ByVal Value As String)
' Sets the property value.
Name = Value
End Set
End Property
该属性的Get
引用自身。所以Get
调用Get
,它再次调用Get
,以此类推,直到函数调用的堆栈空间用完为止<代码>设置
做同样的事情
要解决此问题,该属性非常简单,可以使用:
但是,如果您想长期这样做,则需要一个具有不同名称的支持字段:
Private _Name As String
Public Property Name() As String
Get
' Gets the property value.
Return _Name
End Get
Set(ByVal Value As String)
' Sets the property value.
_Name = Value
End Set
End Property
无论您选择哪种,都需要对
密码
属性进行相同的更改。如果您不打算使用自动实现的属性,则需要使用备份字段(专用变量)来存储属性值<代码>返回名称正在无限期调用prop getterloop@Plutonix抱歉,我更新了代码,粘贴了错误的代码。无论如何,我认为这不会影响错误代码,但我没有得到你的回应。我为什么不使用它们?请仔细阅读评论。问题出在属性getter中,而不是您更改的位。Google upAuto-Implemented Properties
,了解如何摆脱样板代码。我不知道你为什么不使用“它们”不管“它们”指的是什么-支持字段还是自动属性。如果你不打算使用自动实现的属性,你需要使用支持字段(私有变量)来存储属性值<代码>返回名称正在无限期调用prop getterloop@Plutonix抱歉,我更新了代码,粘贴了错误的代码。无论如何,我认为这不会影响错误代码,但我没有得到你的回应。我为什么不使用它们?请仔细阅读评论。问题出在属性getter中,而不是您更改的位。Google upAuto-Implemented Properties
,了解如何摆脱样板代码。我不知道你为什么不使用“他们”,不管“他们”指的是什么——支持字段还是自动属性。