静态&实例变量 [英] static & instance variables

查看:76
本文介绍了静态&实例变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下课程:


class ProvisionCollection

{

...

private int m_VarianceCount;

public int VarianceCount

{

get {return m_VarianceCount; }

}


public static ProvisionCollection GetProvisions(...)

{

ProvisionCollection list = new ProvisionCollection();


...填充集合...


m_VarianceCount =(int)cmd.Parameters [" @ SP_66_COUNT]。值;

返回列表;

}

}


我的问题:这种静态方法是否有权修改

私有实例变量m_VarianceCount?因为现在它

完美无缺,但它似乎不应该。


谢谢,

Jordan

解决方案

Jordan Marr< jn **** @ hotmail.comwrote:


我有以下课程:


class ProvisionCollection

{

...


private int m_VarianceCount;

public int VarianceCount

{

get {return m_VarianceCount; }

}


public static ProvisionCollection GetProvisions(...)

{

ProvisionCollection list = new ProvisionCollection();


...填充集合...


m_VarianceCount =(int)cmd.Parameters [" @ SP_66_COUNT]。值;

返回列表;

}

}


我的问题:这种静态方法是否有权修改

私有实例变量m_VarianceCount?因为现在它

完美无缺,但它似乎不应该。



不,它不应该。这应该会给编译时错误。


你能发一个简短但完整的程序我们可以编译显示

以上工作吗?


请参阅 http://www.pobox。 com / ~siget / csharp / complete.html 了解详情

我的意思是什么。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet 博客: http: //www.msmvps.com/jon.skeet

如果回复该群组,请不要给我发邮件


5月10日下午3:34,Jon Skeet [C#MVP]< s ... @ pobox.comwrote:


Jordan Marr< jnm ... @ hotmail.comwrote:


我有以下课程:


class ProvisionCollection

{

...


private int m_VarianceCount;

public int VarianceCount

{

get {return m_VarianceCount; } $ / $
}


public static ProvisionCollection GetProvisions(...)

{

ProvisionCollection list = new ProvisionCollection();


...填充集合...


m_VarianceCount = (int)cmd.Parameters [" @ SP_66_COUNT"]。值;

返回列表;

}

}


我的问题:这个静态方法是否有权修改

私有实例变量m_VarianceCount?因为现在它

完美无缺,但它似乎不应该。



不,它不应该。这应该会给编译时错误。


你能发一个简短但完整的程序我们可以编译显示

以上工作吗?


Seehttp://www.pobox.com/~skeet/csharp/complete.html了解详情

我的意思是什么。


-

Jon Skeet - < s ... @ pobox.com> http://www.pobox.com/~skeet 博客: http://www.msmvps.com/jon.skeet

如果回复群组,请不要给我发邮件 - 隐藏引用的文字 -


- 显示引用的文字 -



我在C#2.0中有另一个应用程序执行相同的操作但是,有些

类会编译,有些会抛出编译错误。同样的场景是




乔治


你能发布一个简短但完整的程序,我们可以编译显示


以上工作?



创建一个新的控制台应用并粘贴以下两个.cs模块:


使用系统;


名称空间Static_Bug

{

公共类TestClass

{

private string m_SecondName;

公共字符串SecondName

{

get {return m_SecondName; }

}


private int m_NameCount;

public int NameCount

{

get {return m_NameCount; } $ / b
}

public static TestClass Load()

{

TestClass testClass = new TestClass ();


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


testClass.m_SecondName = names [1];

testClass.m_NameCount = names.Length;


返回testClass;

}

}

}

使用系统;


命名空间Static_Bug

{

公共类启动

{

public static void Main()< br $>
{

TestClass testClass = TestClass.Load();

Console.WriteLine(string.Concat(" 2nd Name: \t",

testClass.SecondName));

Console.WriteLine(string.Concat(" Name Count:\t",

testClass.NameCount));

Console.ReadLine();

}

}

}


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

解决方案

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


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


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


这篇关于静态&amp;实例变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆