问题是这样的,我定义了一个类如下class a
{
 public string name=null;//定义了一个符变量public void pro1()//函数1
{
string a="123456";
  name=a;
}public void pro2()//函数2
{
string b;  b=name;}}在函数2中得到b的值是null值,我现在想把函数1中的字符串a的值传给函数2中的字符串b
请问各位怎么能做到, 这个问题在C语言或是在C++中很容易就实现了,在但是C#让我郁闷了
半天了。其实我就是想问,在C#的两个函数中如何传送字符串!能什么方法?最好举个实例!解决就给分

解决方案 »

  1.   

    这个就是实际的 问题是这样的在一个类中有二个函数,
    public partial class aa : System.Web.UI.Page
    {
     string   icoName ; //问题就在这跟踪这个变量
     protected void Button1_Click(object sender, EventArgs e)
        {
            if (this.CustomValidator1.IsValid)
            {
               string fullName = this.File1.PostedFile.FileName;
               string type = fullName.Substring(fullName.LastIndexOf(".") + 1);
               icoName= DateTime.Now.ToFileTime() + "." + type; //问题就在这跟踪这个变量
               //icoName = "123";                //在这个函数内部能得到正确的                                文件名
                if (type == "jpg" || type == "gif" || type == "bmp")
                {
                    this.File1.PostedFile.SaveAs(Server.MapPath("upload") + "\\" + icoName);
                    this.Image1.ImageUrl = "upload" + "\\" + icoName;            }
                else
                {
                    Response.Write("<script lange=javascript>alert('您输入的图片格式不对,请输入,JPG 或是GIG 或是BMP格式的图片')</script>");            }        }    }protected void Button2_Click(object sender, EventArgs e)
        {
            if (this.IsValid)
            {
                          ////////////////////////////////////////
                client oneClient = new client();
                oneClient.useName = this.txtUserName.Text;
                oneClient.userPWD = this.txtUserID.Text;
                oneClient.clientName = this.txtClientName.Text;
                oneClient.clientSpecies = this.ddlClientSpecies.SelectedValue; ;
                oneClient.clientCharactar = this.rblClientCharactar.SelectedItem.Value;
                oneClient.clientPicName = icoName;    //问题就在这跟踪这个变量
      
                              //在这个函数里面我又用到了
                                                        //icoName,但是在这个函数里却变成                          了
                              NULL了
    问题是我怎么能把上面的函中的字符串传送到这个函数里面?
    解决就给分
      

  2.   

    因为你点button2的时候,页面重新刷新了,icoName置空
    把icoName定义成静态变量试试
    public static string icoName;
      

  3.   

    可以把你要的对象放入Session里,页面刷新后再取
      

  4.   

    将icoName的值先保存到ViewState中,在函数“protected void Button2_Click(object sender, EventArgs e)”中再将其取出看看。
      

  5.   

    string   icoName ; //问题就在这跟踪这个变量换成下面的代码private string icoName{
    get{
    object obj=ViewState["icoName"];
    return (obj==null)?"":(string)obj;
    }
    set{
    ViewState["icoName"]=value;
    }
    }
      

  6.   

    如果页面有向服务器提交
    页面中的变量全部置为初始值
    所以它就是null了
    把它保存到viewstate或session中就可以了