【问题标题】:C# Return string value from another class to null valueC#将另一个类的字符串值返回为空值
【发布时间】:2013-09-13 06:40:05
【问题描述】:

例子:

public partial class abc1 : Form
{
       public string str__subgrp { get; set; }
       public string str__type { get; set; }
       public string return_str__type { get; set; 

       public abc1()
       {
          InitializeComponent();
       }
       private void savebtn_click(object sender, EventArge e )
       {
            this.str__subgrp = "ABC";
            this.str__type = "123";
            this.return_str__type = "This word is unicode";
       }
}

public partial class worknow : Form
{
       // ... declare variable in class ....

       public worknow()
       {
          InitializeComponent();
       }

       private void showformbtn_click(object sender, EventArge e )
       {
            string a,b,c;
            using ( var abc1_frm = new abc1())
            {
                 abc1_frm.ShowDialog();
                 a = abc1_frm.str__subgrp;
                 b = abc1_frm.str__type;
                 c = abc1_frm.return_str__type;  // This variable = null

            }
       }
}

在注释行中(这个变量=null)

为什么变量return_str__type会返回null的值?

【问题讨论】:

  • 你错过了} ?? public string return_str__type { get; set;
  • 这不会编译,因为您在第五行缺少}
  • 对不起小姐 } return_str__type = "ภาษาไทย" //

标签: c# string null return


【解决方案1】:

我遇到了类似的问题。它与范围有关。尝试将 return_str__type 声明为 public static 类型,看看它是否有效。并不总是最佳做法,但可能会奏效。

【讨论】:

    【解决方案2】:

    return_str__type 属性的分配发生在 savebtn_click 函数中。 你为什么期望这个函数调用? 尝试将savebtn 按钮设置为AcceptButtonabc1 from,或者找到一种方法如何将savebtn_click 定义为表单的默认事件。 请用你的风格代码做点什么,我的眼睛在流血:)

    【讨论】:

    • 2 类中的变量名不是同一个字符串。我不知道这个错误。而且 C# 编译器没有错误显示。
    【解决方案3】:

    如果您确保在关闭对话框之前执行savebtn_click,您只能期望abc1_frm.return_str__type 具有不同于null 的值。由于您没有确切地告诉我们用户(或您作为测试人员)如何关闭对话框,并且如果您禁用了关闭按钮 [X],我能给您的唯一建议是在该方法中的调试器中设置断点并检查代码是否被执行。

    【讨论】:

    • 是的,我非常确定。并在关闭表单之前调试我的程序。其他变量可以返回值。
    • 然后再次检查 - 更彻底。或者编辑您的问题并提供更详细的问题描述。
    • 我的完整变量名:return_plantbl_beneftextforuser // 它返回空值。我测试将变量名更改为其他:return_plantbl_beneftextforuser_eng // C# 可以返回值
    • 我使用 2 个变量来保持值:return_plantbl_beneftextforuser 保留我的默认语言。 return_plantbl_beneftextforuser_eng 保留英文值。为什么 return_plantbl_beneftextforuser 返回空值?
    • 2个类中的变量名不是同一个字符串。我不知道这个错误。并且C#编译器没有错误显示。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-04
    相关资源
    最近更新 更多