4

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
)
6
  • Not sure,I understand,but,does this help stackoverflow.com/questions/37688708/… Commented Mar 8, 2021 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? Commented Mar 8, 2021 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... Commented Mar 8, 2021 at 11:27
  • what are you trying to do with those objects,can you give more context of this ? Commented Mar 8, 2021 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. Commented Mar 8, 2021 at 11:37

2 Answers 2

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

}
Sign up to request clarification or add additional context in comments.

1 Comment

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 +=
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"
            }
        }
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.