1.getType() vs “1”.getType()

Welcome Forums General PowerShell Q&A 1.getType() vs “1”.getType()

Viewing 4 reply threads
  • Author
    Posts
    • #245505
      Participant
      Topics: 1
      Replies: 2
      Points: -1
      Rank: Member

      Hey there, i am complete newbie here. Please put some light on following:

      “1”. getType() works

      1.getType() gives error. We need parenthesis like (1).getType()

      1.1.getType() works well

      When you need parenthesis in a expression

      Thanksa lot

       

    • #245658
      Participant
      Topics: 15
      Replies: 1776
      Points: 3,218
      Helping Hand
      Rank: Community Hero

      The Powershell parses has a couple modes, Expression (basic math) and Command. You can see some examples here:

      Issues with Windows PowerShell syntax

      When you type a 1, it assumes you are trying to perform arithmetic and errors saying it wants an expression (e.g. +,-. etc.). Parenthesis is basically a wrapper to do “this” first with PEMDAS, so you then able to basically wrap the expression and then use Get Type. The other example using double qoutes is taking you out of Expression mode and into Command mode because you cannot perform arithmetic strings and would be doing concatenation. The parser is just trying to understand what you are trying to do. Parenthesis is a group operator, which you can see more here: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_operators?view=powershell-7

    • #245859
      Participant
      Topics: 1
      Replies: 2
      Points: -1
      Rank: Member

      thanks a lot. great information.

      if you could say something about 1.1.GetType()

      is it in expression parser-mode?

    • #245973
      Participant
      Topics: 15
      Replies: 1776
      Points: 3,218
      Helping Hand
      Rank: Community Hero

      My assumption is that the first dot (.) is in expression mode as you would do decimal arithmetic, but as soon as you hit dot (.) again it would assume you are looking for a property or method as math operations are a single decimal.

    • #246180
      Participant
      Topics: 1
      Replies: 2
      Points: -1
      Rank: Member

      Ok. Thanks

Viewing 4 reply threads
  • You must be logged in to reply to this topic.