I want to know the best way to update a custom object properties multiples times is a script. Since using += to keep adding to an array is more memory intensive operation, what is the other way which is more efficient:
I am updating the properties of my hashtable multiples times in a script based on if condition and try and catch blocks.However I do not want to use += approach, what could be the alternative best approach.
when you post code or error messages or sample data or console output format it as code, please.
In the “Text” view you can use the code tags “PRE”, in the “Visual” view you can use the format template “Preformatted”. You can go back edit your post and fix the formatting - you don’t have to create a new one.
Thanks in advance.
You should show the rest of the code or at least the relevant part of it. If you use loops to update your custom object you could use a variable assignement in front of that loop to catch everything what “comes out”.
Thanks I have done that, yes I know I can assign a variable to a loop. However inside the loop, I have multiple if, else conditions and try and catch blocks and inside each block the properties like status and comments of my hashtable change. Inside each of these the properties of my object is changing, hence I want to achieve this without using +=
For example the below code. In this the servicename,status and comments will change under each block, I want to be able to change the properties of this hastable or customobject multiples times in a script without having to use +=
I just gave this as an example, I need to update the properties like servicename,status and comments multiple times in my script, like between multiple if, else blocks and multiple try, catch blocks within the if, else blocks. I do not want to mention inside the catch block that it does not need a catch. This is just a sample code, if in case I need to have a update these properties inside the catch block or other blocks, what is the most efficient way to do so.
That’s what I still dont understand. I does not make any sense to update one property more than once. What counts is value it has when you use this value? Or did I get this wrong again?
The way I showed is most efficient way I know of. But maybe someone else has a more efficient way … you might wait a little longer for more answers.
I’m not sure I follow either. If you’re updating a hash table property, just assign it like
$hashtable[$element].property = "value"
By using += in your discussion, it makes me think you want to add more properties? If so you can add more to a hashtable like so
$hashtable.add('Key',"value")
Now if you’re simply just trying to make a list, by adding elements one at a time, this has nothing to do with a hash table. The below methods work the same on a regular variable as a hashtable property. Please note, system.collections.arraylist is deprecated, I’m just not comfortable enough with the generic lists to provide an example.