Liran Friedman
Liran Friedman

Reputation: 4287

How to set values to nullable struct members

I have this code:

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; }
}

and when I'm trying to set values to the struct like this:

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 = "";

I'm getting this error by the compailer:

'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?)

What does it mean ? and how do I solve this...? please. Both statements are in the same scope (e.g. in the same class).

Upvotes: 1

Views: 433

Answers (3)

Maarten
Maarten

Reputation: 22945

Since structs are value-types (and therefore cannot be null) and you want it be nullable, you should create a constructor to set the properties. This way you can keep your properties with private setters.

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; }
}

And use it like

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

This will prevent any trouble you have setting a property of a nullable struct.

Upvotes: 0

Muhammad Umar
Muhammad Umar

Reputation: 3781

Nullable types access with Value property. Nullable Types

 public IfStatement? m_IfStatement;

    public struct IfStatement
    {
        public string Statement { get; set; }
        public string Comparison { get; set; }
        public string ConditionValue { get; set; }
        public string IfCondition_True { get; set; }
        public string IfCondition_False { get; set; }
    }

        m_IfStatement = new IfStatement();
        IfStatement ifStat = m_IfStatement.Value;
        ifStat.Statement = cboIfStatement.SelectedItem.ToString();
        ifStat.Comparison = cboComparison.SelectedItem.ToString();
        ifStat.ConditionValue = txtIfValue.Text;
        ifStat.TrueCondition = "";
        ifStat.FalseCondition = "";

Upvotes: 1

dav_i
dav_i

Reputation: 28107

In your first section you have this:

public string Statement { get; private set; }

and your second

m_IfStatement.Statement = cboIfStatement.SelectedItem.ToString();

The second section you are setting Statement, which you have defined in your first that you can only do within the struct itself (i.e. marking it as private).

To fix this, simply change your definition(s) to read:

public string Statement { get; set; }

Upvotes: 0

Related Questions