Class method can't find overloads for method

Greet!
I recently trying to learn the class from Microsoft

when i learning the chapter

class Book {
    # Class properties
    [string]   $Title
    [string]   $Author
    [string]   $Synopsis
    [string]   $Publisher
    [datetime] $PublishDate
    [int]      $PageCount
    [string[]] $Tags
    # Default constructor
    Book() { $this.Init(@{}) }
    # Convenience constructor from hashtable
    Book([hashtable]$Properties) { $this.Init($Properties) }
    # Common constructor for title and author
    Book([string]$Title, [string]$Author) {
        $this.Init(@{Title = $Title; Author = $Author })
    }
    # Shared initializer method
    [void] Init([hashtable]$Properties) {
        foreach ($Property in $Properties.Keys) {
            $this.$Property = $Properties.$Property
        }
    }
    # Method to calculate reading time as 2 minutes per page
    [timespan] GetReadingTime() {
        if ($this.PageCount -le 0) {
            throw 'Unable to determine reading time from page count.'
        }
        $Minutes = $this.PageCount * 2
        return [timespan]::new(0, $Minutes, 0)
    }
    # Method to calculate how long ago a book was published
    [timespan] GetPublishedAge() {
        if (
            $null -eq $this.PublishDate -or
            $this.PublishDate -eq [datetime]::MinValue
        ) { throw 'PublishDate not defined' }

        return (Get-Date) - $this.PublishDate
    }
    # Method to return a string representation of the book
    [string] ToString() {
        return "$($this.Title) by $($this.Author) ($($this.PublishDate.Year))"
    }
}

i try to write my own code over the example

class map {
    [int]$x1
    [int]$y1
    [int]$z1
    [string]$name
    
        # Default constructor
        map() { $this.Init(@{}) }
        # Convenience constructor from hashtable
        map(
            
        
        
        
        
        [hashtable]$Properties) { $this.Init($Properties) }
        # Common constructor for title and author
        map([int]$X1, [int]$Z1) {
            $this.Init(@{ })
        }
        # Shared initializer method
        [void] Init([hashtable]$Properties) {
            foreach ($Property in $Properties.Keys) {
                $this.$Property = $Properties.$Property
            }
        }
    }
    $map=  [System.Collections.Generic.List[map]]$map

class newplace {
[int]$x1
[int]$y1
[int]$z1
[string]$name
newplace (){
$this.x1=123
$this.y1=123
$this.z1=123
$this.name = 'test'}

}

$newplace =[newplace]::new()
$map.add($newplace)

but i get the error of

MethodException: Cannot find an overload for "add" and the argument count: "1".

i know there is sth wrong with the overloads in my code ,but i didn’t really understand why

Thanks for sharing your code. I would take a closer look at the examples. What you are trying to do seems more closely resembling the ‘BookList’ example but your code doesn’t really look like the example.

BookList = Map
Book = NewPlace

However your code is doing something a little different. You seem to be creating a generlic list of things instead, outside of using a method/class to do that. The way you have it in your code I think it is wanting to create a list of MAP objects, not a list of newplace objects. It seems like your intent is to create a list of newplace objects. With your code, the way it’s going to ‘work’ is that you really aren’t going to use the map, your example is using a ‘generic list’ to store an array or list of newplaces. This would probably work

$List = New-Object  System.Collections.Generic.List[newplace]
$list.add($newplace)
$list

Note again the example and how they are doing it is different. If you run Get-Member on it you’ll see its a ‘newplace’ type, not a ‘map’ type. In the example the booklist is a specific class with a property that stores the books.

my opinion- this is really more C# / .Net than PS despite it being a PS example. I’ve only done minimal things with classes myself so my knowledge on a lot of it is limited. My advice is, if you’re simply trying to learn PS, not to focus on this type of stuff until you have a solid understanding of general PowerShell. You can do this stuff in easier ways in PS anyway. For me it’s been more helpful to know how to use methods of existing things, rather than generating my own classes/methods,.

2 Likes

thanks a lot , i don’t want to copy the original code of book and booklist because my code is trying to add $newplace to a $places that already exist so i was trying to create two different class,and i have manage to solve the problem with code

static  [psobject]add (
 [System.Collections.Generic.List[place]] $places,

    [place]$newplace

)

the following method can be call and pass with

    $places = Import-clixml c:\ex-sys\xml\map.xml
    $newplace =  [place]::new(@{

        x=$x
        y=$y 
        z=$z
        name = $name
    }
        
    )
$newplaces = [place]::add($places, $newplace)
$newplaces |Export-Clixml c:\ex-sys\xml\map.xml


$newplaces