Click here to Skip to main content
15,894,405 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i tried MainWindow main;. but it wasn't of any use and i dont want to use new for the same..
Posted

1 solution

When you use new you create a new instance of the class, which has all it's fields and properties independent to any existing instances: it's like cars. It you put your wallet in the glove box of your car, then buy a new car - would you expect to find your wallet in the new car's glove box?

So you are right not to use new if you are trying to access the existing main window - you need to get the actual instance of the window that the user is looking at.

If you are doing this from within the MainWindow class, then that;s pretty easy - you don't have to do anything because your code will automatically reference the current instance when you use a property or field name (unless you are inside a static method, where there is no instance).

If you are outside the class, then it's a lot, lot harder - and will depend on how your code works - because you wouldn't normally want code to refer to external windows as that breaks the OOPs principles. What are you actually trying to achieve that you need this?
 
Share this answer
 
Comments
Member 11521909 23-May-15 5:44am    
i have a class named designer canvas n it generates guid's for every image i drop on the canvas and i want to access the guids n put them into a list created on the MainWindow class
OriginalGriff 23-May-15 6:03am    
So - you don't want to do it like that.
What you want to do is create an event within the class that the main windows class handles. When you have a new Guid (or Guids) you signal the event, and the main window either retrieves them from a custom EventArgs or via a property from your class. That way, your canvas is doesn't need to know what it is embedded in - it just raises the event and the outside world decides what to do about it.

See here:
http://www.codeproject.com/Tips/548131/Transferring-information-between-two-forms-Part

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900