Reputation: 548
I'm trying to get picture to update by reference. I know where the issue is happening, but I'm curious if it is possible to do something like this in C# or not.
I'm making a system where people can do dialogues with pictures on the side, and am loading up a "loading" image while calling an async load of the picture (as it may be big and take a little while to load). All of that is working, I'm just curious if I can get away with using a variable of the picture to update the picture, or if I should reference the object (as the object may be one of three different depending on where the user is, but it always references a picture).
This is all pseudocode I tried to name as obviously as possible.
Class AsyncLoad()
{
Picture pic;
string location;
void AsyncLoadStart(string picLocation, Picture picRef)
{
pic = picRef;
location = picLocation;
AsyncLoad();
}
void AsyncLoad()
{
pic = picRef;
Picture picLoading = PictureFromFile(picLocation);
while (picLoading.IsLoad)
Wait();
pic = picLoading; //issue is here
}
}
The issue is that is simply drops the location of the old pic, and make pic reference picLoading. I'm just curious if there's a way to do what I want without reference to the object that pic is attached to.
Clarification : I am trying to update picRef when assinging picLoading to pic. I assume I have to save a reference to the object that picRef is assigned to, and update picRef directly. I am just curious if there is an alternate way, like how the ref keyword would work if this was not asynchronous.
Edit 2 : The program I'm using these graphics in kind of forces me to use 2 seperate methods to load, as I've clarified above - or else I would just have used the ref keyword. I updated the pseudocode to indicate this (it's a game environment that doesn't let me run the async code directly, but from an update call which happens the next frame). Would be easy if I could just put ref on pic.
Upvotes: 0
Views: 51
Reputation: 75565
If your goal is to assign to the original parameter, which seems to be the case based on your code snippet, then you should use the ref
keyword in declaration.
void AsyncLoadStart(string picLocation, ref Picture picRef)
Upvotes: 2