471,306 Members | 1,380 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,306 software developers and data experts.

Static properties

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
Jan 3 '06 #1
6 1950
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...
Form2 code:

form1.bIsShown = false;


Jan 3 '06 #2
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" <wa*************@yahoo.com> wrote in message
news:u8**************@TK2MSFTNGP11.phx.gbl...
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

Jan 3 '06 #3
"RSH" <wa*************@yahoo.com> a écrit dans le message de news:
u8**************@TK2MSFTNGP11.phx.gbl...

| 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

--
Joanna Carter [TeamB]
Consultant Software Engineer
Jan 3 '06 #4
RSH <wa*************@yahoo.com> wrote:
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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 3 '06 #5
Peter Rilling <pe***@nospam.rilling.net> wrote:
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);
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 3 '06 #6
RSH
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;

}

}

"Joanna Carter [TeamB]" <jo****@not.for.spam> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
"RSH" <wa*************@yahoo.com> a écrit dans le message de news:
u8**************@TK2MSFTNGP11.phx.gbl...

| 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

--
Joanna Carter [TeamB]
Consultant Software Engineer

Jan 3 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by Sergey Klementiev | last post: by
3 posts views Thread by boxim | last post: by
25 posts views Thread by Sahil Malik [MVP] | last post: by
4 posts views Thread by Mantorok | last post: by
8 posts views Thread by Per Bull Holmen | last post: by
3 posts views Thread by =?Utf-8?B?TWlrZQ==?= | last post: by
2 posts views Thread by peridian | last post: by
5 posts views Thread by Dave | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.