473,228 Members | 1,665 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,228 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 2014
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Sergey Klementiev | last post by:
Why it's impossible to have a static indexer in C#?
3
by: boxim | last post by:
got some static props in some classes some of the props need to refer to other static members of the other classes however, cos they're not simple types, cant use const, have to use readonly how...
7
by: Mike P | last post by:
I have a class with a dozen+ properties, some of which will be set a value, and some not, depending on the constructor called. I also have a method which has only one overload and all of the...
25
by: Sahil Malik [MVP] | last post by:
So here's a rather simple question. Say in an ASP.NET application, I wish to share common constants as static variables in global.asax (I know there's web.config bla bla .. but lets just say I...
4
by: Mantorok | last post by:
Hi I have an ASP app which references a few static properties in some of the classes. I understand that you should use Session variables, but is it "possible" to have each session "not"...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
8
by: Per Bull Holmen | last post by:
Hey Im new to c++, so bear with me. I'm used to other OO languages, where it is possible to have class-level initialization functions, that initialize the CLASS rather than an instance of it....
3
by: =?Utf-8?B?TWlrZQ==?= | last post by:
I have a list of static text that I want the client and server to be able to share such that I don't have to hard-code the text on both sides. The client communicates with the server via web...
2
by: peridian | last post by:
Hello, I should have made a note of which version of php 5 I am currently using, but I didn't (I'm pretty sure it's 5.2.2). Ayway, I just want to clarify that my understanding of the static...
5
by: Dave | last post by:
Hello, Suppose you have a class with a static property with only a get (read only). You also have code in a static constructor that sets these properties but takes 1 hour to run. Now suppose...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.