Chinaunix首页 | 论坛 | 博客
  • 博客访问: 12471082
  • 博文数量: 1293
  • 博客积分: 13501
  • 博客等级: 上将
  • 技术积分: 17974
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-08 18:11
文章分类

全部博文(1293)

文章存档

2019年(1)

2018年(1)

2016年(118)

2015年(257)

2014年(128)

2013年(222)

2012年(229)

2011年(337)

分类: 嵌入式

2012-09-22 10:46:40

1、出错代码及现场


  1. class Program
  2. {
  3. int i = 123;
  4. static string s = "Some string";
  5. static object o = s;


  6. static void Main(string[] args)
  7. {
  8.             Test_Exception_Function();
  9. Console.WriteLine("in Main,After Excute Test_Exception_Function........!");
  10. Console.ReadLine();
  11. }


  12. static void Test_Exception_Function()
  13. {

  14. s = “in Test_Exception_Function Function”;

  15. i = 1000;




 

image

 

在一个函数中,调用不是static的字段时,就会爆出“非静态的字段、方法或属性“Finally_test.Program.i”要求对象引用”这样的错误。   

什么原因?


2、出现上面错误原因分析


类中静态的方法、成员函数只能访问静态的数据成员或者静态的方法。 
static void Main(string[] args) //这使用了关键字static代表是静态方法,如果Main方法里面要调用外面的方法或者函数必须是静态的方法或者是函数。 
C#中就连static void Main(string[] args)要访问这个方法外面的变量都得是静态的。


int i = 123;

必须改为

static int i = 123;
阅读(11835) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~