Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何为可为空的结构成员设置值_C#_Struct - Fatal编程技术网

C# 如何为可为空的结构成员设置值

C# 如何为可为空的结构成员设置值,c#,struct,C#,Struct,我有以下代码: public IfStatement? m_IfStatement; public struct IfStatement { public string Statement { get; private set; } public string Comparison { get; private set; } public string ConditionValue { get; private set; } public string IfCo

我有以下代码:

public IfStatement? m_IfStatement;

public struct IfStatement
{
    public string Statement { get; private set; }
    public string Comparison { get; private set; }
    public string ConditionValue { get; private set; }
    public string IfCondition_True { get; private set; }
    public string IfCondition_False { get; private set; }
}
当我试图像这样设置结构的值时:

m_IfStatement = new IfStatement();
m_IfStatement.Statement = cboIfStatement.SelectedItem.ToString();
m_IfStatement.Comparison = cboComparison.SelectedItem.ToString();
m_IfStatement.ConditionValue = txtIfValue.Text;
m_IfStatement.IfTrue = "";
m_IfStatement.IfFalse = "";
我从Compiler那里得到了这个错误:

'System.Nullable<Core.BaseControls.IntegrationTool.frmDataManipulation.IfStatement>' 
does not contain a definition for 'Statement' and no extension method 'Statement' 
accepting a first argument of type 
'System.Nullable<Core.BaseControls.IntegrationTool.frmDataManipulation.IfStatement>' 
could be found (are you missing a using directive or an assembly reference?)
“System.Nullable”
不包含“语句”的定义,也不包含扩展方法“语句”
接受类型为的第一个参数
“System.Nullable”
可以找到(是否缺少using指令或程序集引用?)
这是什么意思?我该如何解决这个问题。。。?请
这两个语句在同一范围内(例如,在同一类中)。

在第一节中,您有以下内容:

public string Statement { get; private set; }
第二个呢

m_IfStatement.Statement = cboIfStatement.SelectedItem.ToString();
您正在设置语句的第二部分,您在第一部分中定义了只能在结构本身中执行的语句(即,将其标记为private)

要解决此问题,只需将定义更改为:


Nullable
使用
Value
属性键入访问


由于结构是值类型(因此不能为null),并且您希望它可以为null,因此应该创建一个构造函数来设置属性。这样,您可以使用私有setter保留属性

public struct IfStatement {
    public IfStatement (string statement, string comparison, string conditionValue, string ifCondition_True, string ifCondition_False) {
        Statement = statement;
        Comparison = comparison;
        ConditionValue = conditionValue;
        IfCondition_True = ifCondition_True;
        IfCondition_False = ifCondition_False;
    }

    public string Statement { get; private set; }
    public string Comparison { get; private set; }
    public string ConditionValue { get; private set; }
    public string IfCondition_True { get; private set; }
    public string IfCondition_False { get; private set; }
}
像这样使用它

m_IfStatement = new IfStatement(
    cboIfStatement.SelectedItem.ToString(),
    cboComparison.SelectedItem.ToString()
    txtIfValue.Text,
    "",
    ""
);

这将防止设置可空结构的属性时出现任何问题。

您将设置保持为
private
,因为您将其定义为可空类型,所以必须使用
m_IfStatement.Value.Statement
和公共设置器。但为什么要使用nullable呢?因为一个结构不能是空的,我试过了,不是这样的。这两个语句在同一范围内。我也尝试了这一操作,但这又出现了另一个错误:无法修改“System.Nullable.value”的返回值,因为它不是variable@MuhammadUmar语句
ifStat=m_IfStatement.Value
创建对象的副本(因为它是一个结构),因此,保存到
ifStat
变量中的值仅在那里更改,而不是在原始对象中更改。
public struct IfStatement {
    public IfStatement (string statement, string comparison, string conditionValue, string ifCondition_True, string ifCondition_False) {
        Statement = statement;
        Comparison = comparison;
        ConditionValue = conditionValue;
        IfCondition_True = ifCondition_True;
        IfCondition_False = ifCondition_False;
    }

    public string Statement { get; private set; }
    public string Comparison { get; private set; }
    public string ConditionValue { get; private set; }
    public string IfCondition_True { get; private set; }
    public string IfCondition_False { get; private set; }
}
m_IfStatement = new IfStatement(
    cboIfStatement.SelectedItem.ToString(),
    cboComparison.SelectedItem.ToString()
    txtIfValue.Text,
    "",
    ""
);