Static properties

  • Thread starter Thread starter RSH
  • Start date Start date
R

RSH

I'm having a bit of trouble understanding Static properties.

I have two forms and I need to be able to access a property from from1 in
form2.

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;

public bool bIsShown

{

get{ return bIsShown; }

set { bIsShown = value; }

}





Form2 code:

form1.bIsShown = false;



Thanks,

Ron
 
RSH,

Form1 code:
private static bool bIsShown = false;

public bool bIsShown

{

get{ return bIsShown; }
must be: get { return Form1.bIsShown; }
set { bIsShown = value; }
must be: set { Form1.bIsShown = value; }

the reason this has to be written like this is that you want to access a
static variable from within an instance. The static variable can be seen
as a variable that is not in a class, so when you are in an instance you
must explicitly write that you are not looking in this instance for the
variable.

Post some more information on the errors you get, I suppose you get an
error that the name bIsShown cannot be used twice...
 
Fields store data, properties do not. You are not declaring a variable
twice, you are defining two different types of objects with the same name.
(Also, you might choose to name the private variable something other than
the property for readability. I tend to put m_ at the from of all my
private variables, others have different conventions.)

Only static properties can assess static fields. Make the property static
as well.
 
"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
 
RSH said:
I'm having a bit of trouble understanding Static properties.

I have two forms and I need to be able to access a property from from1 in
form2.

The code below does not work but I'm not sure why not.

Well, for two reasons:

1) Assuming that form1 is a reference to an instance of Form1, you're
trying to access a static property as if it were an instance property.

2) You can't declare two members with the same name (which you're
trying to).

Note that you've got a static variable but an instance property - this
is almost certanily not what you actually want. Either both should be
static or neither should.
And also why is it necessary to declare the variable bIshShown twice?

You're not - you're declaring a variable, and a property.

I'd recommend not using Hungarian naming, by the way - certainly not
for public members. (I've never liked it for variables either, but I
know some people do.) The naming conventions can be found at
http://tinyurl.com/2cun
 
Peter Rilling said:
Fields store data, properties do not. You are not declaring a variable
twice, you are defining two different types of objects with the same name.
(Also, you might choose to name the private variable something other than
the property for readability. I tend to put m_ at the from of all my
private variables, others have different conventions.)

That much is true.
Only static properties can assess static fields. Make the property static
as well.

That isn't. A static property can't access an instance field (without
an instance to refer to) but an instance property can refer to a static
variable. Here's an example - which is also a demonstration of why it's
a bad idea...

using System;

public class Test
{
static string name;

public string Name
{
get { return name; }
set { name = value; }
}

static void Main()
{
Test t1 = new Test();
Test t2 = new Test();

t1.Name = "Fred";
t2.Name = "Bill";

Console.WriteLine (t1.Name);
}
}
 
Thanks!

That definitely makes sense now.
I was looking at it all wrong...it was somewhat confusing that all of the
examples I saw were using the same variable name only the case was different
(I see that now)...but they indeed are very different animals.

Thanks!

Ron
private static bool bishown;

public static bool bIsShown

{

get

{

return bishown;

}

set

{

bishown = value;

}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top