0

Possible Duplicate:
How can I return multiple values from a function in C#?

Imagine we have a property which gets a value like 10-1685 , i would like to add another property inside a class that returns these two values as separate values.How can i achieve that?

    public string UserName { get; set; }



    public string UserNameSeparated
    {
        get { Username.Split('-');
        }
        set
        {
            ...What do do here??
        }
    }
Community
  • 1
  • 1
Sin5k4
  • 1,556
  • 7
  • 33
  • 57

3 Answers3

3

Change return type of property to array of strings:

public string UserName { get; set; }

public string[] UserNameSeparated
{
    get { Username.Split('-'); }
    set
    {
        UserName = String.Join("-", value);
    }
}

Usage:

foo.UserName = "10-1685";
string[] names = foo.UserNameSeparated;
// names[0] = "10";
// names[1] = "1685";
foo.UserNameSeparated = new string[] { "15", "42" };
// foo.UserName = "15-42"

BUT consider to have properties for both parts of name. You will be able to calculate full name at any time, and you will deal with nice named properties instead of tuples or array indexes (support hell):

public string FirstName { get; set; }
public string LastName { get; set; }

public string UserName
{
    get { return String.Format("{0}-{1}", FirstName, LastName); }
    set 
    {
        // check if value format valid
        var names = value.Split('-');
        FirstName = names[0];
        LastName = names[1];
    }
}

Just compare (you can give more descriptive names, which describe your data better):

foo.FirstName
foo.LastName

With:

foo.UserNameSeparated.Item1
foo.UserNameSeparated.Item2 
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
3

You can use a Tuple.

The question has been answered before here: How can I return multiple values from a function in C#?

Community
  • 1
  • 1
Jamie
  • 132
  • 2
  • 12
  • 1
    I feel like I'm missing something obvious here but... why would you want to do that rather than just create a class or struct with two properties on it? – Brandon Moore Dec 27 '12 at 10:16
  • 1
    @BrandonMoore agree. Never used tuples in my code. Extremely non-descriptive data type. It takes about 10 seconds to create class with two properties. – Sergey Berezovskiy Dec 27 '12 at 10:23
  • Yes, a class with two properties is another viable solution, as seen in the above link. – Jamie Dec 27 '12 at 10:30
0

On the post Jamie referenced, there is a reference to a post that one was based on: Returning multiple values from a C++ function

The 'popular' answer there was to use a touple. However, you should note Bill K, Fred Larson, and Jonathan Leffler's answers which all point out the better way to achieve this is to use a class or struct.

Also look at how Microsoft structures their own code in the .net framework. I have never once seen them use a touple as a return value for anything. I think that's a pretty good sign in itself as to which practice is better.

Community
  • 1
  • 1
Brandon Moore
  • 8,590
  • 15
  • 65
  • 120