Home > Software engineering >  List<class> does not contain definition for string
List<class> does not contain definition for string

Time:12-21

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

CodePudding user response:

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.

CodePudding user response:

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,
        }
  • Related