netmajor
netmajor

Reputation: 6585

Property setter type other value set to property?

It is possible for the type returning by a property's get accessor to be different from the type used by the set accessor?

private string[] artists;
public string[] Artists
{
    get { return artists[0]; } //heree getter return string than string[]
    set { artists = value; OnPropertyChanged("Artist"); }
}

Upvotes: 1

Views: 592

Answers (4)

Henk Holterman
Henk Holterman

Reputation: 273701

No, this won't work.

You could add another property, and include it in the ONPC:

public string Artist
{
    get { return artists[0]; }
}

public string[] Artists
{
    // get { return artists; }   // get string[] is optional
    set { artists = value; OnPropertyChanged("Artist"); OnPropertyChanged("Artists");}
}

Provide a sample of intended use with some context and you might get a better answer.

Upvotes: 2

Kirk Broadhurst
Kirk Broadhurst

Reputation: 28738

No, not possible. It is plain to see that your code is illegal, because your get accessor does not fulfil it's obligation to return a string[]

// you define the Artists property as a string[],
// therefore the get accessor must return a string[]
public string[] Artists
{
    // you are returning only a string
    get { return artists[0]; }

Upvotes: 0

Ivan Crojach Karačić
Ivan Crojach Karačić

Reputation: 1911

Why should this be possible. If you declare the return type then this HAS to be returned.

Upvotes: 0

Thomas Li
Thomas Li

Reputation: 3338

No this is not possible. You have to create another property to return another type.

Upvotes: 1

Related Questions