- This topic has 4 replies, 2 voices, and was last updated 1 month, 3 weeks ago by
July 29, 2020 at 1:30 am #245505
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
July 29, 2020 at 12:00 pm #245658ParticipantTopics: 15Replies: 1776Points: 3,218Rank: Community Hero
The Powershell parses has a couple modes, Expression (basic math) and Command. You can see some examples here:
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-7PowerShell1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950PS C:\Users\rasim> 1.GetType()At line:1 char:11+ 1.GetType()+ ~An expression was expected after '('.+ CategoryInfo : ParserError: (:) , ParentContainsErrorRecordException+ FullyQualifiedErrorId : ExpectedExpressionPS C:\Users\rasim> (1).GetType()IsPublic IsSerial Name BaseType-------- -------- ---- --------True True Int32 System.ValueType#Another example of PEMDAS, finish the expression and the get the typePS C:\Users\rasim> (1 + 2).GetType()IsPublic IsSerial Name BaseType-------- -------- ---- --------True True Int32 System.ValueType#Convert to a string and get the typePS C:\Users\rasim> "1".GetType()IsPublic IsSerial Name BaseType-------- -------- ---- --------True True String System.Object#Defining the numbers as strings results in concatenation, not addition arithmetic of 1+1 = 2PS C:\Users\rasim> "1" + "1"11#Just typing a letter, the error shows Powershell is in expression mode trying to understand the taskPS C:\Users\rasim> a.GetType()At line:1 char:11+ a.GetType()+ ~An expression was expected after '('.+ CategoryInfo : ParserError: (:) , ParentContainsErrorRecordException+ FullyQualifiedErrorId : ExpectedExpression#The parenthesis here take you out of Expression mode in Command mode, so it's looking for a command or alias or something defined as "a"PS C:\Users\rasim> (a).GetType()a : The term 'a' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.At line:1 char:2+ (a).GetType()+ ~+ CategoryInfo : ObjectNotFound: (a:String) , CommandNotFoundException+ FullyQualifiedErrorId : CommandNotFoundException
July 30, 2020 at 12:12 am #245859
thanks a lot. great information.
if you could say something about 1.1.GetType()
is it in expression parser-mode?
July 30, 2020 at 10:26 am #245973ParticipantTopics: 15Replies: 1776Points: 3,218Rank: 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.
July 30, 2020 at 11:22 pm #246180
- You must be logged in to reply to this topic.