using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        
                public Form1()
        {
            InitializeComponent();
            Jis aaa = new Jis() ;
        }        private void button1_Click(object sender, EventArgs e)
        {
            int bbb = aaa.yunsuan(3);
    
        }
    }
}
public class Jis
{
    protected int textsc = 0;
    public int  yunsuan( int a )
    {
        textsc += a;
        return textsc;
        }
}这样的代码的结果是 “错误 1 当前上下文中不存在名称“aaa"     
我应该怎样写才能让其他过程可以访问呢?

解决方案 »

  1.   

    把aaa设计成类成员就可以了。namespace   WindowsApplication1 

            public   partial   class   Form1   :   Form 
            { 
                    private Jis aaa;
                    
                    public   Form1() 
                    { 
                            InitializeComponent(); 
                            this.aaa   =   new   Jis(); 
                    }                 private   void   button1_Click(object   sender,   EventArgs   e) 
                    { 
                            int   bbb   =   this.aaa.yunsuan(3); 
            
                    } 
            } 
    } 或者把yunsuan方法设计为Static,这样就不需要New了。
    一般把工具方法设计为Static:namespace WindowsApplication1 

            public partial class Form1:Form 
            {                 
                    public Form1() 
                    { 
                        InitializeComponent(); 
                        // Jis   aaa   =   new   Jis()   ; 
                    }                 private void button1_Click(object sender, EventArgs e) 
                    { 
                            int   bbb   =   Jis.yunsuan(3); 
                    } 
            } 

    public   class   Jis 

            protected static int   textsc   =   0; 
            public  static int     yunsuan(   int   a   ) 
            { 
                    textsc   +=   a; 
                    return   textsc; 
            } 

      

  2.   

    楼主是相当的头大?
    我来救你,http://tech.163.com/special/000915SN/LanguageC.html看完这个你就不迷糊了,特别是第 15 讲,就是你问的问题。