locked
Class in Powershell v5 RRS feed

  • Question

  • Hello ,

    Unable to create class in PowerShell v5. The below is the error message, Kindly advice.

    Thanks and Regards,
    Anirban Singha

    Saturday, November 29, 2014 7:33 PM

Answers

  • The function is declared void

    [int] cal() ...

    • Proposed as answer by AnnaWY Monday, December 8, 2014 1:35 AM
    • Marked as answer by AnnaWY Monday, December 8, 2014 8:28 AM
    Sunday, November 30, 2014 7:40 AM
  • What happens if you close all PowerShell consoles and try in new one. I came across the same issue and opening new console fixed it.

    http://social.technet.microsoft.com/wiki/contents/articles/28610.powershell-5-0-writing-class-using-powershell.aspx

    Try this code and let me know

     Class Classroom 
        {
    
            #Properties
    
            [String]$Title = "Mr."
    
            [String]$FirstName = "Chendrayan"
    
            [String]$LastName = "Venkatesan"
    
            [String]$Role = "Trainer"
    
            
    
            #Methods
    
            [Void] GetInfo()
    
                {
    
                    $this.Title = $Title
    
                    $this.FirstName = $FirstName
    
                    $this.LastName = $LastName
    
                    $this.Role = $Role
    
                }
    
            [Void] SetInfo([string]$t , [string]$fn , [string]$ln , [String]$r)
    
                {
    
                    $this.Title = $t
    
                    $this.FirstName = $Fn
    
                    $this.LastName = $ln
    
                    $this.Role = $r
    
                }        
    
        }
    
    
    $G = [Classroom]::New()
    
    $G 
    


    Regards Chen V [MCTS SharePoint 2010]



    • Edited by Chen VMVP Saturday, November 29, 2014 8:32 PM
    • Marked as answer by AnnaWY Monday, December 8, 2014 8:25 AM
    Saturday, November 29, 2014 8:20 PM
  • Consider that a class has to have content:

    class test{$x=1}
    $test=[test]::New()

    The first error is likely just a "preview" release bug.  Check on "connect".


    ¯\_(ツ)_/¯

    • Marked as answer by AnnaWY Monday, December 8, 2014 8:26 AM
    Saturday, November 29, 2014 11:05 PM
  • Hi Anirban,

    As Brian suggested, I tested as below:

    class test {
    
    [int] $firstnumber;
    [int] $secondnumber;
    
    [int] cal([int] $a,[int] $b) #change to int instead of void
    {
    [int] $c = $a + $b
     
    return $c #add return
    
    }
    	}
    
    $Newtest = [Test]::new()
    $Newtest.Firstnumber=1
    $Newtest.Secondnumber=2
    $NewTest|ft -AutoSize
    
    $NewTest.cal(1,2)

    If there is anything else regarding this issue, please feel free to post back.

    Best Regards,

    Anna Wang

    • Edited by AnnaWY Thursday, December 4, 2014 9:19 AM
    • Proposed as answer by AnnaWY Monday, December 8, 2014 1:35 AM
    • Marked as answer by AnnaWY Monday, December 8, 2014 8:28 AM
    • Unmarked as answer by AnirbanSingha Saturday, December 13, 2014 6:07 PM
    • Marked as answer by AnirbanSingha Saturday, December 13, 2014 6:07 PM
    Thursday, December 4, 2014 9:18 AM

All replies

  • Hello ,

    Unable to create class in PowerShell v5. The below is the error message, Kindly advice.

    Thanks and Regards,
    Anirban Singha

    • Merged by AnnaWY Wednesday, December 3, 2014 10:52 AM duplicate
    Saturday, November 29, 2014 7:33 PM
  • PS C:\test> $Object = New-Object PSObject -Property (@{"Property1"="Value1";"Property2"="Value2";})
    PS C:\test> $Object
    
    Property1                                                           Property2                                                          
    ---------                                                           ---------                                                          
    Value1                                                              Value2                                                             

    The alternate way to create classes see WIKI article

    How to Create and Use Classes in PowerShell
    http://social.technet.microsoft.com/wiki/contents/articles/27080.how-to-create-and-use-classes-in-powershell.aspx

     
    Saturday, November 29, 2014 7:49 PM
  • What happens if you close all PowerShell consoles and try in new one. I came across the same issue and opening new console fixed it.

    http://social.technet.microsoft.com/wiki/contents/articles/28610.powershell-5-0-writing-class-using-powershell.aspx

    Try this code and let me know

     Class Classroom 
        {
    
            #Properties
    
            [String]$Title = "Mr."
    
            [String]$FirstName = "Chendrayan"
    
            [String]$LastName = "Venkatesan"
    
            [String]$Role = "Trainer"
    
            
    
            #Methods
    
            [Void] GetInfo()
    
                {
    
                    $this.Title = $Title
    
                    $this.FirstName = $FirstName
    
                    $this.LastName = $LastName
    
                    $this.Role = $Role
    
                }
    
            [Void] SetInfo([string]$t , [string]$fn , [string]$ln , [String]$r)
    
                {
    
                    $this.Title = $t
    
                    $this.FirstName = $Fn
    
                    $this.LastName = $ln
    
                    $this.Role = $r
    
                }        
    
        }
    
    
    $G = [Classroom]::New()
    
    $G 
    


    Regards Chen V [MCTS SharePoint 2010]



    • Edited by Chen VMVP Saturday, November 29, 2014 8:32 PM
    • Marked as answer by AnnaWY Monday, December 8, 2014 8:25 AM
    Saturday, November 29, 2014 8:20 PM
    • Proposed as answer by Mike Laughlin Monday, December 1, 2014 2:39 PM
    Saturday, November 29, 2014 8:21 PM
  • To instantiate an instance of a class you need to use it's new() constructor.  Once you have registered the class, you do it like this :

    $whatever = [classname]::New()

    Here's an example.

    class Dice
    {
    [int32] $value

        [void] Roll()
        {
           $this.value = Get-Random -Minimum 1 -Maximum 6

        }

    }

    PS> $dice = [Dice]::New()
    PS> $dice.Roll()
    PS> $dice | ft -AutoSize

    value
    -----
        1

    PS> $dice.Roll()
    PS> $dice | ft -AutoSize

    value
    -----
        4

    • Edited by _TimPringle Saturday, November 29, 2014 8:43 PM
    • Proposed as answer by _TimPringle Saturday, November 29, 2014 8:44 PM
    Saturday, November 29, 2014 8:40 PM
  • Consider that a class has to have content:

    class test{$x=1}
    $test=[test]::New()

    The first error is likely just a "preview" release bug.  Check on "connect".


    ¯\_(ツ)_/¯

    • Marked as answer by AnnaWY Monday, December 8, 2014 8:26 AM
    Saturday, November 29, 2014 11:05 PM
  • Hi All,

    It works with all your advice, I am calling a method and inside method (cal) I have a variable $c, The variable $c only display in write-host and if i mentioned only $c, it not showing any output, Any reason for this kind of behavior.

    Thanks and Regards,
    Anirban Singha

    Sunday, November 30, 2014 6:44 AM
  • The function is declared void

    [int] cal() ...

    • Proposed as answer by AnnaWY Monday, December 8, 2014 1:35 AM
    • Marked as answer by AnnaWY Monday, December 8, 2014 8:28 AM
    Sunday, November 30, 2014 7:40 AM
  • Hi Anirban,

    As Brian suggested, I tested as below:

    class test {
    
    [int] $firstnumber;
    [int] $secondnumber;
    
    [int] cal([int] $a,[int] $b) #change to int instead of void
    {
    [int] $c = $a + $b
     
    return $c #add return
    
    }
    	}
    
    $Newtest = [Test]::new()
    $Newtest.Firstnumber=1
    $Newtest.Secondnumber=2
    $NewTest|ft -AutoSize
    
    $NewTest.cal(1,2)

    If there is anything else regarding this issue, please feel free to post back.

    Best Regards,

    Anna Wang

    • Edited by AnnaWY Thursday, December 4, 2014 9:19 AM
    • Proposed as answer by AnnaWY Monday, December 8, 2014 1:35 AM
    • Marked as answer by AnnaWY Monday, December 8, 2014 8:28 AM
    • Unmarked as answer by AnirbanSingha Saturday, December 13, 2014 6:07 PM
    • Marked as answer by AnirbanSingha Saturday, December 13, 2014 6:07 PM
    Thursday, December 4, 2014 9:18 AM