Is this a valid programming technique ? 
Author Message
 Is this a valid programming technique ?

I hope to explain this clearly enough, if in doubt please ask.

I create a class called "Test".

Inside I define a static variable as such: public static string testme = "";

I then add some trivial functions to my class which are not static.

From my application I can then set the value of testme as follows:

Test.testme = "some value";

This is allowed because the variable is static and I don't need an instance
of the class HOWEVER
and this is where it gets interesting:

If I now instantiate the class: Test t = new Test();

A function now inside t can reference the static variable testme. How is
this possible ? Firstly the variable is static
and so I would have thought pretty useless and where is the value of testme
being stored if there is no object instance to start
with ?

This is interesting because I could have 10 objects all of class Test. If I
then change the value of testme by saying Test.testme= "some new value"; The
value changes for each instance of the class. This again shows the classes
are connected in some way ? which I thought was not possible.

Is this a bug ? am I mad ?

thanks!

Craig



Thu, 16 Jun 2005 03:37:07 GMT  
 Is this a valid programming technique ?
a static variable belongs to the type, so any piece of code can access it at
any time, including instance methods of the class.
The static variable is accessed as Test.testme, however, within the scope of
the class testme can be resolved to Test.testme without explicit usage of
the Test prefix.
In otherwords, Test.testme is a single varible, and all accesses to it
accesses the same variable.
So, yes, setting Test.testme changes it in all instances, as well as in
every single other class within your application.

Quote:
> I hope to explain this clearly enough, if in doubt please ask.

> I create a class called "Test".

> Inside I define a static variable as such: public static string testme =
"";

> I then add some trivial functions to my class which are not static.

> From my application I can then set the value of testme as follows:

> Test.testme = "some value";

> This is allowed because the variable is static and I don't need an
instance
> of the class HOWEVER
> and this is where it gets interesting:

> If I now instantiate the class: Test t = new Test();

> A function now inside t can reference the static variable testme. How is
> this possible ? Firstly the variable is static
> and so I would have thought pretty useless and where is the value of
testme
> being stored if there is no object instance to start
> with ?

> This is interesting because I could have 10 objects all of class Test. If
I
> then change the value of testme by saying Test.testme= "some new value";
The
> value changes for each instance of the class. This again shows the classes
> are connected in some way ? which I thought was not possible.

> Is this a bug ? am I mad ?

> thanks!

> Craig



Thu, 16 Jun 2005 03:55:42 GMT  
 Is this a valid programming technique ?
thanks, so its ok to code this way ?

Craig


Quote:
> a static variable belongs to the type, so any piece of code can access it
at
> any time, including instance methods of the class.
> The static variable is accessed as Test.testme, however, within the scope
of
> the class testme can be resolved to Test.testme without explicit usage of
> the Test prefix.
> In otherwords, Test.testme is a single varible, and all accesses to it
> accesses the same variable.
> So, yes, setting Test.testme changes it in all instances, as well as in
> every single other class within your application.


> > I hope to explain this clearly enough, if in doubt please ask.

> > I create a class called "Test".

> > Inside I define a static variable as such: public static string testme =
> "";

> > I then add some trivial functions to my class which are not static.

> > From my application I can then set the value of testme as follows:

> > Test.testme = "some value";

> > This is allowed because the variable is static and I don't need an
> instance
> > of the class HOWEVER
> > and this is where it gets interesting:

> > If I now instantiate the class: Test t = new Test();

> > A function now inside t can reference the static variable testme. How is
> > this possible ? Firstly the variable is static
> > and so I would have thought pretty useless and where is the value of
> testme
> > being stored if there is no object instance to start
> > with ?

> > This is interesting because I could have 10 objects all of class Test.
If
> I
> > then change the value of testme by saying Test.testme= "some new value";
> The
> > value changes for each instance of the class. This again shows the
classes
> > are connected in some way ? which I thought was not possible.

> > Is this a bug ? am I mad ?

> > thanks!

> > Craig



Thu, 16 Jun 2005 05:09:09 GMT  
 Is this a valid programming technique ?
as long as you take it into account, its really no different than doing say
public class StaticTest {
public static int testme;
Quote:
}

public class Test {
    public void Test() {
        StaticTest.testme.ToString();
    }

Quote:
}

however, i wouldn't suggest using the testme value explicitly inside a
method, like any other static value, there really is no gaurentee that two
references to it will return the same value.

Quote:
> thanks, so its ok to code this way ?

> Craig



> > a static variable belongs to the type, so any piece of code can access
it
> at
> > any time, including instance methods of the class.
> > The static variable is accessed as Test.testme, however, within the
scope
> of
> > the class testme can be resolved to Test.testme without explicit usage
of
> > the Test prefix.
> > In otherwords, Test.testme is a single varible, and all accesses to it
> > accesses the same variable.
> > So, yes, setting Test.testme changes it in all instances, as well as in
> > every single other class within your application.


> > > I hope to explain this clearly enough, if in doubt please ask.

> > > I create a class called "Test".

> > > Inside I define a static variable as such: public static string testme
=
> > "";

> > > I then add some trivial functions to my class which are not static.

> > > From my application I can then set the value of testme as follows:

> > > Test.testme = "some value";

> > > This is allowed because the variable is static and I don't need an
> > instance
> > > of the class HOWEVER
> > > and this is where it gets interesting:

> > > If I now instantiate the class: Test t = new Test();

> > > A function now inside t can reference the static variable testme. How
is
> > > this possible ? Firstly the variable is static
> > > and so I would have thought pretty useless and where is the value of
> > testme
> > > being stored if there is no object instance to start
> > > with ?

> > > This is interesting because I could have 10 objects all of class Test.
> If
> > I
> > > then change the value of testme by saying Test.testme= "some new
value";
> > The
> > > value changes for each instance of the class. This again shows the
> classes
> > > are connected in some way ? which I thought was not possible.

> > > Is this a bug ? am I mad ?

> > > thanks!

> > > Craig



Thu, 16 Jun 2005 05:21:02 GMT  
 Is this a valid programming technique ?
Conceptually, a static variable is like a global variable.  There is one
copy and it belongs to a class.  Creating an instance of a class does not
create another static variable.  You can access it without an instance of
the class because it exists when you start up an application - before any
class instances are created.  When you change it from anywhere, you change
that one variable.

David


Quote:
> I hope to explain this clearly enough, if in doubt please ask.

> I create a class called "Test".

> Inside I define a static variable as such: public static string testme =
"";

> I then add some trivial functions to my class which are not static.

> From my application I can then set the value of testme as follows:

> Test.testme = "some value";

> This is allowed because the variable is static and I don't need an
instance
> of the class HOWEVER
> and this is where it gets interesting:

> If I now instantiate the class: Test t = new Test();

> A function now inside t can reference the static variable testme. How is
> this possible ? Firstly the variable is static
> and so I would have thought pretty useless and where is the value of
testme
> being stored if there is no object instance to start
> with ?

> This is interesting because I could have 10 objects all of class Test. If
I
> then change the value of testme by saying Test.testme= "some new value";
The
> value changes for each instance of the class. This again shows the classes
> are connected in some way ? which I thought was not possible.

> Is this a bug ? am I mad ?

> thanks!

> Craig



Thu, 16 Jun 2005 09:49:30 GMT  
 Is this a valid programming technique ?

Don't ya just love it. They have been preaching for years that globals
are bad. So they create a language where everything is encapsulated.
But wait, in some cases we really need globals. No problem, just stick
them in a class and call them static. Now they are OK :-)

On Sat, 28 Dec 2002 20:49:30 -0500, "David Rose"

Quote:

>Conceptually, a static variable is like a global variable.  There is one
>copy and it belongs to a class.  Creating an instance of a class does not
>create another static variable.  You can access it without an instance of
>the class because it exists when you start up an application - before any
>class instances are created.  When you change it from anywhere, you change
>that one variable.

>David



>> I hope to explain this clearly enough, if in doubt please ask.

>> I create a class called "Test".

>> Inside I define a static variable as such: public static string testme =
>"";

>> I then add some trivial functions to my class which are not static.

>> From my application I can then set the value of testme as follows:

>> Test.testme = "some value";

>> This is allowed because the variable is static and I don't need an
>instance
>> of the class HOWEVER
>> and this is where it gets interesting:

>> If I now instantiate the class: Test t = new Test();

>> A function now inside t can reference the static variable testme. How is
>> this possible ? Firstly the variable is static
>> and so I would have thought pretty useless and where is the value of
>testme
>> being stored if there is no object instance to start
>> with ?

>> This is interesting because I could have 10 objects all of class Test. If
>I
>> then change the value of testme by saying Test.testme= "some new value";
>The
>> value changes for each instance of the class. This again shows the classes
>> are connected in some way ? which I thought was not possible.

>> Is this a bug ? am I mad ?

>> thanks!

>> Craig



Sat, 18 Jun 2005 23:59:23 GMT  
 Is this a valid programming technique ?
But they're encapsulated in a class and located in one place.
You can extend from that class and do all sorts of OO things with
it.

Globals == bad

Public static properties == good

See the difference?

-c


Quote:

> Don't ya just love it. They have been preaching for years that globals
> are bad. So they create a language where everything is encapsulated.
> But wait, in some cases we really need globals. No problem, just stick
> them in a class and call them static. Now they are OK :-)

> On Sat, 28 Dec 2002 20:49:30 -0500, "David Rose"

> >Conceptually, a static variable is like a global variable.  There is
one
> >copy and it belongs to a class.  Creating an instance of a class does
not
> >create another static variable.  You can access it without an
instance of
> >the class because it exists when you start up an application - before
any
> >class instances are created.  When you change it from anywhere, you
change
> >that one variable.

> >David



> >> I hope to explain this clearly enough, if in doubt please ask.

> >> I create a class called "Test".

> >> Inside I define a static variable as such: public static string
testme =
> >"";

> >> I then add some trivial functions to my class which are not static.

> >> From my application I can then set the value of testme as follows:

> >> Test.testme = "some value";

> >> This is allowed because the variable is static and I don't need an
> >instance
> >> of the class HOWEVER
> >> and this is where it gets interesting:

> >> If I now instantiate the class: Test t = new Test();

> >> A function now inside t can reference the static variable testme.
How is
> >> this possible ? Firstly the variable is static
> >> and so I would have thought pretty useless and where is the value
of
> >testme
> >> being stored if there is no object instance to start
> >> with ?

> >> This is interesting because I could have 10 objects all of class
Test. If
> >I
> >> then change the value of testme by saying Test.testme= "some new
value";
> >The
> >> value changes for each instance of the class. This again shows the
classes
> >> are connected in some way ? which I thought was not possible.

> >> Is this a bug ? am I mad ?

> >> thanks!

> >> Craig



Sun, 19 Jun 2005 02:12:54 GMT  
 Is this a valid programming technique ?

Quote:

> But they're encapsulated in a class and located in one place.

Not necessarily the latter, even. (See below.)

Quote:
> You can extend from that class and do all sorts of OO things with
> it.

And you can easily split up the globals into logical classes - you might
have some globals to do with the UI, some globals to do with logging,
etc. By encapsulating them in different classes, you end up with more
reusable and readable code, IMO.

--
Jon Skeet

If replying to the group, please do not mail me at the same time



Sun, 19 Jun 2005 19:28:06 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. I am new to programming and am lost

2. State-Machine Techniques, preferred techniques?

3. Good programming style: multidimensional arrays or pointer technique

4. Programming Techniques

5. Object-oriented programming techniques in C?

6. Techniques for event-based programming in C

7. TSR programming techniques in `C'

8. Is this a bad programming technique ?

9. valid C program

10. Is this a valid ANSI program?

11. I am not quite understand the program asks for

12. I am not quite understand the program asks for

 

 
Powered by phpBB® Forum Software