C#中我的字符串位于代码块中,可以在代码块外部读取字符串。

huangapple go评论50阅读模式
英文:

C# my string is in block of code, can read the string outside of the block of code

问题

我有一个名为mydata的字符串,在一个公共的void中。如下所示:

private void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
    string mydata = ((SerialPort)sender).ReadExisting();
}

在这段代码之后,我有一个按钮的代码,里面包含一个if语句。如下所示:

private void button1_Click(object sender, EventArgs e)
{
    if (mydata == "Random")
    {
        //执行函数
    }
}

包含mydata的if语句会出现一个错误:"The name mydata does not exist in the current context."(mydata的名称在当前上下文中不存在)。

我的问题是,我能否将private更改为public,然后从mydata字符串中获取数据?这样该如何做!

我尝试过将private更改为public,以为我可以检索到mydata字符串,但这并没有起作用。
英文:

I have a string name called mydata in a public void. Like so:

        private void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {

            string mydata = ((SerialPort)sender).ReadExisting();
        }

after this code I have a button code with an if statment. Like so:

        private void button1_Click(object sender, EventArgs e)
        {
            if (mydata == "Random")
            {
                //DO Function
            }
        }

The if statement which includes mydata gives an error "The name mydata does not exist inthe current context.

My question is can I change the private void to public and then i can pull the data from mydata string? How is this done!

I have tried changing private to public thinking i can retrieve mydata string but that did not work.

答案1

得分: 4

访问修饰符(private等)并不是真正的问题。这涉及到变量的作用域。通常情况下,当一个方法需要向另一个方法“发送一个值”或“接收一个值”时,我们会谈论方法的参数和返回值。

然而,您所拥有的是事件处理程序,它们不会直接调用彼此。因此,传递值和返回值实际上不是一个选项。

数据需要存储在两者都可以看到的公共位置。例如,如果这两者都在同一个类中,并且在这里使用了相同的类实例,那么您可以将该值存储在类级别的字段中。例如:

private string mydata;

private void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
    mydata = ((SerialPort)sender).ReadExisting();
}

private void button1_Click(object sender, EventArgs e)
{
    if (mydata == "Random")
    {
        //执行功能
    }
}

请注意,如果类的不同实例需要共享此数据(一个常见的示例是ASP.NET在不同请求上的页面),那么您需要将该值的作用域提升得更高,将其保留到类本身之外的某些存储位置,例如数据库。

英文:

Access modifiers (private, et al) aren't really the issue here. It's a matter of variable scope. Generally when a method needs to "send a value" or "receive a value" to/from another method, we'd be talking about method arguments and return values.

However, what you have are event handlers, which don't directly call one another. So passing values and returning values aren't really an option.

The data would need to be stored in a common location both can see. For example, if both of these are in the same class, and if the same instance of that class is being used here, then you can store the value in a class-level field. For example:

private string mydata;

private void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
    mydata = ((SerialPort)sender).ReadExisting();
}

private void button1_Click(object sender, EventArgs e)
{
    if (mydata == "Random")
    {
        //DO Function
    }
}

Note that if different instances of the class need to share this data (a common example that trips up beginners is ASP.NET pages on different requests) then you'd need to raise the scope of the value even more, persisting it to some storage outside of the class itself. A database, for example.

huangapple
  • 本文由 发表于 2023年2月24日 02:30:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75548916.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定