"RSH" <
[email protected]> a écrit dans le message de (e-mail address removed)...
| I have two forms and I need to be able to access a property from from1 in
| form2.
You need to remember that the code that you write in a module containing a
form class is*not* a form, it represents what goes on in an instance of the
class that you are writing.
Static fields/properties are a way of accessing state that is common to the
*type* of the form class, not an *instance* of that class.
You can create more than one instance of any class therefore, whether you
use a static field/property or an instance field/property depends on whether
you want to restrict the value of that member to represent something
relevent to the class rather than an instance. e.g. a good static
field/property would be InstanceCount, which can be updated whenever you
create a new instance of the form to reflect how many instances of that form
class have been created.
I cannot tell from your example whether you intend class use or a value for
each instance.
| The code below does not work but I'm not sure why not. And also why is it
| necessary to declare the variable bIshShown twice?
|
|
| Form1 code:
|
| private static bool bIsShown = false;
This is a static field, applicable to the class Form1, not to instances of
that class. If it were public, you would call it like this : bool test =
Form1.blsShown;
| public bool bIsShown
| {
| get{ return bIsShown; }
| set { bIsShown = value; }
| }
Although this instance property can access the static field, both the field
and the property have the same name and I would doubt if this even compiles.
You need to decide whether you want to change the property to be static, or
whether you want to change the field to be an instance field that would
reflect the state of form1 rather than the class Form1.
Joanna