By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,963 Members | 1,749 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,963 IT Pros & Developers. It's quick & easy.

static & instance variables

P: n/a
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
Share this Question
Share on Google+
14 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.