在方法之间传递字符串 [英] passing a string between methods

查看:93
本文介绍了在方法之间传递字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我想知道我是否可以得到一些建议...
我快要完成专项工作了,但是在解决错误方面遇到了一些问题

基本上,我需要通过,修改和返回来自文本框的字符串输入.

使用stringbuilder时,字符串的每个其他字母都包含字符"x"(即abc变成axbxcx).然后,新字符串将返回到sendtext,并在其中写入串行端口.

使用文本框进行测试,在"txt_ammend"方法中尝试时,新字符串可以正确打印,但是仅在"sendtext"中打印字符串中的最后一个字符.

任何建议都很好
干杯,
M


Hey guys, I was wondering if i could get a little advice...
I''m almost at the end of a hench project but i''m having some problems getting the bugs out

basically I need to pass, ammend and return a string input from a textbox.

Using stringbuilder the character ''x'' is included every other letter of the string (ie abc turns into axbxcx). The new string is then returned to sendtext where it is written to a serial port.

using textboxes to test, the new string is printed correctly when tried in the "txt_ammend" method, but only prints the last character in the string within "sendtext"

Any suggestions would be great
Cheers,
M


//***************sends string trom txt box to serial
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == 13)
    {
        string ErrorMessage = Sendtext(textBox1.Text);
    }
}

public string Sendtext(string ToSend)
{
    txt_ammend();   //calls method                               
    try
    {
        foreach (char sb in ToSend)
        {
            serialPort1.WriteLine(sb.ToString());
            Thread.Sleep(950);
            textBox2.Text = sb.ToString();  //this only displays last 
                                            //char of string
        } 
    }
    finally
    {
        serialPort1.Write("x"); 
    }
    return "OK";
}

//**************************stringbulider method
string txt_ammend()            
{
    StringBuilder sb = new StringBuilder();
    foreach (char c in textBox1.Text)
    {
        sb.Append(c);
        textBox3.Text = sb.ToString();   //this shows correctly
                                         //ammended string
        if (!char.IsWhiteSpace(c))       //ie axbxcxdx etc
        {
            sb.Append("x");
            //Thread.Sleep(500);
        }
    }
    return sb.ToString();
}

推荐答案

好吧,您正在调用txt_ammend,但是您没有将返回的值接收到变量中.您可能应该这样做:

Well, You''re calling txt_ammend, but you''re not receiving the returned value into a variable. You should probably do this:

string myText = txt_ammend();



然后使用myText.

您的代码中还存在其他不一致之处,但这应该可以使您到达要执行的操作.



And then work with the myText.

There are other inconsistencies in your code, but this should get you to where you want to do.


这是因为在运行该代码时会阻塞UI线程.因此,对文本框的更改只有在循环完成后才会显示在屏幕上,届时它将仅显示您将其设置为的最后一个字符.

使用后台线程进行串行端口写入,或将文本附加到文本框,而不是在每次循环迭代期间覆盖它.


------

由于此答案受到质疑,因此我将添加更多文本,以便OP理解我在说什么.您在这里有此循环:

That''s because you block the UI thread when you run that code. So your changes to the textbox won''t render on screen until the loop is done with by which time it will only show the last character you set it to.

Use a background thread for the serialport write, or append the text to the textbox instead of overwriting it during each loop iteration.


------

Since this answer has been challenged, I''ll add more text so the OP understands what I am talking about. You have this loop here:

foreach (char sb in ToSend)
{
    serialPort1.WriteLine(sb.ToString());
    Thread.Sleep(950);
    textBox2.Text = sb.ToString();
}



我假设从那里的Sleep通话中,您希望看到在文本框中出现的每个字符.这不会发生,因为Sleep会阻塞当前线程,在您的情况下就是UI线程.因此,除非那是在后台线程中,否则您将看不到字符一个接一个地闪烁.

因此,替代方法是更改​​此行:



I assume from the Sleep call you have there, that you expect to see character by character appearing in your textbox. This wont happen because Sleep will block the current thread, which in your case is the UI thread. So unless that is in a background thread you will not see the characters flashing by one after the other.

So the alternative is to change this line:

textBox2.Text = sb.ToString();



至:



to:

textBox2.Text += sb.ToString();




现在,在循环结束时,您将看到整个字符集.

顺便说一句,BTW也阅读了John的回复,他解决了您代码中的另一个错误.




Now at the end of the loop, you will see the entire set of characters.

BTW read John''s reply too, he addresses a different bug in your code.


这篇关于在方法之间传递字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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