skip to Main Content

I have declared a helper class to create a JSON. But I just doesn not want to work properly with Lists. Any suggestions to this code? The error occours when trying to set roleDescription and roleType value.

Error: ‘List< generateCredentialsRoleList>’ does not contain a definition for ‘roleDescription ‘

Error: ‘List< generateCredentialsRoleList>’ does not contain a definition for ‘roleType’

public class generateTokenRequest
{
    public generateToken generateToken { get; set; }
}
public class generateToken
{
    public List<generateCredentialsRoleList> credentialsRoleList { get; set; }
    public string description { get; set; }
}

public class generateCredentialsRoleList
{
    public string roleDescription { get; set; }
    public string roleType { get; set; }
}

public string generateTokenRequest(string roleDescription, string roleType, string description)

{
    generateTokenRequest generateTokenRequest = new generateTokenRequest
    {
        generateToken = new generateToken
        {
            credentialsRoleList = new List<generateCredentialsRoleList>
            {
                roleDescription = roleDescription,
                roleType = roleType,

            },
            description = description,
        }

    };
    string json = JsonConvert.SerializeObject(generateTokenRequest, Newtonsoft.Json.Formatting.Indented);

    return json;
}

2

Answers


  1. You need to add new item in list initializer like this:

    generateToken = new generateToken
            {
                credentialsRoleList = new List<generateCredentialsRoleList>
                {
                   new generateCredentialsRoleList() {
                      roleDescription = roleDescription,
                      roleType = roleType
                     }
    
                },
                description = description,
            }
    
    Login or Signup to reply.
  2. Here:

    credentialsRoleList = new List<generateCredentialsRoleList>
    {
        roleDescription = roleDescription,
        roleType = roleType,
    
    },
    

    you try to initialize a list. A list takes items. You don’t provide items, you provide property values of an item.

    How to fix this depends on what you actually want to do. Do you want to initialize the list with a single item containing those properties? That would be done as follows:

    credentialsRoleList = new List<generateCredentialsRoleList>
    {
        new generateCredentialsRoleList()
        {
            roleDescription = roleDescription,
            roleType = roleType
        }
        // you could add more items to your list here, if you wanted
    },
    

    Some additional recommendations:

    • generateCredentialsRoleList is not a good name of the item in a list. It’s like calling a single apple a "bag of apples".
    • When asking a question on StackOverflow, please always provide the complete error message you get right in the question. Sometimes the cause of an error is obvious, but most of the time it isn’t, and the precise error message helps.
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search