Powershell Class collection question

Welcome Forums General PowerShell Q&A Powershell Class collection question

This topic contains 4 replies, has 2 voices, and was last updated by

 
Participant
1 week, 1 day ago.

  • Author
    Posts
  • #113797

    Participant
    Points: 1
    Rank: Member

    I have been working on a scripting project that involves creating custom powershell classes. I am currently trying to understand the following syntax

    $Temp = [myclass[]]::new(1)

    This syntax seems to create a generic List of some kind where I can specify the size of the List. Is there any way for me to modify the constructor for this? Ideally I would like to do something like the following:

    [string[]]$ClassData = @('test1','test2','test3')
    [myclass[]]$Temp = [myclass[]]::new($ClassData)

    And this would call the constructor for myclass for each of the items in $ClassData. Unfortunately this is not the way that it works currently and I have been working around the situation by using the following:

    [string[]]$ClassData = @('test1','test2','test3')
    [myclass[]]$Temp = @($ClassData | [myclass[]]::new($_))

    Any assistance or explanations as to how to make all this work would be greatly appriciated

    Tim

  • #114004

    Participant
    Points: 65
    Published
    Rank: Member

    What you are trying to achieve is to create multiple instances of a class. AFAIK, this cannot be achieved without looping.

    A small correction in your code, you have to use foreach.

    [myclass[]]$Temp = $ClassData | ForEach-Object -Process {[myclass[]]::new($_)}
    • #114007

      Participant
      Points: 1
      Rank: Member

      @kvprasoon I have since found out that

      [myclass[]]$Temp = $ClassData

      will meet the simple example that I used and calls the class constructor with the corresponding data from the $ClassData array. So powershell basically handles the foreach loop automatically using that syntax. I am still experimenting with calling more complex constructors

  • #114013

    Participant
    Points: 65
    Published
    Rank: Member

    Are you sure on [myclass[]]$Temp = $ClassData ? can you post the class definition.

  • #114072

    Participant
    Points: 1
    Rank: Member

    Here is an example to illustrate my point

    class MyClass {
        [string]$Name = ''
        MyClass([string]$Data){
            $this.Name = $Data
        }
    }
    
    $Data = @('This','Is','A','Test')
    [MyClass[]]$Temp = $Data
    
    $Temp[0] | GM
    
    $Temp  | FT

    This will yield the following results

    TypeName: MyClass
    
    Name        MemberType Definition                    
    ----        ---------- ----------                    
    Equals      Method     bool Equals(System.Object obj)
    GetHashCode Method     int GetHashCode()             
    GetType     Method     type GetType()                
    ToString    Method     string ToString()             
    Name        Property   string Name {get;set;} 
    
    Name
    ----
    This
    Is
    A
    Test

You must be logged in to reply to this topic.