gpt4 book ai didi

arrays - 从函数返回数组列表

转载 作者:行者123 更新时间:2023-12-02 23:29:04 24 4
gpt4 key购买 nike

因此,我有一个poweshell表单,您可以在其中将用户添加到列表框,同时添加到arraylist。当我尝试返回所有用户的arraylist时,它什么也不返回。

这是我的代码

#---------------------#
# Function AddUserGUI #
#---------------------#
Function AddUserGUI()
{
$AllUsers = New-Object System.Collections.ArrayList

#---Form
$Form = New-Object System.Windows.Forms.Form
$Form.FormBorderStyle = "FixedSingle"
$Form.Size = New-Object System.Drawing.Size(295,280)
$Form.MaximizeBox = $False
$Form.Text = "Add User(s)"
$Form.StartPosition = "CenterScreen"
$Form.Font = New-Object System.Drawing.Font("Segoe UI",9,0,3,1)

#---TexBox
$objTextBox = New-Object System.Windows.Forms.TextBox
$objTextBox.Location = New-Object System.Drawing.Size(10,10)
$objTextBox.Size = New-Object System.Drawing.Size(183,100)
$objTextBox.MaxLength = 100

#---ListBox
$objListBox = New-Object System.Windows.Forms.ListBox
$objListBox.Location = New-Object System.Drawing.Size(10,45)
$objListBox.Size = New-Object System.Drawing.Size(183,199)
$objListBox.HorizontalScrollbar = $True

#---Buttons
$Add = New-Object System.Windows.Forms.Button
$Add.Location = New-Object System.Drawing.Size(205,10)
$Add.Size = New-Object System.Drawing.Size(75,23)
$Add.FlatStyle = "PopUp"
$Add.Text = "Add"

$Remove = New-Object System.Windows.Forms.Button
$Remove.Location = New-Object System.Drawing.Size(205,45)
$Remove.Size = New-Object System.Drawing.Size(75,23)
$Remove.FlatStyle = "PopUp"
$Remove.Text = "Remove"

$Confirm = New-Object System.Windows.Forms.Button
$Confirm.Location = New-Object System.Drawing.Size(205,220)
$Confirm.Size = New-Object System.Drawing.Size(75,23)
$Confirm.FlatStyle = "PopUp"
$Confirm.Text = "Confirm"

#---Buttons On-Click Actions
$Add_OnClick=
{
if($objTextBox.Text -ne "")
{
$UserChk = $true #[Bool](Get-ADObject -Filter { sAMAccountName -eq $objTextBox.Text } -Searchbase "ou=User_PFS,dc=pfs,dc=local")

if($UserChk -eq $True)
{
if($objListBox.Items -contains $objTextBox.Text) {DisplayMsgBox "User already in list." "Duplicate entry" "Information" 0}

ElseIf($objTextBox.Text -ne $NULL -and $objListBox.Items -notcontains $objTextBox.Text)
{
$objListBox.Items.Add($objTextBox.Text)
[void]$AllUsers.Add($objTextBox.Text)
}
}

ElseIf($UserChk -eq $False) {DisplayMsgBox "User not found." "Invalid User Name" "Information" 0}

$objTextBox.Text = ""
}
}

$Remove_OnClick=
{
$AllUsers.Remove($objListBox.SelectedItem)
$objListBox.Items.Remove($objListBox.SelectedItem)
}

$Confirm_OnClick=
{
if($AllUsers.Count -eq 0)
{
DisplayMsgBox "Please add at least one user to the list." "No Users Found" "Information" 0
}

ElseIf($AllUsers.Count -gt 0)
{
$Form.Close()

Return $AllUsers
}
}

#---Adding Elements to the form
$Add.add_Click($Add_OnClick)
$Remove.add_Click($Remove_OnClick)
$Confirm.add_Click($Confirm_OnClick)

$Form.Controls.Add($objListBox)
$Form.Controls.Add($objTextBox)
$Form.Controls.Add($Add)
$Form.Controls.Add($Remove)
$Form.Controls.Add($Confirm)

#---Display Form
$Form.ShowDialog()| Out-Null
}

$AllUsers = AddUserGUI

foreach($User in $AllUsers){write-host $User}

foreach循环不会产生任何输出。如何正确返回此数组列表?

最佳答案

在返回之前,请使用前导,以防止管道将其展开为元素流:

function return-arraylist
{
$arraylist = new-object collections.arraylist
[void]$arraylist.Add('Item one')
[void]$arraylist.Add('Item two')
return ,$arraylist
}

(return-arraylist).gettype()



IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True ArrayList System.Object

关于arrays - 从函数返回数组列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20686849/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com