Error when using string.Join

user3177511

I'am loading a text file and displaying the textfile name in a listbox. And when I click the listbox im saving the path of the file to a variable. This works in another application of mine, but in the new application I get the following error :

Error 8 The best overloaded method match for 'string.Join(string, string[])' has some invalid arguments Error 9 Argument 2: cannot convert from 'System.Collections.Generic.IEnumerable' to 'string[]'

This is the code :

string fileloadpath;

private void FileListbox_SelectedIndexChanged(object sender, EventArgs e)
{
  var selectedItems = FileListbox.SelectedItems.Cast<FileItem>();                   

  var all = string.Join(Environment.NewLine, selectedItems.Select(x => x.Path1));

  fileloadpath = all;
}

Edit : I added ToArray() at the end and that fixed it. Thanks guys.

I have one more error related to this :

When I display the file path in the listbox, instead of the actual file name(test.txt), the text is displayed as : "OpenCV.Form1+FileItem"

Here is the code :

void reciperefresh()
   {
       FileListbox.Items.Clear();

       string[] files = Directory.GetFiles(@"C:\Recipe", "*.txt", 
          SearchOption.AllDirectories);

       foreach (string f in files)
       {
           var fileItem = new FileItem { Title1 = Path.GetFileName(f), 
            Path1 = Path.GetFullPath(f) };
           FileListbox.Items.Add(fileItem);

       }
   }
Owen Pauling

Select returns an IEnumerable but the method requires a string[]. Add ToArray after the Select.

var all = string.Join(Environment.NewLine, selectedItems.Select(x => x.Path1).ToArray());

Re: the additional part to your question (which should really be a new question), you need to change it to:

FileListbox.Items.Add(fileItem.Title1);

Otherwise it is using the Object.ToString() method which you are not overriding. Alternatively override the ToString method in your FileItem class.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Access syntax error when using Join

From Dev

'Too large to JOIN' error when I'm not using JOIN

From Dev

'Too large to JOIN' error when I'm not using JOIN

From Dev

Error when declaring and using a string

From Dev

AWS Cloudformation Error when using Fn::Join with a parameter

From Java

Error when using the same string in different tests

From Dev

ksprintf type error when using dynamic string

From Dev

Error when using a String as Range (VBA Excel)

From Dev

Having an error when using String in Comparator

From Dev

Error when using ?string("0.00") in FTL

From Dev

error in join using pig

From Dev

error when using ternary operator in formatting string (aka interpolated string)

From Dev

How to print a custom string when a value is null, using .NET's string.Join method?

From Dev

Using join with an iterator and a String function

From Dev

Why am I getting a type error when trying to join a list to a string?

From Dev

join using linq getting error

From Dev

LazyInitializationException when using join in CrudRepository

From Dev

SQL CASE when using JOIN

From Dev

Input must be a string MATLAB ERROR when using cellstr(sentiment)

From Dev

ConvertTo-Json throws error when using a string terminating in backslash

From Dev

"String or Binary Data Would be Truncated" Error when using Linq to SQL

From Dev

__toString() must not throw an exception error when using string

From Dev

Memory error when copying parts of string using strtok

From Dev

"Error while parsing the string" when using RPy2 and dplyr

From Dev

String Input error when using stringstream and if-else statement

From Dev

Error when using @RequestScope, @SessionScope, @ApplicationScope to create String Bean

From Dev

error when using "push()" to add string to JSON array

From Dev

__toString() must not throw an exception error when using string

From Dev

Error when converting datetime to string using dd/MM/yyyy format

Related Related

  1. 1

    Access syntax error when using Join

  2. 2

    'Too large to JOIN' error when I'm not using JOIN

  3. 3

    'Too large to JOIN' error when I'm not using JOIN

  4. 4

    Error when declaring and using a string

  5. 5

    AWS Cloudformation Error when using Fn::Join with a parameter

  6. 6

    Error when using the same string in different tests

  7. 7

    ksprintf type error when using dynamic string

  8. 8

    Error when using a String as Range (VBA Excel)

  9. 9

    Having an error when using String in Comparator

  10. 10

    Error when using ?string("0.00") in FTL

  11. 11

    error in join using pig

  12. 12

    error when using ternary operator in formatting string (aka interpolated string)

  13. 13

    How to print a custom string when a value is null, using .NET's string.Join method?

  14. 14

    Using join with an iterator and a String function

  15. 15

    Why am I getting a type error when trying to join a list to a string?

  16. 16

    join using linq getting error

  17. 17

    LazyInitializationException when using join in CrudRepository

  18. 18

    SQL CASE when using JOIN

  19. 19

    Input must be a string MATLAB ERROR when using cellstr(sentiment)

  20. 20

    ConvertTo-Json throws error when using a string terminating in backslash

  21. 21

    "String or Binary Data Would be Truncated" Error when using Linq to SQL

  22. 22

    __toString() must not throw an exception error when using string

  23. 23

    Memory error when copying parts of string using strtok

  24. 24

    "Error while parsing the string" when using RPy2 and dplyr

  25. 25

    String Input error when using stringstream and if-else statement

  26. 26

    Error when using @RequestScope, @SessionScope, @ApplicationScope to create String Bean

  27. 27

    error when using "push()" to add string to JSON array

  28. 28

    __toString() must not throw an exception error when using string

  29. 29

    Error when converting datetime to string using dd/MM/yyyy format

HotTag

Archive