0

I have named a class "File", where there are some "attributes" as properties.

 class File
    {
        public string Attribute1 { get; set; }
        public string Attribute2 { get; set; }
        public string Attribute3 { get; set; }
        public string Attribute4 { get; set; }

    }

The Text-files that I want to read, contains these "attributes". For Example I have read files, and file names are like below:

List<string> filenames = new List<string>();
            filenames.Add("A0A01M");
            filenames.Add("A1G4AA");
            filenames.Add("A1L4AR");
            filenames.Add("A1L4AX");
    ...(more than 3000 Files)

How to crate class objects based on this list for each text files, which contains attributes ?! I am a little bit confused here!!

13
  • Please clarify the purpose of the attributes. Show an example content of a file Commented Nov 2, 2021 at 10:14
  • How does the File class relate to the list? It's unclear. Commented Nov 2, 2021 at 10:14
  • 2
    Well, split this problem into two parts: 1) Reading/writing a single file; 2) Using the result of 1 in a loop or using LINQ to handle multiple files. Which part are you stuck on at the moment? Commented Nov 2, 2021 at 10:14
  • so you could create instances of File using new File { Attribute1 = "A1...", Attribute2 = "A2..." }... Commented Nov 2, 2021 at 10:14
  • 2
    No need to type in CAPS, that won't help you getting an answer Commented Nov 2, 2021 at 10:19

2 Answers 2

1

You can use LINQ:

List<File> files = filenames.Select(CreateFile).ToList();

and a method CreateFile:

static File CreateFile(string fileName)
{
    // probably load the file from disc here and extract attributes
    return new File
    {
        Attribute1 = ...,
        Attribute2 = ...,
        ...
    };
}
Sign up to request clarification or add additional context in comments.

2 Comments

I think this solutions is not the best option , you dont use filename for nothing, I think the filename attribute has to be on File class.
@hesolar: my comment in CreateFile suggests that OP needs to use the fileName to load that file from disc. But the implementation is up to OP and out of scope of this question. I guess the fileName is not part of the File class(but it's easy to add it as additional property). He wants to initialize 5 attributes from the content of each file.
0

First add constructor with a id:

class File
    {
        public File(String id){this.ID=id;}
        public string ID {get; set;}
        public string Attribute1 { get; set; }
        public string Attribute2 { get; set; }
        public string Attribute3 { get; set; }
        public string Attribute4 { get; set; }

    }

Then on your main method where you get the filename list you can do that: List filenames.... Create a empty list of files:

List<File> fileList = new List<File>();

Then fill fileList with the other values of the filenames list

filenames.ForEach(filenameID=> fileList.Add(filenameID);

Comments

Your Answer

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