# Array ForEach Type Conversion with Custom Object

**URL:** <https://forums.powershell.org/t/array-foreach-type-conversion-with-custom-object/20341>\
**Category:** PowerShell Help\
**Created:** [September 5, 2022, 2:47am UTC](https://forums.powershell.org/t/array-foreach-type-conversion-with-custom-object/20341 "2022-09-05T02:47:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mscreations](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/mscreations/32/3913_2.png) [@mscreations](https://forums.powershell.org/u/mscreations)\
**Post date:** [September 5, 2022, 2:47am UTC](https://forums.powershell.org/t/array-foreach-type-conversion-with-custom-object/20341/1 "2022-09-05T02:47:20Z")

</div>

Okay, I’m having a little trouble with some type conversion where I am not sure what function I need to have defined for the code to work right.

I am utilizing ConvertFrom-Json to load the object from a file. The line that I’m having trouble with is the `$obj.JobList.ForEach([BackupJob])` line. I get an error that "Cannot find an overload for “Add” and the argument count “1”. I know I need to overload a function somewhere, but I’m not sure what.

I know I can get it to work by using a traditional foreach loop, but I’m wondering if there is an easy way to make it work with this line (mostly because I’m interested in how the mechanics behind the array.ForEach is working)

Below is a shortened version of my class definitions. A previous post of mine had a more full version if you want to see that.

```auto
class Backup
{
    [BackupJob[]]$Joblist

    Backup([PSCustomObject]$obj) {
        $this.Joblist = $obj.JobList.ForEach([BackupJob])
    }

}

class BackupJob
{
    [string]$MountPoint

    BackupJob([PSCustomObject]$obj) {
        $this.MountPoint = $obj.MountPoint
    }
}

```

---

<div class="post-metadata">

**Author:** ![kvprasoon](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kvprasoon/32/4449_2.png) [@kvprasoon](https://forums.powershell.org/u/kvprasoon)\
**Post date:** [September 6, 2022, 8:33am UTC](https://forums.powershell.org/t/array-foreach-type-conversion-with-custom-object/20341/2 "2022-09-06T08:33:40Z")

</div>

Foreach on an object will be used like below.

```auto
$Obj.Foreach({$_})
# contents inside {} will be same as how we use it in Foreach-Object

```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:16pm UTC](https://forums.powershell.org/t/array-foreach-type-conversion-with-custom-object/20341/3 "2024-05-16T20:16:39Z")

</div>

Closed on May 16, 2024, 8:16pm UTC


