关于static,ref,值传递的问题请教

 

我写了一个类TextParser类,

里面有2个方法,

一个是public string[] GetMatchedText(string textcontext),它用于从文档中读取文本内容textcontext,从中获取匹配的字符串,返回由这些字符串组成的数组。

另一个是public void ParserAndReplaceText(string[] matches,ref string textcontext),它用于分析从上面那个方法返回的字符串数组,看是否符合要求,然后在原来的文本textcontext中把符合的字符串替换掉为AAA(另外,ParserAndReplaceText还递归调用了自己,就是字符串中的数组如果符合,就执行ParserAndReplaceText(GetMatchedText(字符串中的数组), ref string textcontext)直到不符合)

我又写了一个TextCommonProcesser类,它封装了对每一个文档的一些公开处理和文档的属性,如文档的文本内容textcontext,字符编码等。

然后我在一个.aspx中,创建了一个TextCommonProcesser的实例,再创建了一个TextParser的实例,

public class WebForm1 : System.Web.UI.Page

     {

         protected System.Web.UI.WebControls.Button Button1;

         protected System.Web.UI.WebControls.Button Button2;

         protected System.Web.UI.WebControls.TextBox TextBox;

 

         protected TextCommonProcesser tcp;

         protected TextParser tp=new TextParser();

    

         private void Page_Load(object sender, System.EventArgs e)

         {

         }

 

         private void Button1_Click(object sender, System.EventArgs e)

         {

              tcp=new TextCommonProcesser();

              this.TextBox.Text=tcp.TextContext;

         }

 

         private void Button2_Click(object sender, System.EventArgs e)

         {

              tp.ParserAndReplaceText(GetMatchedText(tcp.TextContext), ref string tcp.TextContext)

              this.TextBox.Text=tcp.TextContext;

         }

     }

可点完button1textbox取得了tcp.TextContext的值,

但再点button2后,textbox的值无变化。

我之前把TextParser类和TextCommonProcesser类写在一个类里,方法都是static时是OK的。但写成2个类就不行了,小弟基础不好,请各位高手指教,谢谢。

原文地址:https://www.cnblogs.com/bankey/p/844919.html