Can observable collection be passed as ref parameter? - wpf

Can an observable collection be passed as a reference parameter?
I am getting an error that this can't be passed as a reference parameter.
This is the exact error message I am getting:
"A property of indexer may not be passed as an out or ref parameter".

Yes, you can pass an ObservableCollection by reference. However, you can't pass this by reference for any class. (You can for a struct, but please don't.)
If you're having trouble passing a normal variable by reference, please post some code.
EDIT: I've just seen your edit... and the compiler error message is fairly clear. You can't pass properties or indexers by reference. So you can't do:
Foo(ref x.SomeProperty);
but you can do:
var tmp = x.SomeProperty;
Foo(ref tmp);
x.SomeProperty = tmp;

Related

Set parameters of an object using a variable in the reference visual basic

I would like to achieve the following in visual basic. Is it possible to be able to have the reference change with each iteration inside the for loop? I have an array of object names (as Strings) stored in "objectArray" that contains the names of objects with the property "valuePath". I would like to change the property of "valuePath" for each object from "objectArray" in one for loop. I do not want to statically call each of the objects manually. I can access the property of the object by calling "Me.objectName.valuePath" from inside the method where this code exists.
For item in objectArray
Me.item.valuePath = "some value"
Next
Any help would be greatly appreciated! Thank you.
I ended up saving an array of Objects instead of an array of Strings and that solved my problem. Thank you for your assistance!

How to access object values using object["key"] approach

In the code I am referring, object attributes are accessed using the object["key"] method instead of calling object.key to access attribute values.
But when I try to create a simple object array and access attributes using above approach, I am getting below error.
if bank_record.effective_date.strip() == "25/07/2019" and bank_record["description"].__contains__("50036"):
TypeError: 'COM' object is not subscriptable
The reason given for object is not subscriptable error is missing __getitem__ method for the class. But in the code I am referring, it doesn't contain such method for any of the dto classes. But the above object["key"] method works just fine. What am I missing. I have been trying to figure this out for a while.
I just want to loop through a object array and access object attributes and modify them on the run. In order to make the function generic, I want to access these object attributes using object["key"] approach. Please help..
My mistake, I have missed set of steps. In the code I am referring, they are looping a json object array, which is created by dumping, python object array values into a json string and loaded back to a json object array.
excel_dto_list = []
#add objects to the list
#...
json_string = json.dumps([ob.__dict__ for ob in excel_dto_list])
#done in another method
downloaded_object = json.loads(json_string)
for x in downloaded_object:
print(x["comment"])

Does "inout" affect array's copy on write behaviour?

I think inout makes you passes in a reference (is that accurate?), then if the reference gets changed many times, as you might do with an array, the array then does not have to copied many times because its now a reference type?
The semantics for in-out parameters in swift is different from passing value by reference. Here's exactly what happens when you're passing an in-out parameter:
In-out parameters are passed as follows:
When the function is called, the value of the argument is copied.
In the body of the function, the copy is modified.
When the function returns, the copy’s value is assigned to the original argument.
This behavior is known as copy-in copy-out or call by value result. For example, when a computed property or a property with observers is passed as an in-out parameter, its getter is called as part of the function call and its setter is called as part of the function return.
See https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/doc/uid/TP40014097-CH34-ID545
Array is value type in swift so it's fully copied in this scenario. Of course the swift compiler may optimize that but anyway you're guaranteed to see exact same behavior as it'd be with full copies performed.
If you want to pass an array by reference and allow the called function to modify elements quickly, you have the choice of either explictly creating an NSMutableArray, or creating a class where instances have an array as their single member.

What is the difference between a model object queried by filter and an object queried by get() in Django?

I keep coming across this issue where I am trying to update a record using the update() method.
It always works when I query an object using filter.
my_dictionary = {"key":"Val","another":"Val"}
thing = Thing.objects.filter(pk=1)
thing[0].update(**my_dictionary) wrote it wrong in the original question.
thing.update(**my_dictionary)
When I query the object using get() it keeps telling me that the object has no method update()
my_dictionary = {"key":"Val","another":"Val"}
thing = Thing.objects.get(pk=1)
thing.update(**my_dictionary)
Isn't a model object the same in both cases? Why would one have an update method and the other one not? Any insight would be greatly appreciated.
The documentation is very explicit about this:
filter() will always give you a QuerySet, even if only a single object matches the query - in this case, it will be a QuerySet containing a single element.
If you know there is only one object that matches your query, you can use the get() method on a Manager which returns the object directly.
Your first snippet returns a QuerySet, which has an update method. The second snippet returns a model instance, which doesn't.
Note that you have not shown the exact code you are using: thing[0].update would give exactly the same error as the second snippet.
You're using QuerySet.update() and ModelInstance.save().
If you’re just updating a record and don’t need to do anything with the model object, the most efficient approach is to call update(), rather than loading the model object into memory. For example, instead of doing this:
e = Entry.objects.get(id=10)
e.comments_on = False
e.save()
...do this:
Entry.objects.filter(id=10).update(comments_on=False)

What does this error actually mean?

So I have seen this error show up, just sometimes, but it is not helpful in describing the actual error which has occured. Nor does it give any clues as to what might cause it to display.
Cannot use modParams with indexes that do not exist.
Can anyone explain more verbosly what this error means, what it relates to (such as a behaviour, component, controller, etc), the most common causes and how to fix it?
To kickstart the investigation, you can find the error here.
https://github.com/cakephp/cakephp/blob/master/lib/Cake/Utility/ObjectCollection.php#L128
Layman's Terms
CakePHP is being told to apply an array of parameters to a collection of objects, such that each particular object can modify the parameters sent on to the next object. There is an error in how CakePHP is being told to do this.
In Depth
Generically, this rises from the CakePHP event publication mechanism. Somewhere in your code is an instance of ObjectCollection, which is being triggered with certain parameters. That is, a method is being called on every object in that collection.
Each callback method is given parameters. Originally the parameters are passed into trigger(). In normal cases (where modParams is false), every callback gets the same parameters. But when modParams is not strictly false, the result of each callback overwrites the parameter indicated by modParams.
So if there are two objects in the collection, modParams is 1, and the params[1] is 'a' initially, then the callback is given the first object with params[1] == a. That callback returns 'b', so when the next callback is called, the second object gets params[1] == b.
The exception raises when the modParams value given does not exist in the originally given params. Eg, if modParams is 2 and params is array (0 => 'a', 1 => 'b'), you'll get this exception.
In your case
Specifically, debugging this has to be done at a low-level because it's a method on a generic class. The backtrace from the Exception should get you bubbled up to a trigger() call on a particular concrete class. That call is being given non-false modParams and a params that doesn't have the given modParams. It could be a code bug in a concrete class extending ObjectCollection, or it could simply be a generic message arising from a method not being given expected arguments.
Have you tried reading the documentation?
/*
* - `modParams` Allows each object the callback gets called on to modify the parameters to the next object.
* Setting modParams to an integer value will allow you to modify the parameter with that index.
* Any non-null value will modify the parameter index indicated.
* Defaults to false.
*/
You did not paste any code, so I guess your 3rd arg of the method contains something wrong.

Resources