locked
Working with Lists RRS feed

  • Question

  • User-1664485818 posted

    Hi folks, I have a list that could contain 1 file name and up to 5 file names. I need to assign each file name to a string.

    What’s the best way to achieve this?

    I guess I could use an if statement to check the count…

    Any pointers much appreciated.

                        string file0;
                        string file1;
                        string file2;
                        string file3;
                        string file4;
    
                        List<string> myFiles = new List<string>();
    
                        foreach (var item in UploadFiles)
                        {
                            myFiles.Add(item.Filename);
                        }
                   
                        if (myFiles.Count == 0)
                        {
                             file0 = myFiles[0];
                        }
                        else if (myFiles.Count == 1)
                        {
                             file0 = myFiles[0];
                             file1 = myFiles[1];
                        }

    Friday, April 10, 2020 12:15 PM

All replies

  • User475983607 posted

    This is a nutty design that's not reasonable or recommended.  You could pull it off but you'll need to come up with the maximum items you expect.  Declare the same amount of variables as the maximum.  Then have craft a case statement to populate the variables by the initial array count.

    It would be much easier to use the array that you already have.  Can you explain the use case for this crazy design?

    Friday, April 10, 2020 12:34 PM
  • User753101303 posted

    Hi,

    It's common to see a beginner having file0, file1, file2 and asking how to programmatically use any of them (the response being to use an array or list).

    This is the first time though I see someone trying the other way round. What is the benefit of having a data structure that allows to handle file[x] and map each item back to independently declared variables which will be much less flexbile ??? 

    What will you do then later that you couldn't do with a list ?

    Friday, April 10, 2020 1:18 PM
  • User-1664485818 posted

    The max items will be 5, I need to map each item to a string, what’s the recommended way?

    How would you check for nulls?  

    Friday, April 10, 2020 2:07 PM
  • User475983607 posted

    brucey

    The max items will be 5, I need to map each item to a string, what’s the recommended way?

    How would you check for nulls?  

    I think you are looking for a dictionary.

    https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2?view=netframework-4.8

    Dictionary<string, string> dic = new Dictionary<string, string>();
    int i = 0;
    foreach (var item in UploadFiles)
    {
    	dic.Add($"file{i++}", item.Filename);
    }

    For the second time, it will be much easier to provide assistance if you explain the use case (problem you are trying to solve) not your solution.

    Friday, April 10, 2020 2:32 PM
  • User753101303 posted

    I don't think you need this and never saw someone needing that. After:

    foreach (var item in UploadFiles)
    {
        myFiles.Add(item.Filename);
    }
    

    You canc heck myFiles.Count to see if you have between 1 and 5 files and do whatever you want. foreach tthat you already used) allows to process all files one after the other without having to care if you have 1, 2, 3 , 4 or 5 files (ore more later if you just change the Count test) etc...

    Edit: or you created file0 to file5 columns in a db to store those names which is a bad design ??? Explain what you could do with file0, file1 etc... that you couldn't do (and more easily) by processing each item found in a list.

    Friday, April 10, 2020 2:33 PM
  • User-1664485818 posted

    I'm working with MigraDoc to create a pdf document. I don't know if I can sling it into a table with a foreach loop.

    I can create MigraDoc paragraphs by using strings...

    Friday, April 10, 2020 2:58 PM
  • User475983607 posted

    I'm working with MigraDoc to create a pdf document. I don't know if I can sling it into a table with a foreach loop.

    I can create MigraDoc paragraphs by using strings...

    Are you trying to convert the file contents into a string?

    Friday, April 10, 2020 3:10 PM
  • User-1664485818 posted

    Yes, just trying to pass data from the database to pdf.

    I would usually just use a html table and populate the table with a foreach loop

    Friday, April 10, 2020 3:15 PM
  • User753101303 posted

    You want to merge multiple PDF files into a single file (to a new one or by appending to the first file ?)

    Humm you already used foreach on a list so why suddently it would not work with your own list? A list of string is just a convenient way to add/remove/.browse/process multiple strings stored in a single structure rather than having to write code against disjointed file0, file1, file2 etc... variables.

    An array could be enough if you know how many items you want from the start and won't need to add or remove items later. Then you need to use a list instead if you need to add or remove items at any time...

    Edit: ok and how would you use a foreach loop on file0, file1, file2 etc... ??? you already have a list and a foreach loop will work just fine.

    Write your code with foreach and show which problem you have when trying... Ypu want to show file names inside an HTML table and save that as PDF file using the product you mentioned ???

    Friday, April 10, 2020 3:18 PM
  • User475983607 posted

    Yes, just trying to pass data from the database to pdf.

    I would usually just use a html table and populate the table with a foreach loop

    I'm guessing that you've written code that expects a string but you have an array and not sure what to do.   The standard solution is a loop but, unfortunately, you have not clearly defined the problem you are trying to solve.  

    Friday, April 10, 2020 3:21 PM
  • User-1664485818 posted

    Sorry guys I think I have confused the issue….

     I would ideally like to populate a table with my list

    The table is MigraDoc

              
                
                Table table = document.LastSection.AddTable();
                table.Borders.Visible = true;
                table.TopPadding = 5;
                table.BottomPadding = 5;
    
                Column column = table.AddColumn();
                column.Format.Alignment = ParagraphAlignment.Left;
           
                Row row = table.AddRow();
                row.Cells[0].AddParagraph("Merge Right");
                row.Cells[0].MergeRight = 1;
                table.AddRow();

    I would like to add my  List<string> myFiles = new List<string>(); to the table just not sure how to do this....

    I would usually do something like this...

      @foreach (var item in Model.Files)
         {
           <tr>
           <td>@item.FileName</td>
           <tr>
         }

    Friday, April 10, 2020 3:27 PM
  • User475983607 posted

    brucey

    Sorry guys I think I have confused the issue….

     I would ideally like to populate a table with my list

    The table is MigraDoc

              
                
                Table table = document.LastSection.AddTable();
                table.Borders.Visible = true;
                table.TopPadding = 5;
                table.BottomPadding = 5;
    
                Column column = table.AddColumn();
                column.Format.Alignment = ParagraphAlignment.Left;
           
                Row row = table.AddRow();
                row.Cells[0].AddParagraph("Merge Right");
                row.Cells[0].MergeRight = 1;
                table.AddRow();

    I would like to add my  List<string> myFiles = new List<string>(); to the table just not sure how to do this....

    I would usually do something like this...

      @foreach (var item in Model.Files)
         {
           <tr>
           <td>@item.FileName</td>
           <tr>
         }

    Contact MigraDoc support for assistance or read the reference docs.

    http://www.pdfsharp.net/wiki/Invoice-sample.ashx

    I assume a pattern similar to the following but it is up to you to learn how to use MigraDoc.

    foreach(var string in ListOfItems)
    {
    	Row row = table.AddRow();
    	row.Cells[0].AddParagraph("Merge Right");
    	row.Cells[0].MergeRight = 1;
    	row.Cells[0].AddParagraph(item);
    	table.AddRow();
    }

    Friday, April 10, 2020 3:56 PM