![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | Accessing Collection type items using Negative Index Numbers It doesn't seem to be possible to access .NET collection type's item with negative index numbers but if i use "+=" operator, then PowerShell converts the object types to "System.Object[]" so negative indexing works. Is this by design because .Net collections do not take negative index numbers?(for consistency reasons) Another question: Why does "+=" changes the type of the object instance instead of using underlying "Add" method? Case 1) Non-Generic .NET Types(Negative Indexes dont work) [^_^]PS[24]>$m = [RegEx]::Matches('abc', 'abc') [^_^]PS[25]>$m.Count 1 [^_^]PS[26]>$m[-1] [^_^]PS[27]>$m.GetType().FullName System.Text.RegularExpressions.MatchCollection [^_^]PS[28]>$b = @("abc") [^_^]PS[29]>$b[-1] abc [^_^]PS[30]>$b.GetType().FullName System.Object[] [^_^]PS[39]>$arr = New-Object System.Collections.ArrayList [^_^]PS[40]>[void] $arr.Add("abc") [^_^]PS[41]>[void] $arr.Add("def") [^_^]PS[42]>$arr.GetType().FullName System.Collections.ArrayList [^_^]PS[43]>$arr[-1] [^_^]PS[44]>rv arr 1.1) Using "+=" operator instead of "Add" method("+=" changed type of $arr so negative index works) [^_^]PS[45]>$arr = New-Object System.Collections.ArrayList [^_^]PS[46]>$arr += 'abc' [^_^]PS[47]>$arr += 'def' [^_^]PS[48]>$arr.GetType().FullName System.Object[] [^_^]PS[49]>$arr[-1] def Case #2) Generic Object(Negative index does not work) [^_^]PS[50]>$g = New-Object System.Collections.ObjectModel.Collection``1"[[System.String]]" [^_^]PS[51]>$g.Add("abc") [^_^]PS[52]>$g.Add("def") [^_^]PS[53]>$g.Count 2 [^_^]PS[54]>$g[-1] [^_^]PS[55]>$g.item[-1] Unable to index into an object of type System.Management.Automation.PSParameterizedProperty. At line:1 char:9 + $g.item[- <<<< 1] [^_^]PS[56]>rv g 2.1) Using "+=" to add an item changes object type from "Collection" to "System.Object[]" (Negative indexing works) [^_^]PS[57]>$g = New-Object System.Collections.ObjectModel.Collection``1"[[System.String]]" [^_^]PS[58]>$g += "abc" [^_^]PS[59]>$g += "def" [^_^]PS[60]>$g.Count 2 [^_^]PS[61]>$g[-1] def [^_^]PS[62]>$g.GetType().FullName System.Object[] |
My System Specs![]() |
| | #2 (permalink) |
| | Re: Accessing Collection type items using Negative Index Numbers "DBMwS" <DBMwS@discussions.microsoft.com> wrote in message news:4A1D1590-A301-43D6-A836-BCBBB703F5A5@microsoft.com... > It doesn't seem to be possible to access .NET collection type's item with > negative index numbers > but if i use "+=" operator, then PowerShell converts the object types to > "System.Object[]" so > negative indexing works. > > Is this by design because .Net collections do not take negative index > numbers?(for consistency reasons) I think that is a safe bet. Even VB changed the way it accesses arrays and collections to be zero based. Negative indexes are "right out". > Another question: Why does "+=" changes the type of the object instance > instead of using underlying "Add" method? My guess is that the ArrayList occupies index 0 and the string 'abc' occupies index 1 and 'def' occupies index 2. When PowerShell sees += and the expression on the LHS doesn't represent an event then PoSH assumes you are appending to an array. Since the types of the first two elements differ then it can only hold the items in an object[]. -- Keith |
My System Specs![]() |
| | #3 (permalink) |
| | Re: Accessing Collection type items using Negative Index Numbers Thanks for the response there, Keith. PowerShell is a *power*ful shell as well as a language but still "scripting" side of nature might be a bit degraded had it not supported negative indexing at all.. I tend not to use negative indexes when i write long scripts. But I personally think that, not being able to use negative index number to retrieve last collection item would be too much to type sometimes in an * interactive * mode |
My System Specs![]() |
| | #4 (permalink) |
| | Re: Accessing Collection type items using Negative Index Numbers Negative indexes not working on a collection is just a bug. Please post on the connect site. -- Bruce Payette [MSFT] Windows PowerShell Technical Lead Microsoft Corporation This posting is provided "AS IS" with no warranties, and confers no rights. Visit the Windows PowerShell Team blog at: http://blogs.msdn.com/PowerShell Visit the Windows PowerShell ScriptCenter at: http://www.microsoft.com/technet/scr.../hubs/msh.mspx "DBMwS" <DBMwS@discussions.microsoft.com> wrote in message news:4A1D1590-A301-43D6-A836-BCBBB703F5A5@microsoft.com... > It doesn't seem to be possible to access .NET collection type's item with > negative index numbers > but if i use "+=" operator, then PowerShell converts the object types to > "System.Object[]" so > negative indexing works. > > Is this by design because .Net collections do not take negative index > numbers?(for consistency reasons) > > Another question: Why does "+=" changes the type of the object instance > instead of using underlying "Add" method? > > > Case 1) Non-Generic .NET Types(Negative Indexes dont work) > [^_^]PS[24]>$m = [RegEx]::Matches('abc', 'abc') > [^_^]PS[25]>$m.Count > 1 > [^_^]PS[26]>$m[-1] > [^_^]PS[27]>$m.GetType().FullName > System.Text.RegularExpressions.MatchCollection > [^_^]PS[28]>$b = @("abc") > [^_^]PS[29]>$b[-1] > abc > [^_^]PS[30]>$b.GetType().FullName > System.Object[] > [^_^]PS[39]>$arr = New-Object System.Collections.ArrayList > [^_^]PS[40]>[void] $arr.Add("abc") > [^_^]PS[41]>[void] $arr.Add("def") > [^_^]PS[42]>$arr.GetType().FullName > System.Collections.ArrayList > [^_^]PS[43]>$arr[-1] > [^_^]PS[44]>rv arr > 1.1) Using "+=" operator instead of "Add" method("+=" changed type of $arr > so negative index works) > [^_^]PS[45]>$arr = New-Object System.Collections.ArrayList > [^_^]PS[46]>$arr += 'abc' > [^_^]PS[47]>$arr += 'def' > [^_^]PS[48]>$arr.GetType().FullName > System.Object[] > [^_^]PS[49]>$arr[-1] > def > > Case #2) Generic Object(Negative index does not work) > [^_^]PS[50]>$g = New-Object > System.Collections.ObjectModel.Collection``1"[[System.String]]" > [^_^]PS[51]>$g.Add("abc") > [^_^]PS[52]>$g.Add("def") > [^_^]PS[53]>$g.Count > 2 > [^_^]PS[54]>$g[-1] > [^_^]PS[55]>$g.item[-1] > Unable to index into an object of type > System.Management.Automation.PSParameterizedProperty. > At line:1 char:9 > + $g.item[- <<<< 1] > [^_^]PS[56]>rv g > 2.1) Using "+=" to add an item changes object type from "Collection" to > "System.Object[]" > (Negative indexing works) > [^_^]PS[57]>$g = New-Object > System.Collections.ObjectModel.Collection``1"[[System.String]]" > [^_^]PS[58]>$g += "abc" > [^_^]PS[59]>$g += "def" > [^_^]PS[60]>$g.Count > 2 > [^_^]PS[61]>$g[-1] > def > [^_^]PS[62]>$g.GetType().FullName > System.Object[] > |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Get-Process has processes with negative numbers | PowerShell | |||
| remove items from index manually | Vista file management | |||
| Windows Experience Index items gone | Vista General | |||
| accessing the extended type system from C# | PowerShell | |||
| Question regarding negative (hexa)decimal numbers | PowerShell | |||