我正在学习VB.NET,所以我想创建一个简单的登录屏幕。现在,我只想要,如果我点击按钮,它会向控制台写入一些内容(我仍然不知道输出的位置)但是一旦我点击“运行”,我就会收到堆栈溢出异常。
有人可以告诉我为什么这段代码不起作用?
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
答案 0 :(得分:2)
你有这个:
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
,依此类推,直到您用完函数调用的堆栈空间。 Set
做同样的事情。
要解决此问题,该属性非常简单,可以使用auto-implement shorthand:
Public Property Name As String
但是如果你想做很多事情,你需要一个不同名称的支持字段:
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
无论您选择哪种方式,您都需要对Password
属性进行相同的更改。