GMon
GMon

Reputation: 678

Get array from C# string property

I'm retrieving a row from a database that has an attribute which is a comma separated list of ID's

1,2,3,4,5

In my POCO is it possible to do something like this to get an array back?

public string SomeIDs 
{
    get 
    {
        return SomeIDs.split(',');
    }
    set;
}

Edit: Sorry, to clarify, I am setting with a string and want to return a string array

Upvotes: 2

Views: 400

Answers (3)

Yuval Itzchakov
Yuval Itzchakov

Reputation: 149518

You can't have a setter which accepts a string and returns a string[]. You'll need to expose one property which accepts a string, and a read-only property (as below) which returns a parsed array from that string:

private static readonly string[] emptyIds = new string[0];
public string SomeIds { get; set; }
public string[] ParsedIds 
{
    get 
    {
        return !string.IsNullOrEmpty(SomeIds) ? SomeIds.Split(',') : 
                                                emptyIds;
    }
}

Edit:

Upvotes: 7

Nacho
Nacho

Reputation: 65

you can try this:

private string _someIDs;
public object SomeIDs
{
  get { return _someIDs.Split(','); }
  set { _someIDs = value as string; }
} 

Upvotes: -2

dknipper
dknipper

Reputation: 69

Is this what you need:

private string _ids;
public string[] SomeIDs
{
    get { return _ids.Split(','); }
}

Upvotes: 1

Related Questions