Reputation: 235
I am new to WPF application and also c#. I'll explain my situation:
I have a class called myObject
which has an image instance variable of type Image
During runtime, I want to get the object reference of the image
like:
myObject obj = new myObject();
Image img = obj.getImage();//reference to obj's image variable;
Now from img
i want to get the object obj
's reference. How do I get this?
Edit: Below is a solution.
Guys! First of all Thank you SO Much for all the help! I really appreciate! All of u rock!
Following Noob.net's advice, Whenever i create myObject obj, i stored the obj reference in img.Tag Property!
myObject obj = new myObject();
img.MouseLeftButtonDown += new MouseButtonEventHandler(img_MouseLeftButtonDown);
Image img = obj.getImage();
The handler part:
Image i = (Image)sender;
myObject otemp = (myObject)i.Tag;// refers to the same object from which img was created
Upvotes: 2
Views: 810
Reputation: 64098
If you follow the advice to put Image
and myObject
in a Dictionary
together, you're going to have a bad time. Your images will live as long as the dictionary does and probably will not be disposed of in a timely fashion.
So why track this mapping using a data structure when you can structure your data to track the reference by default?
If you wrap the mapping in a View Model, you'll see what I mean:
public class MyObjectViewModel : INotifyPropertyChanged
{
private myObject obj;
public Image Image
{
get;
private set;
}
public MyObjectViewModel(myObject obj)
{
this.obj = obj;
this.Image = obj.getImage();
}
public void SomethingThatMakesImageChange()
{
this.Image = obj.getImage();
this.RaisePropertyChanged("Image");
}
// ... insert suitable INPC implementation ...
}
This approach has 2 distinct advantages:
Image
class will only live as long as necessary in the viewmyObject
instances, you can use your View Model to handle these changes and updates to the Image
accordingly.Upvotes: 2
Reputation: 6690
I think I didnt understand, but here goes my answer:
public class MyObject
{
private Image img;
public Image Img
{
get { return img; }
set { img = value; }
}
public MyObject()
{
Img = Image.FromFile("");
}
}
public class Main
{
MyObject obj = new MyObject();
//obj.Img = Image.FromFile(""); //if u want
Image image = obj.Img;
}
Upvotes: 0
Reputation: 155
You can use Tag property to store additional information about your Image instance
Upvotes: 2
Reputation: 28338
The only way you can do this directly is if your Image
type has a reference back to it's owner object. If you're talking about the Image
type that's built into .NET then there's no such property.
You will have to take steps to record the parent object of each Image
somewhere else where you can get back to it later. One common pattern would be to maintain a HashTable
in memory where the Image
object is the key and the MyObject
object is the value.
Upvotes: 2
Reputation: 45155
Your Image
class would have to have a Parent
property that holds a reference back to your myObject
instance. Then you'd have to make sure that when you set the image on myObject
that you set the Parent
property.
Now if you can't, or won't add a Parent
property to the Image
class, then your only option would be to compare instance of myObject
with the Image
to see if myObject
contains it. If you have a collection of myObject
, then this is fairly easy with Linq, but the performance may not be great.
Upvotes: 0