Assigning Values to Nested Class Members

I am new to C#. I am wondering how to instantiate an object with nested class members:
c#
namespace LearnosityCookbookRecipe
{
class Question
{
public string Reference
{ get; set; }
public string WidgetType
{ get; set; }
public string Type
{ get; set; }
public struct QuestionData
{
public Array Options
{ get; set; }
public string Stimulus
{ get; set; }
struct QuestionValidation
{
string ScoringType
{ get; set; }
struct valid_response
{
public int Score
{ get; set; }
public Array Value
{ get; set; }
}

}

}
}
}
c#
namespace LearnosityCookbookRecipe
{
class Question
{
public string Reference
{ get; set; }
public string WidgetType
{ get; set; }
public string Type
{ get; set; }
public struct QuestionData
{
public Array Options
{ get; set; }
public string Stimulus
{ get; set; }
struct QuestionValidation
{
string ScoringType
{ get; set; }
struct valid_response
{
public int Score
{ get; set; }
public Array Value
{ get; set; }
}

}

}
}
}
I am trying to figure out how to assign the values to the QuestionValidation Stuct and ScoringType
c#
Question question = new Question();
question.Reference = "SAMPLE-REF-ID1";
question.WidgetType = "response";
question.Type = "mcq";
c#
Question question = new Question();
question.Reference = "SAMPLE-REF-ID1";
question.WidgetType = "response";
question.Type = "mcq";
3 Replies
Gramore
Gramore6mo ago
You haven't defined a field on Question with the type of Question.QuestionValidation, you've just defined the struct type as a nested type Question.QuestionValidation so there's nothing to assign the values to Also you should probably format your code (indentation etc.) So you've got something like :
namespace LearnosityCookbookRecipe
{
class Question
{
public string Reference { get; set; }
public string WidgetType { get; set; }
public string Type { get; set; }
public struct QuestionData
{
public object[] Options {get;set;}
public string Stimulus { get; set; }
public struct QuestionValidation
{
string ScoringType { get; set; }
public struct valid_response
{
public int Score { get; set; }
public object[] Value { get; set; }
}

}
}
}
}
namespace LearnosityCookbookRecipe
{
class Question
{
public string Reference { get; set; }
public string WidgetType { get; set; }
public string Type { get; set; }
public struct QuestionData
{
public object[] Options {get;set;}
public string Stimulus { get; set; }
public struct QuestionValidation
{
string ScoringType { get; set; }
public struct valid_response
{
public int Score { get; set; }
public object[] Value { get; set; }
}

}
}
}
}
Where object[] replaces your Array type just for the example So a literal translation of what you want is probably:
namespace LearnosityCookbookRecipe
{
class Question
{
public string Reference { get; set; }
public string WidgetType { get; set; }
public string Type { get; set; }
+ public Question.QuestionData Data { get; set; }
public struct QuestionData
{
public object[] Options {get;set;}
public string Stimulus { get; set; }
+ public Question.QuestionData.QuestionValidation Validation { get; set; }
public struct QuestionValidation
{
string ScoringType { get; set; }
+ public Question.QuestionData.QuestionValidation.valid_response ValidResponse { get; set; }
public struct valid_response
{
public int Score { get; set; }
public object[] Value { get; set; }
}

}
}
}
}
namespace LearnosityCookbookRecipe
{
class Question
{
public string Reference { get; set; }
public string WidgetType { get; set; }
public string Type { get; set; }
+ public Question.QuestionData Data { get; set; }
public struct QuestionData
{
public object[] Options {get;set;}
public string Stimulus { get; set; }
+ public Question.QuestionData.QuestionValidation Validation { get; set; }
public struct QuestionValidation
{
string ScoringType { get; set; }
+ public Question.QuestionData.QuestionValidation.valid_response ValidResponse { get; set; }
public struct valid_response
{
public int Score { get; set; }
public object[] Value { get; set; }
}

}
}
}
}
But you probably don't actually want to nest the structs since it just makes them harder to work with So you'd be able to write:
Question question = new Question();
question.Reference = "SAMPLE-REF-ID1";
question.WidgetType = "response";
question.Type = "mcq";
var data = new Question.QuestionData();
//etc
question.Data = data
Question question = new Question();
question.Reference = "SAMPLE-REF-ID1";
question.WidgetType = "response";
question.Type = "mcq";
var data = new Question.QuestionData();
//etc
question.Data = data
Or using initializer syntax:
Question question = new Question(){
Reference = "SAMPLE-REF-ID1",
WidgetType = "response",
Type = "mcq",
Data = new Question.QuestionData(){
// etc.
}
)
Question question = new Question(){
Reference = "SAMPLE-REF-ID1",
WidgetType = "response",
Type = "mcq",
Data = new Question.QuestionData(){
// etc.
}
)
The alternative here being something like:
namespace LearnosityCookbookRecipe
{
class Question
{
public string Reference { get; set; }
public string WidgetType { get; set; }
public string Type { get; set; }
public QuestionData Data { get; set; }
}

public struct QuestionData
{
public object[] Options {get;set;}
public string Stimulus { get; set; }
public QuestionValidation Validation { get; set; }
}

public struct QuestionValidation
{
string ScoringType { get; set; }
public ValidResponse ValidResponse { get; set; }
}

// Renamed:
public struct ValidResponse
{
public int Score { get; set; }
public object[] Value { get; set; }
}
}
namespace LearnosityCookbookRecipe
{
class Question
{
public string Reference { get; set; }
public string WidgetType { get; set; }
public string Type { get; set; }
public QuestionData Data { get; set; }
}

public struct QuestionData
{
public object[] Options {get;set;}
public string Stimulus { get; set; }
public QuestionValidation Validation { get; set; }
}

public struct QuestionValidation
{
string ScoringType { get; set; }
public ValidResponse ValidResponse { get; set; }
}

// Renamed:
public struct ValidResponse
{
public int Score { get; set; }
public object[] Value { get; set; }
}
}
BlackBerry-Py
BlackBerry-Py6mo ago
@Gramore - Thank You So much
Gramore
Gramore6mo ago
No worries 🙂
Want results from more Discord servers?
Add your server
More Posts