Assigning Values to Nested Class Members
I am new to C#. I am wondering how to instantiate an object with nested class members:
I am trying to figure out how to assign the values to the QuestionValidation Stuct and ScoringType
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; }
}
}
}
}
}
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
You haven't defined a field on
Where
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:
Or using initializer syntax:
The alternative here being something like:
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; }
}
}
}
}
}
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; }
}
}
}
}
}
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
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.
}
)
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; }
}
}
@Gramore - Thank You So much
No worries 🙂