![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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) |
| | Objects property returns null, but it isn't.... I've encountered a really strange problem trying to access a decimal property on an object that is returned from a cmdlet I've built. If I print out the object to the console, it shows all the members populated and correct. However, if I try and access a property using the ".", it returns nothing. e.g. $myObject = my-cmdlett $myObject # prints out object details to console, everything correct. $myObject.amount # returns nothing. amount is a public property, is a decimal, and is populated with 10.0. I've used "." all over my script to access properties and have had this issue pop up quite often. Usually refactoring the code and just general trial and error fixes the problem - but I'd really like to know why this happens, especially as this time I don't seem to be able to solve it. Anyone have an idea? Thanks. |
My System Specs![]() |
| | #2 (permalink) |
| | RE: Objects property returns null, but it isn't.... Are you sure that your result $myObject is a single object and not an array? If it is an array see the thread "Confused getting properties of Collections of one vs. many" for similar discussion and answers. -- Thanks, Roman |
My System Specs![]() |
| | #3 (permalink) |
| | RE: Objects property returns null, but it isn't.... It's definately a single object. I proved this by trying to index it ($object[0]) - it returned null. "Roman Kuzmin" wrote: > Are you sure that your result $myObject is a single object and not an array? > If it is an array see the thread "Confused getting properties of Collections > of one vs. many" for similar discussion and answers. > > -- > Thanks, > Roman > |
My System Specs![]() |
| | #4 (permalink) |
| | Re: Objects property returns null, but it isn't.... Paul, that indicates that you do have something there, a null value. Here's an example of what I see attempting to index into a single-valued variable and one that has a null value in the indexed location: PS> $foo = 1 PS> $foo[0] Unable to index into an object of type System.Int32. At line:1 char:6 + $foo[0 <<<< ] PS> $foo = $null,1 PS> $foo[0] To confirm this, try getting the Length or Count on the output from the cmdlet. "Paul" <Paul@discussions.microsoft.com> wrote in message news:BAB4F308-D502-4C3D-B6D8-3302512DEA01@microsoft.com... > It's definately a single object. > > I proved this by trying to index it ($object[0]) - it returned null. > > "Roman Kuzmin" wrote: > >> Are you sure that your result $myObject is a single object and not an >> array? >> If it is an array see the thread "Confused getting properties of >> Collections >> of one vs. many" for similar discussion and answers. >> >> -- >> Thanks, >> Roman >> |
My System Specs![]() |
| | #5 (permalink) |
| | Re: Objects property returns null, but it isn't.... Paul wrote: > It's definately a single object. > > I proved this by trying to index it ($object[0]) - it returned null. Then something is definitely wrong, since indexing a non-collection should return an error like "Unable to index into an object of type X". To check if you really got back a single object with correct type, try this: $myObject = my-cmdlett $myObject.gettype().fullname If it is returning back an array (like System.Object[]), dump the contents with this: for ($i = 0; $i -lt $myObject.length; $i++) { "$i=$($myObject[$i])" } I'm suspecting that for some reason you are actually getting back an array with two items: $null and your object. |
My System Specs![]() |
| | #6 (permalink) |
| | Re: Objects property returns null, but it isn't.... You guys rock my world. You're right. I have no idea *how* that variable has become an array because looking at the code it all seems fine. I now know what's wrong though and can work on fixing it. Thanks. "Jouko Kynsijärvi" wrote: > Paul wrote: > > It's definately a single object. > > > > I proved this by trying to index it ($object[0]) - it returned null. > > Then something is definitely wrong, since indexing a non-collection should > return an error like "Unable to index into an object of type X". > > To check if you really got back a single object with correct type, try this: > > $myObject = my-cmdlett > $myObject.gettype().fullname > > If it is returning back an array (like System.Object[]), dump the contents > with this: > > for ($i = 0; $i -lt $myObject.length; $i++) { "$i=$($myObject[$i])" } > > I'm suspecting that for some reason you are actually getting back an array > with two items: $null and your object. > > > |
My System Specs![]() |
| | #7 (permalink) |
| | Re: Objects property returns null, but it isn't.... I don't know what the cmdlet is working on, but we've had prior discussions about how the COM adapter, for example, was returning a null instead of nothing at all when calling methods that had a void return type. Check for method calls in your code that don't send a result somewhere specific, and try explicitly casting them to void. "Paul" <Paul@discussions.microsoft.com> wrote in message news:680A06CA-4E97-452B-856D-D60AB85E96A8@microsoft.com... > You guys rock my world. You're right. I have no idea *how* that variable > has > become an array because looking at the code it all seems fine. I now know > what's wrong though and can work on fixing it. > > Thanks. > > "Jouko Kynsijärvi" wrote: > >> Paul wrote: >> > It's definately a single object. >> > >> > I proved this by trying to index it ($object[0]) - it returned null. >> >> Then something is definitely wrong, since indexing a non-collection >> should >> return an error like "Unable to index into an object of type X". >> >> To check if you really got back a single object with correct type, try >> this: >> >> $myObject = my-cmdlett >> $myObject.gettype().fullname >> >> If it is returning back an array (like System.Object[]), dump the >> contents >> with this: >> >> for ($i = 0; $i -lt $myObject.length; $i++) { "$i=$($myObject[$i])" } >> >> I'm suspecting that for some reason you are actually getting back an >> array >> with two items: $null and your object. >> >> >> |
My System Specs![]() |
| | #8 (permalink) |
| | Re: Objects property returns null, but it isn't.... Paul wrote: > You guys rock my world. You're right. I have no idea *how* that variable has > become an array because looking at the code it all seems fine. I now know > what's wrong though and can work on fixing it. > > Thanks. More evidence in favor of Mike's complaint... :-P |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Net.WebRequest.Create objects GetResponse method returns -1.. why? | .NET General | |||
| Gotcha: $null to [string] IS NOT $null | PowerShell | |||
| Find objects with a certain property | PowerShell | |||
| should out-null offer the option of disposing incoming objects? | PowerShell | |||
| get-adobject only returns 1000 objects | PowerShell | |||