Link to home
Create AccountLog in
Avatar of SubbuUSA
SubbuUSAFlag for United States of America

asked on

c# question

Friends,

I have a snippet which has to be separate method call and should return me two strings. I will send fullName as a parameter from an other method and it should do the split and return me fristname and lastname as separate strings.

if (!string.IsNullOrEmpty(fullName))
  {

int nameSplit = fullName.LastIndexOf(' ');
  if (nameSplit > 0){
							eventFeedPerson.GroupEventPersonFirstName = fullName.Substring(0, nameSplit);
							eventFeedPerson.GroupEventPersonLastName = fullName.Substring(nameSplit + 1);
}
							else
{
							eventFeedPerson.GroupEventPersonFirstName = fullName;
eventFeedPerson.GroupEventPersonLastName = string.Empty;
	}
 }

Open in new window

Avatar of Pryrates
Pryrates
Flag of Germany image

you can only return ONE value from a method, not two.

you could return a class that contains first an last name as property.
Those properties you can match to your eventFeedPerson object

        private void button1_Click(object sender, EventArgs e)
        {            
            Person p = SplitName("Hans Meiser");
            MessageBox.Show(p.FirstName + " " + p.LastName);
        }

        public Person SplitName(string fullName)
        {
            if (string.IsNullOrEmpty(fullName))
                return new Person(null, null);

            int nameSplit = fullName.LastIndexOf(' ');
            if (nameSplit > 0)
            {
                return new Person(fullName.Substring(0, nameSplit), fullName.Substring(nameSplit + 1));
            }
            else
            {
                return new Person(fullName, null);
            }
        }
    public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public Person(string fn, string ln)
        {
            this.FirstName = fn;
            this.LastName = ln;
        }
    }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Calling example:
string source = "John Doe";
string fn, ln;

SplitStrings(source, out fn, out ln);

Open in new window

Avatar of SubbuUSA

ASKER

Thanks..your code was excellent
NP. Glad to help  = )