How do I prevent typos in property names of Objects?

Welcome Forums General PowerShell Q&A How do I prevent typos in property names of Objects?

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

GS
 
Participant
3 years, 10 months ago.

  • Author
    Posts
  • #22148
    GS

    Participant
    Points: 1
    Rank: Member

    Hello,

    It tooks me an hour to found simple typo in statement "$file.Trim().lenth -gt 0" where $file is just String object. Obviously I would not be able even to compile this if this would be .NET application but in powershell it did not do anything at all and just returned $false for that statement.
    There is no solution in Powershell for this issue and I just need to be very carefull or there is some sort of "Option Explicit" or similar I can define to raise Exception on trying to get Property which does not exist?

    Thanks,
    G

  • #22149

    Member
    Points: 0
    Rank: Member

    Yep:

    Set-StrictMode -Version Latest
    

    This does a number of things, one of which is producing an error when you try to access a non-existent property.

  • #22150

    Keymaster
    Points: 1,704
    Helping HandTeam Member
    Rank: Community Hero

    Also try to get in the habit of using Tab completion. When it's available, it'll help prevent typos like that.

  • #22163
    GS

    Participant
    Points: 1
    Rank: Member

    Tab completion does not work if it's not property of defined object
    $a.Length works but $a.Trim().Length does not

The topic ‘How do I prevent typos in property names of Objects?’ is closed to new replies.