Is it possible to access a property of a class from another class without creating a new instance?
I want to be able to alter the state/value of a property of a class from another class.
In my case I have a control who's code was automatically generated by visual studio and want to access it
from my own class.
AndyPosted Dec 3, 2011, 11:48 AM
Anyways, thank you both for taking time and helping me!
Sam HobbsPosted Dec 2, 2011, 4:32 PM
An object-oriented solution would be to create a class that contains the data but no UI members or that is not a UI element, then the data could be put there and gotten from there. You would need a way for one object (class) to notify another object that a change has occured but that is easy using .Net. However explaining how to do it is not as easy as the way Vulpes suggests but once you understand it can make the program more "elegant" and easier to maintain, especially if it will be maintained by more than one person. Even if you are the only one maintaining the program, use of object-oriented designs such as this will likely make it easier for you when you look at the program later and think "well now how did I do that?".
We do not know how the second form is created but inmost situations, one form creates anotehr form and the first form needs to updata data in the second form. One way for the first form to access the second form is for the first form to simply keep a reference to the second form; then you do not need to do:
If that is unclear, then please understand that it will be very clear once you learn the fundamentals of object-oriented design; it is something that works in an equivalent manner in most object-oriented languages. Also, a common solution is to create a public method or proerty in Form2 that the first form can call to change the contents of a control, such as textBox1. That might seem to be unnecessary extra work, but experienced programmers have learned the value of doing things like that; it makes things easier in the future.
Now see how much more time is spent answering this question? That is why members that try to help others often answer with simpler solutions. I know that many of us, iincluding Vulpes, would prefer to spend the time writing an article on the subject and in fact there are many articles on the subject already. Yet members often don't spend any time trying to look for articles.
VulpesPosted Dec 2, 2011, 2:36 PM
AndyPosted Dec 2, 2011, 2:16 PM
VulpesPosted Dec 2, 2011, 1:40 PM