What's this curly braces syntax?

This topic contains 2 replies, has 3 voices, and was last updated by Profile photo of Rob Simmers Rob Simmers 5 months, 1 week ago.

  • Author
    Posts
  • #64375
    Profile photo of Mark
    Mark
    Participant

    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
    Profile photo of Dan Potter
    Dan Potter
    Participant

    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
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    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.

You must be logged in to reply to this topic.