473,795 Members | 3,481 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

static & instance variables

I have the following class:

class ProvisionCollec tion
{
...

private int m_VarianceCount ;
public int VarianceCount
{
get { return m_VarianceCount ; }
}

public static ProvisionCollec tion GetProvisions(. ..)
{
ProvisionCollec tion list = new ProvisionCollec tion();

... populate collection ...

m_VarianceCount = (int)cmd.Parame ters["@SP_66_COU NT"].Value;
return list;
}
}

My question: Should this static method have access to modify the
private instance variable "m_VarianceCoun t"? Because right now it
works perfectly, but it doesn't seem like it should.

Thanks,
Jordan

May 10 '07 #1
14 9920
Jordan Marr <jn****@hotmail .comwrote:
I have the following class:

class ProvisionCollec tion
{
...

private int m_VarianceCount ;
public int VarianceCount
{
get { return m_VarianceCount ; }
}

public static ProvisionCollec tion GetProvisions(. ..)
{
ProvisionCollec tion list = new ProvisionCollec tion();

... populate collection ...

m_VarianceCount = (int)cmd.Parame ters["@SP_66_COU NT"].Value;
return list;
}
}

My question: Should this static method have access to modify the
private instance variable "m_VarianceCoun t"? Because right now it
works perfectly, but it doesn't seem like it should.
No, it shouldn't. That should give a compile time error.

Could you post a short but complete program we can compile showing the
above working?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 10 '07 #2
On May 10, 3:34 pm, Jon Skeet [C# MVP] <s...@pobox.com wrote:
Jordan Marr <jnm...@hotmail .comwrote:
I have the following class:
class ProvisionCollec tion
{
...
private int m_VarianceCount ;
public int VarianceCount
{
get { return m_VarianceCount ; }
}
public static ProvisionCollec tion GetProvisions(. ..)
{
ProvisionCollec tion list = new ProvisionCollec tion();
... populate collection ...
m_VarianceCount = (int)cmd.Parame ters["@SP_66_COU NT"].Value;
return list;
}
}
My question: Should this static method have access to modify the
private instance variable "m_VarianceCoun t"? Because right now it
works perfectly, but it doesn't seem like it should.

No, it shouldn't. That should give a compile time error.

Could you post a short but complete program we can compile showing the
above working?

Seehttp://www.pobox.com/~skeet/csharp/complete.htmlfo r details of
what I mean by that.

--
Jon Skeet - <s...@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- Hide quoted text -

- Show quoted text -
I had another app in C# 2.0 that did the same thing, however, some
classes would compile and some would throw a compile error. It was
the same scenerio.

Jordan

May 11 '07 #3
Could you post a short but complete program we can compile showing the
above working?

Create a new console app and paste the following two .cs modules:

using System;

namespace Static_Bug
{
public class TestClass
{
private string m_SecondName;
public string SecondName
{
get { return m_SecondName; }
}

private int m_NameCount;
public int NameCount
{
get { return m_NameCount; }
}

public static TestClass Load()
{
TestClass testClass = new TestClass();

string[] names = {"bob", "john", "sam" };

testClass.m_Sec ondName = names[1];
testClass.m_Nam eCount = names.Length;

return testClass;
}
}
}
using System;

namespace Static_Bug
{
public class Startup
{
public static void Main()
{
TestClass testClass = TestClass.Load( );

Console.WriteLi ne(string.Conca t("2nd Name:\t",
testClass.Secon dName));
Console.WriteLi ne(string.Conca t("Name Count:\t",
testClass.NameC ount));
Console.ReadLin e();
}
}
}

May 11 '07 #4
On May 11, 4:18 pm, Jordan Marr <jnm...@hotmail .comwrote:

<snip>
I had another app in C# 2.0 that did the same thing, however, some
classes would compile and some would throw a compile error. It was
the same scenerio.
I suspect you had something which looked like that on the surface, but
there was some difference somewhere which made it correct (from the
compiler's point of view).

Again, if you can provide full example code, we should be able to find
out what's going on.

Jon

May 11 '07 #5
On May 11, 11:36 am, Jordan Marr <jnm...@hotmail .comwrote:
Could you post a short but complete program we can compile showing the
above working?

Create a new console app and paste the following two .cs modules:

using System;

namespace Static_Bug
{
public class TestClass
{
private string m_SecondName;
public string SecondName
{
get { return m_SecondName; }
}

private int m_NameCount;
public int NameCount
{
get { return m_NameCount; }
}

public static TestClass Load()
{
TestClass testClass = new TestClass();

string[] names = {"bob", "john", "sam" };

testClass.m_Sec ondName = names[1];
testClass.m_Nam eCount = names.Length;

return testClass;
}
}

}

using System;

namespace Static_Bug
{
public class Startup
{
public static void Main()
{
TestClass testClass = TestClass.Load( );

Console.WriteLi ne(string.Conca t("2nd Name:\t",
testClass.Secon dName));
Console.WriteLi ne(string.Conca t("Name Count:\t",
testClass.NameC ount));
Console.ReadLin e();
}
}

}- Hide quoted text -

- Show quoted text -
I don't see what the problem is. Static doesn't mean you can't access
instance methods. You can as long as you have an instance to operate
on, which your code shows you do.

May 11 '07 #6
On May 11, 8:36 am, Jordan Marr <jnm...@hotmail .comwrote:
Could you post a short but complete program we can compile showing the
above working?

Create a new console app and paste the following two .cs modules:

using System;

namespace Static_Bug
{
public class TestClass
{
private string m_SecondName;
public string SecondName
{
get { return m_SecondName; }
}

private int m_NameCount;
public int NameCount
{
get { return m_NameCount; }
}

public static TestClass Load()
{
TestClass testClass = new TestClass();

string[] names = {"bob", "john", "sam" };

testClass.m_Sec ondName = names[1];
testClass.m_Nam eCount = names.Length;

return testClass;
}
}

}

using System;

namespace Static_Bug
{
public class Startup
{
public static void Main()
{
TestClass testClass = TestClass.Load( );

Console.WriteLi ne(string.Conca t("2nd Name:\t",
testClass.Secon dName));
Console.WriteLi ne(string.Conca t("Name Count:\t",
testClass.NameC ount));
Console.ReadLin e();
}
}

}
Ahh. This is a different situation from that which you originally
posted.

In your original post, your static method referred to the instance
field all by itself, without a instance of the class. Your new code
instantiates the class and then refers to the instance field of that
instance.

There's nothing wrong with this. It's the same as saying:

public static ArrayList GetList()
{
ArrayList list = new ArrayList();
list.Add(5);
}

"Add" is an instance method, but since you made an instance of
ArrayList, and you're accessing ITS Add method, the compiler has
everything it needs: a reference to an instance method (or field, or
property) and an instance to which it belongs.

Now, one thing that may cause a bit of confusion with your example is
the subject of access. You declared the fields "private", and yet
something logically "outside" the instance (the static method) is able
to get at them. "private" access means that the fields are private to
the class, and the static method is part of the class, so it can get
at them, just as if you pass one instance of a class to another
instance, the instance method that receives another of its own class
as a parameter can get at private fields, properties, methods, and
events of both its own instance ("this.") and the instance that was
passed to it (e.g. "otherInstance. ").

Similarly, a static method that is passed an instance of its
containing class, or instantiates its containing class (as your static
method does) can get at all of the private members of that instance,
since the static method also belongs to that same class.

May 11 '07 #7
On May 11, 4:36 pm, Jordan Marr <jnm...@hotmail .comwrote:
Could you post a short but complete program we can compile showing the
above working?

Create a new console app and paste the following two .cs modules:
<snip>

That doesn't show any bugs. Note that you're accessing the member
variables via a reference to an instance, which is fine - and is *not*
what your first post was showing.

Jon

May 11 '07 #8
On May 11, 11:44 am, Andy <a...@med-associates.comw rote:
On May 11, 11:36 am, Jordan Marr <jnm...@hotmail .comwrote:


Could you post a short but complete program we can compile showing the
above working?
Create a new console app and paste the following two .cs modules:
using System;
namespace Static_Bug
{
public class TestClass
{
private string m_SecondName;
public string SecondName
{
get { return m_SecondName; }
}
private int m_NameCount;
public int NameCount
{
get { return m_NameCount; }
}
public static TestClass Load()
{
TestClass testClass = new TestClass();
string[] names = {"bob", "john", "sam" };
testClass.m_Sec ondName = names[1];
testClass.m_Nam eCount = names.Length;
return testClass;
}
}
}
using System;
namespace Static_Bug
{
public class Startup
{
public static void Main()
{
TestClass testClass = TestClass.Load( );
Console.WriteLi ne(string.Conca t("2nd Name:\t",
testClass.Secon dName));
Console.WriteLi ne(string.Conca t("Name Count:\t",
testClass.NameC ount));
Console.ReadLin e();
}
}
}- Hide quoted text -
- Show quoted text -

I don't see what the problem is. Static doesn't mean you can't access
instance methods. You can as long as you have an instance to operate
on, which your code shows you do.- Hide quoted text -

- Show quoted text -
But I accessed a *private* member of the instance from a static (non
instanced) method. It just doesn't seem like that should work!!

Jordan

May 11 '07 #9
That doesn't show any bugs. Note that you're accessing the member
variables via a reference to an instance, which is fine - and is *not*
what your first post was showing.

And yet both examples compile and work perfectly.

Jordan

May 11 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3672
by: James | last post by:
Hello Java NG, I not sure if this is the right NG for this type of question but if not please let me know which is, TIA Any way first off let me say I'm a student and this WAS last weeks lab, turned in, graded and passed so I'm not trying to get someone to do my lab assignments, but after I got this back I was reading about the DecimalFormat and I tried to format my output but I keep getting an error message. can anyone please tell me...
9
2314
by: Tim Clacy | last post by:
Would some kind soul suggest a pre-processor test for the C++ language revision whereby class static variables were specified to refer to the same instance? Specifically, the following Singleton template will work with some compilers but not with older ones (because every module that includes the header gets its own unique static 'instance'): template<typename T> struct Singleton { static T& Instance() { static T instance; return...
115
7653
by: Mark Shelor | last post by:
I've encountered a troublesome inconsistency in the C-language Perl extension I've written for CPAN (Digest::SHA). The problem involves the use of a static array within a performance-critical transform function. When compiling under gcc on my big-endian PowerPC (Mac OS X), declaring this array as "static" DECREASES the transform throughput by around 5%. However, declaring it as "static" on gcc/Linux/Intel INCREASES the throughput by...
9
2311
by: Neil Kiser | last post by:
I'm trying to understand what defining a class as 'static' does for me. Here's an example, because maybe I am thinking about this all wrong: My app will allows the user to control the fonts that the app uses. So I will need to change the fonts depending on what settings the user has entered. However, it seems kind of wasteful to me to go to teh registry, fetch the font information and create new font objects for every form that I am...
18
3327
by: Frank Rizzo | last post by:
Hello, I have a class with all static methods that is called by multiple threads. I was wondering what effect that has on the competing threads. Does Thread2 have to wait until Thread1 is done with the StaticClass.Method1 before it can use it? What if I removed static methods and made all the threads instantiate its own copy of the class? Would that remove the waiting contention?
5
6279
by: | last post by:
Hi, How long do webservice objects live for? In particular, if i have static variables filled with data from a static constructor in a webservice, how long will that data persist? thxs
11
3846
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 experts. I would like to produce Javascript classes that can be "subclassed" with certain behaviors defined at subclass time. There are plenty of ways to do this through prototyping and other techniques, but these behaviors need to be static and...
16
8628
by: RB | last post by:
Hi clever people :-) I've noticed a lot of people stating not to use static variables with ASP.NET, and, as I understand it, the reason is because the variable is shared across user sessions - which is Very Bad (tm) for reasons I understand! However, does this rule apply only to global static variables, or does it apply to procedure-level static variables.
0
4060
by: Luis Zarrabeitia | last post by:
Quoting Joe Strout <joe@strout.net>: I'm sure your credentials are bigger than mine. But he is right. A lot of languages have ditched the "concept" of a static variable on a method (how do you parse that sentence, btw?) in favour of using encapsulation. Of the top of my head, there is Java, C# and Python, and I think even PHP and Perl. They call them "private variables", including the name-mangled-publicly-accessible-__python's...
0
9519
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10164
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9042
agi2029
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...
1
7538
isladogs
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...
0
6780
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5437
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...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
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
2
3723
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.