只是玩一些vb.net并且我不明白为什么当我在文本框中输入Dog时标签继续说不是狗?
Public Class Form1
Dim dogAnswer As Boolean
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
End Sub
Public Function getText() As Boolean
dogAnswer = False
If TextBox1.Text = "Dog" Then
Return dogAnswer = True
End If
Return dogAnswer
End Function
Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If dogAnswer = True Then
Label1.Text = "dog"
Else
Label1.Text = "Not dog"
End If
End Sub
End Class
答案 0 :(得分:2)
有三个问题:
1)你永远不会调用GetText
2)即使您确实调用了GetText,它也总是返回false。
3)您正在执行区分大小写的比较,因此dog和DOG的值将返回false。
将您的代码更改为以下内容将获得您所需要的内容:
Public Function getText() As Boolean
If TextBox1.Text.Equals("Dog", StringComparison.InvariantCultureIgnoreCase) Then
dogAnswer = True
Else
dogAnswer = False
End If
Return dogAnswer
End Function
Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If getText() Then
Label1.Text = "dog"
Else
Label1.Text = "Not dog"
End If
End Sub
请注意,getText可以消除,您不需要dogAnswer成员:
Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If TextBox1.Text.Equals("Dog", StringComparison.InvariantCultureIgnoreCase) Then
Label1.Text = "dog"
Else
Label1.Text = "Not dog"
End If
End Sub
答案 1 :(得分:1)
默认布尔值为false!
Dim dogAnswer As Boolean
答案 2 :(得分:0)
首先我要感谢你,我只是在玩游戏,我知道当你只是想要了解一些新东西时,代码会变得多么混乱。
这里有很多错误......
在我看来,你有一个名为dogAnswer的模块(表单)级变量,它只在调用函数getText时设置为True。这是一个具有副作用的函数(返回结果并且还改变状态),有人会说这是不好的做法。其他人会因为这个而说我。
函数永远不会在你的类(表单)中调用,除非调用它,否则dogAnswer的值将保持为False(它的初始值 - 有些人会说你应该显式设置初始值而不是依赖于默认值)< / p>
因此,即使TextBox1.Text确实包含“Dog”,因为函数getText永远不会被调用,dogAnswer将始终为False,而Label1.Text注定要保持为“Not dog”。
但即使你确实调用它,该函数也永远不会起作用,因为你试图用以下行设置dogAnswer的值......
Return dogAnswer = True
...这会将dogAnswer(初始化为False,从未更改)与True进行比较。 如果dogAnswer为True,那么(dogAnswer = True)将返回True,但是在执行此行之前,dogAnswer必须为False,因为代码中没有其他行设置它的值并且它初始化为False。所以dogAnswer总是会错的
我认为你需要的是:
dogAnswer = True
...但是你仍然需要在dogAnswer改变之前在某处调用getText
最后,我注意到您将TextBox1.Text的值与“Dog”进行比较,但将Label1.Text设置为“dog”。您可能需要不区分大小写的比较,否则,如果在TextBox1中输入“dog”,则相等测试将失败。
我还会说一个名为getText的函数返回一个布尔值有点模糊
以下是您的代码的固定版本
Public Class Form1
Dim dogAnswer As Boolean
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
End Sub
Public Function getText() As Boolean
dogAnswer = False
If TextBox1.Text.Equals("Dog", StringComparison.CurrentCultureIgnoreCase) Then
dogAnswer = True
End If
Return dogAnswer
End Function
Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim dummy As Boolean
dummy = getText()
If dogAnswer = True Then
Label1.Text = "dog"
Else
Label1.Text = "Not dog"
End If
End Sub
End Class
......但这是一个更好的方式...
Public Class Form1
Dim dogAnswer As Boolean
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
End Sub
Public Sub CheckDogStatus()
dogAnswer = TextBox1.Text.Equals("Dog", StringComparison.CurrentCultureIgnoreCase)
End Sub
Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
CheckDogStatus()
If dogAnswer = True Then
Label1.Text = "dog"
Else
Label1.Text = "Not dog"
End If
End Sub
End Class
祝你好运!