What's this curly braces syntax?

Welcome Forums General PowerShell Q&A What's this curly braces syntax?

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

 
Participant
2 years ago.

  • Author
    Posts
  • #64375

    Participant
    Points: 0
    Rank: Member

    I'm trying to follow this WPF tutorial. The guy uses syntax like this:

    $Button.Add_Click{$CurrentValue ++}

    Powershell 2.0 is unhappy about it but in 5.0 it's fine. Well I know that Add_Click is a method so I replaced it with

    $Button.Add_Click({$CurrentValue ++})

    and 2.0 swallowed it.

    But the question is since what version the omission of parentheses in method calls became legitimate?

  • #64377

    Participant
    Points: 21
    Rank: Member

    I've always known it to be () in all versions. You might ask Jvierra over on the sapien forums, he's been around a lot longer than I.

  • #64380

    Participant
    Points: 513
    Helping Hand
    Rank: Major Contributor

    The {} is a script block

    PS C:\Users\Rob> $test = {}
    
    PS C:\Users\Rob> $test.GetType()
    
    IsPublic IsSerial Name                                     BaseType                                                                                                                                                                
    -------- -------- ----                                     --------                                                                                                                                                                
    True     True     ScriptBlock                              System.Object                                                                                                                                                           
    

    As Powershell has progressed, the developers attempt to take redundant code and let you use 'shortcuts'. For instance, in v2 you have to do | Where {$_.Param -eq "Blue"} and in v3 and above you can do Where Param -eq "Blue". The language is built for administrators and simplicity. If you are writing a production script and not a quick query, it's best to make the code as strongly-typed as possible, in IMHO. I use my code in multiple environments and many, sadly, still use V2, which makes me ensure that all code runs on V2 – V5.

The topic ‘What's this curly braces syntax?’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort