-3

I have a class as below.

public class PurgeRecord
{
    public int Index { get; set; }
    public string Source { get; set; }
    public int PurgeFileID { get; set; }
    public string AuthorisationID { get; set; }
    public string RecordSystem { get; set; }
    public string KeyName { get; set; }
    public string[] KeyValues { get; set; }
    public bool IsValid { get; set; }
    public string Action { get; set; }
    public string ErrorDetail { get; set; }
    public string FileName { get; set; }
}

I am getting some string values separated by '|' into string array and lopping over it as follows.

string[] test = Convert.ToString(values[5]).Split('|');
foreach (string key in test)
{
    purgeRecord = new PurgeRecord()
    {
        KeyValues = key,
        IsValid = true,
        FileName = "XYZ"
    };
    lstPurgeRecords.Add(purgeRecord);
}

But I am getting an error on key as cannot convert string to string[] implicitly. I tried many ways and tried googling as well but no luck.

Please help.

Bharadwaj
  • 2,535
  • 1
  • 22
  • 35
Abhijit
  • 31
  • 1
  • 10
  • If `key` is a name then assign it to `KeyName` property – Bharadwaj Jan 17 '18 at 13:52
  • 7
    The problem isn't only with the code, the problem is that we don't understand what you're trying to do. Why did you try placing a string value into an array variable? What was the expected outcome? – Lasse V. Karlsen Jan 17 '18 at 13:55
  • KeyValues is of string[] datatype and you are assigning that to a string value key – Maddy Jan 17 '18 at 14:04

3 Answers3

0

Split itself returns String[]

Splits a string into substrings that are based on the characters in an array.

Its Syntax

public string[] Split(params char[] separator)

Simply update your code

from

string[] test = Convert.ToString(values[5]).Split('|');

with

string[] test = values[5]).Split('|');
Ehsan Ullah Nazir
  • 1,827
  • 1
  • 14
  • 20
0

You can simply put it inside a string[] like this:

KeyValues = new string[] { key }

Or even shorter format:

KeyValues = new [] { key }
DavidG
  • 113,891
  • 12
  • 217
  • 223
0

The KeyValues property type is string array, when you are trying to initialize a PurgeRecord instance you are trying to insert a string into a string[].

So this is what you should do:

//Every object instance in C# have the function 'ToString'.
string[] test = values[5].ToString().Split('|');

foreach (string key in test)
{
    purgeRecord = new PurgeRecord()
    {
        //Create an array to insert in the values, but probably this is the KeyName
        KeyValues = new[] { key },
        IsValid = true,
        FileName = "XYZ"
    };
    lstPurgeRecords.Add(purgeRecord);
}

There is a nice way to do with Linq too:

lstPurgeRecords = values[ 5 ]
                    .ToString()
                    .Split( '|' )
                    .Select( key => new PurgeRecord 
                    {
                        KeyValues = new[] { key },
                        IsValid = true,
                        FileName = "XYZ"
                    } );
Lucas
  • 1,259
  • 15
  • 25