【问题标题】:How do you recall a function to return another value?你如何回忆一个函数来返回另一个值?
【发布时间】:2013-12-01 18:22:10
【问题描述】:

如何在 VB.NET 中调用布尔函数以返回另一个值?

所以第一次轮到它为真,但随后发生了一个应该使它为假的事件,但是该函数不会重复以兑现这一点。

我已尝试按名称调用该函数,我希望它在其中重复,但这不起作用。

根据要求,这是一个小例子:

   Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click
           Dim 1 As String = comboboxSelectGroup.SelectedItem
'What I've tried:
           Call CallByName(Me, AlreadyInListbox(), vbMethod, e)
           If AlreadyInListbox() = True Then
            MsgBox("Its already in there")
           End If
           If AlreadyInListbox() = False Then
               ListBox1.Items.Add(comboboxSelectGroup.SelectedItem)
           End If

       End Sub

    Function AlreadyInListbox() As Boolean
        For Each item In ListBox1.Items
            If item.Contains(comboboxSelectGroup.SelectedItem) Then
                Return True
            Else
                Return False
            End If
        Next
    End Function

所以每次单击按钮时,我希望函数重复 For 循环,以便在适当的情况下产生另一个值。

【问题讨论】:

  • 不清楚你的问题是什么。如果您可以发布尽可能小的示例来说明问题,那将非常有帮助。
  • 我添加了一个例子。

标签: vb.net function boolean


【解决方案1】:

首先,AlreadyInListbox 可以简化:

Function AlreadyInListbox() As Boolean

   Return ListBox1.Items.Contains(comboboxSelectGroup.SelectedItem)

End Function

没有必要遍历每个项目,因为项目集合可以回答问题。鉴于其新的简单性,它不需要存在。

Private Sub btn1_Click(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles btn1.Click

       ' WHAT????  this cant compile. so this isnt real code
       'Dim 1 As String = comboboxSelectGroup.SelectedItem

       ' tell us why this wont work.  is there an error?
       If AlreadyInListbox()  Then
            MsgBox("Its already in there")
       ELse
           ListBox1.Items.Add(comboboxSelectGroup.SelectedItem)
       End If

End Sub

但是,鉴于新的简单性,这将起作用:

If ListBox1.Items.Contains(comboboxSelectGroup.SelectedItem) Then
     MsgBox("Its already in there")
Else
     ListBox1.Items.Add(comboboxSelectGroup.SelectedItem)
End If

我不知道你所说的“重复”一个函数是什么意思,只要它们在scope 中,它们就可以无限重复使用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-09-15
    • 2021-12-11
    • 1970-01-01
    • 2011-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多