2

I'm doing some basic validation on form fields. What's the correct way to iterate through an array of objects to validate them? When I try the below I get

The property 'BackColor' cannot be found on this object. Verify that the property exists and can be set.

I guess what I'm missing is a way of telling Powershell these are references to other variables, rather than variables themselves.

$MandatoryFields = @(
    'txtUsername',
    'txtFirst',
    'txtLast',
    'txtEmail',
    'ComboLicense'
)

ForEach ($Field in $MandatoryFields) {
    If ([string]::IsNullOrWhitespace($Field.text)) {
        $Validation = "failed"
        $Field.BackColor = "red"
    }
}

EDIT: Okay, what I needed was the actual variables in the array, like so:

$MandatoryFields = @(
    $txtUsername,
    $txtFirst,
    $txtLast,
    $txtEmail,
    $ComboLicense
)
daninthemix
  • 2,542
  • 8
  • 29
  • 40
  • Not sure,I understand,but,does this help https://stackoverflow.com/questions/37688708/iterate-over-psobject-properties-in-powershell – TheGameiswar Mar 08 '21 at 11:21
  • Don't think so. So in my example, I have objects in the script - txtUsername, txtFirst etc - I've written down the names of those objects in the $MandatoryFields array. And I want to do this with each of those objects in a ForEach loop. I want to do those things with the actual objects, not with the names of those objects that I've written in the $MandatoryFields array. Does that make sense? – daninthemix Mar 08 '21 at 11:26
  • In other words, I need to tell Powershell - don't worry about the text 'txtUsername' in this array - instead go and look at the actual txtUsername object elsewhere in this script and give me a property... – daninthemix Mar 08 '21 at 11:27
  • what are you trying to do with those objects,can you give more context of this ? – TheGameiswar Mar 08 '21 at 11:30
  • Basically I want to read the Text property of a bunch of objects, check that its not empty, and if it is, change the BackColor property of that object to red. I wanted to do it with an array of Object names, rather than having dozens of IF statements. – daninthemix Mar 08 '21 at 11:37
  • Take a look at my post https://stackoverflow.com/questions/66106458/foreach-printing-out-whole-array . Kind of similar, ignore the actual question and see the code. – Abraham Zinala Mar 08 '21 at 14:01

2 Answers2

2

Try adding your objects to an array like below

$objects = [System.Collections.ArrayList]@()

$myObject = [PSCustomObject]@{
    Name     = 'Kevin'
    Language = 'PowerShell'
    State    = 'Texas'
}


$objects.add($myObject)

$myObject1= [PSCustomObject]@{
    Name     = 'Kevin'
    Language = 'PowerShell'
    State    = 'Texas'
}


  $objects.add($myObject1)

foreach($obj in $objects){

$obj.firstname

}
TheGameiswar
  • 27,855
  • 8
  • 56
  • 94
  • Arrays are not expandable so probably better in this case to use something like an ArrayList, or List[object], no? Otherwise you are processing an entire new array every time you call += – Efie Mar 08 '21 at 13:19
0

I'm assuming your using System.Windows.Forms to create your form. If that's the case when adding controls you should assign a name to the control. Then you can loop through your controls and check if the control name matches your list of mandatory controls and then execute your check.

$MandatoryFields = @(
    'txtUsername',
    'txtFirst',
    'txtLast',
    'txtEmail',
    'ComboLicense'
)

$Controls = MyForm.controls

ForEach ($c in $Controls) {
    ForEach ($Field in $MandatoryFields) {
        if ($c.Name -eq $Field) {
            If ([string]::IsNullOrWhitespace($c.text)) {
                $Validation = "failed"
                $c.BackColor = "red"
            }
        }
    }
}
Bee_Riii
  • 814
  • 8
  • 26