buzzzzjay
buzzzzjay

Reputation: 1150

C# Building a Method Variable inside another Method Variable

I have a method and I am trying to add another method as a list variable so that I can add multiple Error's per file. I am currently passing the files list variable to multiple different functions. I would like the Error variable to be contained in the files, but I have been unable to figure it out. Thanks!

    class AllFiles
    {
        public string FileName { get; set; }
        public string FileType { get; set; }
        ...
        public List<ErrorClass> Error { get; set; }
    }

    class ErrorClass
    {
        public int ErrorCode { get; set; }
        public int Total { get; set; }
        public string ErrorMessage { get; set; }
        ...
    }

I use the following to initialize my files as a list so I can add multiple files.

        List<AllFiles> files = new List<AllFiles>();
        files.Add(new AllFiles());

I am wanting it to look like the following:

files[0]
   Error[0]
   Error[1]
files[1]
   Error[0]
   Error[1]
   Error[2]
files[2]
   Error[0]
   ...

Upvotes: 1

Views: 188

Answers (1)

Jord&#227;o
Jord&#227;o

Reputation: 56477

You need to create an instance of the List<ErrorClass> (ideally) inside the AllFiles class' constructor, and assign it to the Error property.

Some other recommendations:

  • AllFiles is not a good name for a class that represents one file
  • ErrorClass could maybe just be called Error or FileError
  • The Error property inside AllFiles should be named Errors and should have a private setter.

E.g.:

class MyFile {
  public MyFile() {
    Errors = new List<MyFileError>();
  }

  public string Name { get; set; }
  public string Type { get; set; }
  ...
  public List<MyFileError> Errors { get; private set; }
}

class MyFileError {
  public int Code { get; set; }
  public int Total { get; set; }
  public string Message { get; set; }
  ...
}

Depending on your design, you could better encapsulate the error list inside MyFile and just expose an IEnumerable<MyFileError> and an AddError(MyFileError) method.

Upvotes: 3

Related Questions