473,324 Members | 2,370 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,324 software developers and data experts.

static & instance variables

I have the following class:

class ProvisionCollection
{
...

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

public static ProvisionCollection GetProvisions(...)
{
ProvisionCollection list = new ProvisionCollection();

... populate collection ...

m_VarianceCount = (int)cmd.Parameters["@SP_66_COUNT"].Value;
return list;
}
}

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

Thanks,
Jordan

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

class ProvisionCollection
{
...

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

public static ProvisionCollection GetProvisions(...)
{
ProvisionCollection list = new ProvisionCollection();

... populate collection ...

m_VarianceCount = (int)cmd.Parameters["@SP_66_COUNT"].Value;
return list;
}
}

My question: Should this static method have access to modify the
private instance variable "m_VarianceCount"? 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.com>
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.comwrote:
Jordan Marr <jnm...@hotmail.comwrote:
I have the following class:
class ProvisionCollection
{
...
private int m_VarianceCount;
public int VarianceCount
{
get { return m_VarianceCount; }
}
public static ProvisionCollection GetProvisions(...)
{
ProvisionCollection list = new ProvisionCollection();
... populate collection ...
m_VarianceCount = (int)cmd.Parameters["@SP_66_COUNT"].Value;
return list;
}
}
My question: Should this static method have access to modify the
private instance variable "m_VarianceCount"? 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.htmlfor 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_SecondName = names[1];
testClass.m_NameCount = names.Length;

return testClass;
}
}
}
using System;

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

Console.WriteLine(string.Concat("2nd Name:\t",
testClass.SecondName));
Console.WriteLine(string.Concat("Name Count:\t",
testClass.NameCount));
Console.ReadLine();
}
}
}

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_SecondName = names[1];
testClass.m_NameCount = names.Length;

return testClass;
}
}

}

using System;

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

Console.WriteLine(string.Concat("2nd Name:\t",
testClass.SecondName));
Console.WriteLine(string.Concat("Name Count:\t",
testClass.NameCount));
Console.ReadLine();
}
}

}- 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_SecondName = names[1];
testClass.m_NameCount = names.Length;

return testClass;
}
}

}

using System;

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

Console.WriteLine(string.Concat("2nd Name:\t",
testClass.SecondName));
Console.WriteLine(string.Concat("Name Count:\t",
testClass.NameCount));
Console.ReadLine();
}
}

}
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.comwrote:
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_SecondName = names[1];
testClass.m_NameCount = names.Length;
return testClass;
}
}
}
using System;
namespace Static_Bug
{
public class Startup
{
public static void Main()
{
TestClass testClass = TestClass.Load();
Console.WriteLine(string.Concat("2nd Name:\t",
testClass.SecondName));
Console.WriteLine(string.Concat("Name Count:\t",
testClass.NameCount));
Console.ReadLine();
}
}
}- 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
On May 11, 4:57 pm, Jordan Marr <jnm...@hotmail.comwrote:
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 -

But I accessed a *private* member of the instance from a static (non
instanced) method. It just doesn't seem like that should work!!
No, that's fine. From within a type, you can access private members of
any instance of the same type.

Jon

May 11 '07 #11
On May 11, 11:55 am, "Jon Skeet [C# MVP]" <s...@pobox.comwrote:
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
I see what you are saying now. I did indeed misrepresent the
problem. My code that compiled is calling the private member from an
actual instance.

I didn't realize that was valid to reference a private member
"externally" like that (even though the static member is declared
internally). It is convenient that I am able to do this though!

Does "shared" work the same way in VB?

Jordan

May 11 '07 #12
Jordan Marr <jn****@hotmail.comwrote:

<snip>
I see what you are saying now. I did indeed misrepresent the
problem. My code that compiled is calling the private member from an
actual instance.

I didn't realize that was valid to reference a private member
"externally" like that (even though the static member is declared
internally). It is convenient that I am able to do this though!

Does "shared" work the same way in VB?
Yes, but it's really irrelevant to whether the method is static or not.
It would be the same situation if you try to access private members of
one instance from another. Effectively, the fact that it's a static
method is irrelevant except that there's no "this" to refer to.

--
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
May 11 '07 #13
"Jordan Marr" <jn****@hotmail.comschrieb im Newsbeitrag
news:11*********************@e65g2000hsc.googlegro ups.com...
>
But I accessed a *private* member of the instance from a static (non
instanced) method. It just doesn't seem like that should work!!
You seem to suppose private restricts the access to member to methods of the
same *instance*.
But it only limits acces to the same *class* not to the same *instance*.

Christof

May 14 '07 #14
On May 14, 3:05 am, "Christof Nordiek" <c...@nospam.dewrote:
"Jordan Marr" <jnm...@hotmail.comschrieb im Newsbeitragnews:11*********************@e65g2000hs c.googlegroups.com...
But I accessed a *private* member of the instance from a static (non
instanced) method. It just doesn't seem like that should work!!

You seem to suppose private restricts the access to member to methods of the
same *instance*.
But it only limits acces to the same *class* not to the same *instance*.

Christof
You're right, that is exactly what I was supposing. But I got it
now.
Thanks!

Jordan

May 14 '07 #15

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

Similar topics

1
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,...
9
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...
115
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...
9
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...
18
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...
5
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
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...
16
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 -...
0
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.