I jus realized that I can change the values of "static variables" and
"instance variable" through the standard constructor This means that
something like this will compile:
public class SomeClass
{
public SomeClass()
{
abc++; // Instance Variable
xyz++; // Static Variable
}
int abc;
static int xyz;
}
However, if instead of using the standard constructor I use a static
constructor, I can only initialize the static variables. The thing that gets
me, is that I always looked at static members on a world of their own
totally separated from the instance class world. But now it looks like the
static variable is going transsexual on me.
Am I missing something? Am I the only one that thinks this behavior is kind
of breaking the rules?
Thanks. 10 2907
Of course you can access the static context from the instance context.
If something is declared static, it does not in any way mean it can only be
accessed from
a static context. If it was, then a fundamental purpose of static members
have been lost.
From a static context however, you cannot access instance members.
Actually, that is very logical. Which instance would we otherwise refer to?
Yes, you can write xyz++. However, you cannot write this.xyz++, but you can
write
this.abc++, since it is an instance member, and thus is in context of
'this', where xyz is not.
When you are changing the abc variable, the value is changed in the scope of
that particular instance.
When you are changing the xyz variable, the value is changed in a global
scope.
Yes, you are right, static members are totally separated from the instance
members.
But you can still reach them from anywhere. Otherwise, there would be just
no point in having static members.
Hope i have been for help in this question.
--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Rene" <no****@nospam. nospam> wrote in message
news:eH******** ******@tk2msftn gp13.phx.gbl... I jus realized that I can change the values of "static variables" and "instance variable" through the standard constructor This means that something like this will compile:
public class SomeClass { public SomeClass() { abc++; // Instance Variable xyz++; // Static Variable } int abc; static int xyz; }
However, if instead of using the standard constructor I use a static constructor, I can only initialize the static variables. The thing that gets me, is that I always looked at static members on a world of their own totally separated from the instance class world. But now it looks like the static variable is going transsexual on me.
Am I missing something? Am I the only one that thinks this behavior is kind of breaking the rules? Thanks.
I guess it would make more sense to me if the required syntax was something
like:
SomeClass. xyz++;
not just the plain xyz++, just calling xyz++ does not seem like your typical
static call.
"Dennis Myrén" <de****@oslokb. no> wrote in message
news:Il******** ************@ne ws4.e.nsc.no... Of course you can access the static context from the instance context. If something is declared static, it does not in any way mean it can only be accessed from a static context. If it was, then a fundamental purpose of static members have been lost.
From a static context however, you cannot access instance members. Actually, that is very logical. Which instance would we otherwise refer to? Yes, you can write xyz++. However, you cannot write this.xyz++, but you can write this.abc++, since it is an instance member, and thus is in context of 'this', where xyz is not. When you are changing the abc variable, the value is changed in the scope of that particular instance. When you are changing the xyz variable, the value is changed in a global scope.
Yes, you are right, static members are totally separated from the instance members. But you can still reach them from anywhere. Otherwise, there would be just no point in having static members.
Hope i have been for help in this question.
-- Regards, Dennis JD Myrén Oslo Kodebureau "Rene" <no****@nospam. nospam> wrote in message news:eH******** ******@tk2msftn gp13.phx.gbl...I jus realized that I can change the values of "static variables" and "instance variable" through the standard constructor This means that something like this will compile:
public class SomeClass { public SomeClass() { abc++; // Instance Variable xyz++; // Static Variable } int abc; static int xyz; }
However, if instead of using the standard constructor I use a static constructor, I can only initialize the static variables. The thing that gets me, is that I always looked at static members on a world of their own totally separated from the instance class world. But now it looks like the static variable is going transsexual on me.
Am I missing something? Am I the only one that thinks this behavior is kind of breaking the rules? Thanks.
Well, specifying the class in this case is superfluos, since you are already
there.
If, from another class, you would like to change xyz, then yes you will of
course
need to use the fully qualified name, which is [NAMESPACE(S)].SomeClass.
xyz++;
--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Rene" <no****@nospam. nospam> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. .. I guess it would make more sense to me if the required syntax was something like:
SomeClass. xyz++;
not just the plain xyz++, just calling xyz++ does not seem like your typical static call.
"Dennis Myrén" <de****@oslokb. no> wrote in message news:Il******** ************@ne ws4.e.nsc.no... Of course you can access the static context from the instance context. If something is declared static, it does not in any way mean it can only be accessed from a static context. If it was, then a fundamental purpose of static members have been lost.
From a static context however, you cannot access instance members. Actually, that is very logical. Which instance would we otherwise refer to? Yes, you can write xyz++. However, you cannot write this.xyz++, but you can write this.abc++, since it is an instance member, and thus is in context of 'this', where xyz is not. When you are changing the abc variable, the value is changed in the scope of that particular instance. When you are changing the xyz variable, the value is changed in a global scope.
Yes, you are right, static members are totally separated from the instance members. But you can still reach them from anywhere. Otherwise, there would be just no point in having static members.
Hope i have been for help in this question.
-- Regards, Dennis JD Myrén Oslo Kodebureau "Rene" <no****@nospam. nospam> wrote in message news:eH******** ******@tk2msftn gp13.phx.gbl...I jus realized that I can change the values of "static variables" and "instance variable" through the standard constructor This means that something like this will compile:
public class SomeClass { public SomeClass() { abc++; // Instance Variable xyz++; // Static Variable } int abc; static int xyz; }
However, if instead of using the standard constructor I use a static constructor, I can only initialize the static variables. The thing that gets me, is that I always looked at static members on a world of their own totally separated from the instance class world. But now it looks like the static variable is going transsexual on me.
Am I missing something? Am I the only one that thinks this behavior is kind of breaking the rules? Thanks.
And since I am in a role asking stupid questions why is it that you can't
have an instance and static variables declare with the same name? Aren't
both variables in totally different scopes?
int abc;
static int abc; // Error: already contains a definition for 'abc'
If it was possible to declare an instance member and a static member
with the same name, in the same class, then you would always have to
refer to the static member with it's fully qualified name, or refer to the
instance member with "this" always, to distuingish them.
public class SomeClass
{
public SomeClass()
{
abc++; // Instance Variable(or is it the static?)
abc++; // Static Variable(or is it the instance member?)
this.abc++//Certainly the instance member.
SomeClass.abc++//Certainly the static member.
}
int abc;
static int abc;
}
This is a C# design question, which i am not in position to answer.
My best bet though, why this is not possible, is that it would be kind of
nasty.
This is a limitation of C# though, as it would be possible to do in the
Intermediate Language.
--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Rene" <no****@nospam. nospam> wrote in message
news:Ok******** ********@TK2MSF TNGP12.phx.gbl. .. And since I am in a role asking stupid questions why is it that you can't have an instance and static variables declare with the same name? Aren't both variables in totally different scopes?
int abc; static int abc; // Error: already contains a definition for 'abc'
Rene <no****@nospam. nospam> wrote: And since I am in a role asking stupid questions why is it that you can't have an instance and static variables declare with the same name? Aren't both variables in totally different scopes? int abc; static int abc; // Error: already contains a definition for 'abc'
No, they have the same scope. From the C# spec:
<quote>
The scope of a member declared by a class-member-declaration (§17.2) is
the class-body in which the declaration occurs. In addition, the scope
of a class member extends to the class-body of those derived classes
that are included in the accessibility domain (§10.5.2) of the member.
</quote>
class-member-declaration includes both instance variables and static
variables.
I think the main reason for disallowing it is to try to prevent
confusion. In theory you could distinguish between them using this.abc
or ClassName.abc, but I don't think it would ever really be a good
idea.
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Thanks for putting up with my stupid question Dennis. I am still learning
and every once in a while a connection goes bad in my brain and I get
confused with the stupidest most basic things.
Thanks again.
I do not in any way think it was a stupid question.
We all have to start from somewhere, do we not?
Take care.
--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Rene" <no****@nospam. nospam> wrote in message
news:%2******** ********@TK2MSF TNGP11.phx.gbl. .. Thanks for putting up with my stupid question Dennis. I am still learning and every once in a while a connection goes bad in my brain and I get confused with the stupidest most basic things. Thanks again.
> From a static context however, you cannot access instance members. Actually, that is very logical. Which instance would we otherwise refer to? Yes, you can write xyz++. However, you cannot write this.xyz++, but you can write this.abc++, since it is an instance member, and thus is in context of 'this', where xyz is not. When you are changing the abc variable, the value is changed in the scope of that particular instance. When you are changing the xyz variable, the value is changed in a global scope.
One small nit, you can access instance state from a static method as long as
that method is passed an instance:
class Foo
{
int abc;
public static Bar(Foo f)
{
f.abc++;
}
}
Regards
Richard Blewett - DevelopMentor http://www.dotnetconsult.co.uk/weblog http://www.dotnetconsult.co.uk This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Yu |
last post by:
I have found that the static object is initialised at the time when
the shared libary is loaded. The initialisation caused the invocation
of the constructor.
May I know of any way that I can initialize the static object without
invoking the constructor?
Below is the sample coding.
Header file ASURegistrationManager.h
#include "ASURegistration.h"
|
by: baumann |
last post by:
hi all,
according the private / protected access control,
- private; that is, its name can be used only by members and friends
of the class in which it is
declared.
- protected; that is, its name can be used only by members and
friends of the class in which it is
|
by: ank |
last post by:
Hi,
I was curious about how to define static data member of template class.
Should I put the definition in a separate source file or in the same
header file as its template class?
And when this data will be initialized if it is used across many
translation unit, assume that it has constructor and needs dynamic
initialization?
I have blindly searched for the answer but I still not thoroughly
|
by: Dave |
last post by:
Hi everyone,
Is it possible, using an Attribute or by some other means, to notify the C#
Compiler to serialize all static field's that have initializers before code
in an explicit static constructor?
Example:
public class MyClass
{
|
by: Dennis |
last post by:
I have a function which is called from a loop many times. In that function,
I use three variables as counters and for other purposes. I can either use
DIM for declaring the variables or Static. Would the performance be better
using Static versus Dynamic. I would think it would be quicker with STATIC
declarations since the variables would only have to be created once. Can
anyone confirm this. Thanks.
--
Dennis in Houston
| |
by: Vladislav Kosev |
last post by:
I have this strange problem now twice: I am writing this relatevely large web
site on 2.0 and I made a static class, which I use for url encoding and
deconding (for remapping purposes). This static class needs the session
context to encode a url (because I stored the current language there), so I
made a static field of type HttpContext, which I refresh every reqest by
assigning the current context.
Now, every now and then I get this...
|
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. Like, for instance
the Objective-C method:
+(void)initialize
Which has the following characteristics: It is guaranteed to be run
|
by: Sandro Bosio |
last post by:
Hello everybody, my first message on this forum. I tried to solve my issue by reading other similar posts, but I didn't succeed. And forgive me if this mail is so long.
I'm trying to achieve the following (with incomplete succes): I want in a given namespace Parameters a list of "initializers" (which are objects derived from a simple interface that can be implemented anywhere, and are used to define which parameters the program will take at...
|
by: Jess |
last post by:
Hello,
I learned that there are five kinds of static objects, namely
1. global objects
2. object defined in namespace scope
3. object declared static instead classes
4. objects declared static inside functions (i.e. local static
objects)
5. objects declared at file scope.
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |