1

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).

2
  • 2
    you have kept set as private Commented Apr 22, 2014 at 11:41
  • Since you defined it as a nullable type, you have to use m_IfStatement.Value.Statement and also a public setter. But why use nullable anyway? Since a struct cannot be null. Commented Apr 22, 2014 at 11:46

3 Answers 3

1

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 = ""; 
Sign up to request clarification or add additional context in comments.

2 Comments

I tried this also, and this get a nother error: Cannot modify the return value of 'System.Nullable<Core.BaseControls.IntegrationTool.frmDataManipulation.IfStatement>.Value' because it is not a variable
@MuhammadUmar The statement ifStat = m_IfStatement.Value creates a copy of the object (since it is a struct), so the values saved into your ifStat variable are only changed there, not in the original object.
0

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.

Comments

0

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

1 Comment

I tried it, it's not that. Both statements are in the same scope

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.