4

I created the following:

public class HttpStatusErrors
{
 public HttpStatusErrors()
 {
 this.Details = new List<HttpStatusErrorDetails>();
 }
 public string Header { set; get; }
 public IList<HttpStatusErrorDetails> Details { set; get; }
}
public class HttpStatusErrorDetails
{
 public HttpStatusErrorDetails()
 {
 this.Errors = new List<string>();
 }
 public string Error { set; get; }
 public IList<string> Errors { set; get; }
}

In my code I am using it like this:

var msg = new HttpStatusErrors();
 msg.Header = "Validation Error"; 
 foreach (var eve in ex.EntityValidationErrors) {
 msg.Details. // Valid so far 
 msg.Details.Error // Gives the error below:

The Ide recognizes msg.Details as being valid but when I try to write the second line I get:

Error 3 'System.Collections.Generic.IList<TestDb.Models.Http.HttpStatusErrorDetails>' 
does not contain a definition for 'Error' and no extension method 'Error' accepting a first 
argument of type 'System.Collections.Generic.IList<TestDb.Models.Http.HttpStatusErrorDetails>' 
could be found (are you missing a using directive or an assembly reference?) 
C:\K\ST136 Aug 16\WebUx\Controllers\ProblemController.cs 121 33 WebUx

Is there something I am doing wrong? I thought the way I had this set up that new Lists would be created when the first class was created.

asked Aug 15, 2013 at 10:22
1
  • 2
    You're trying to assign a value of a single variable to a List. This cannot be done. Commented Aug 15, 2013 at 10:24

3 Answers 3

10

msg.Details returns a List object. List<T> does not have an Errors property. You need to access a specific element in your list, and only then will you have your Errors property.

For example:

msg.Details[0].Error

In your code you might want to make sure that msg.Details contains elements before trying to access them, or better yet iterate over them in a foreach loop.

answered Aug 15, 2013 at 10:24

3 Comments

@Jason No idea, especially seeings as it's the right answer. Should probably check that Details isn't empty before accessing the element, though.
@Jason I guess someone didn't understand the question/answer.
Btw, OP tries to access the Error not the Errors property, both exist ;)
0

Details is a collection. The property Error belongs to an item in the collection Details. There is no property Error on that collection Details

answered Aug 15, 2013 at 10:25

Comments

0

you are tying to go to Details which is a IList<HttpStatusErrorDetails>.

if you want to go through the items in that list u need go over them for example

msg.Details[number].Errors

or

foreach(HttpStatusErrorDetails err in msg.Details)
{
 err.Errors
}
answered Aug 15, 2013 at 10:25

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.