static & instance variables

J

Jordan Marr

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
 
J

Jon Skeet [C# MVP]

Jordan Marr said:
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.
 
J

Jordan Marr

Jordan Marr said:
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 - <[email protected]>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
 
J

Jordan Marr

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();
}
}
}
 
J

Jon Skeet [C# MVP]

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
 
A

Andy

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.
 
B

Bruce Wood

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.
 
J

Jon Skeet [C# MVP]

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
 
J

Jordan Marr

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
 
J

Jordan Marr

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
 
J

Jon Skeet [C# MVP]

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
 
J

Jordan Marr

<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
 
J

Jon Skeet [C# MVP]

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.
 
C

Christof Nordiek

Jordan Marr said:
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
 
J

Jordan Marr

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top