patel.milanb
patel.milanb

Reputation: 5992

how to add properties to class without changing return type in c#

I have a class which i can read but not write because of the company policy. I have following structures in my project.

public class Name                    // can not touch this class OR modify it
{
  public string FirstName { get; set; }
  public string LastName { get; set; }

  public string GetNames()
  {
        return FirstName + " " + LastName;
  }
}

public class Details
{
  // some methods and properties.

    public Name LoadName()          // This method return type i can not change as this method is used in ObjectDataSource for GridView
    {
        var names = new Name();
        if (txtInpput.Text == "Jermy")
        {
              names.FirstName = "Jermy";
              names.LastName = "Thompson";
        }
        else
        {
              names.FirstName = "Neville";
              names.LastName = "Vyland";
        }

        return
        names;
    }
}

No I want to add extra property in class Name called "Email Address" and use class Details LoadName() method to return the type which includes the **Email Address** as well. as i am bound to use that method i can not change the return type Name to something else.

I can extend the class Name and overrides the GetNames() method to include the newly created property but that wont help as i can not change the return type in class Details LoadName() method.

I am not sure this is even possible BUT just wondering if there are any solutions.

I am suing
.NET 3
VS 2010

Upvotes: 0

Views: 640

Answers (2)

sangram parmar
sangram parmar

Reputation: 8736

Try this:

    public class Name                    // can not touch this class OR modify it
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }

        public string GetNames()
        {
            return FirstName + " " + LastName;
        }
    }
    public class Name1:Name {
        public string EmailAddress { get; set; }
        public override string GetNames()
        {
            return FirstName + " " + LastName+" "+EmailAddress;
        }

    }
    public class Details
    {
        // some methods and properties.

        public Name LoadName()          // This method return type i can not change as this method is used in ObjectDataSource for GridView
        {
            TextBox txtInpput = new TextBox();
            var names = new Name();
            if (txtInpput.Text == "Jermy")
            {
                names.FirstName = "Jermy";
                names.LastName = "Thompson";
            }
            else
            {
                names.FirstName = "Neville";
                names.LastName = "Vyland";
            }

            return
            names;
        }
    }
    public  class Details1:Details {
        public override Name LoadName()          
        {
            TextBox txtInpput = new TextBox();
            var names = new Name();
            if (txtInpput.Text == "Jermy")
            {
                names.FirstName = "Jermy";
                names.LastName = "Thompson";
            }
            else
            {
                names.FirstName = "Neville";
                names.LastName = "Vyland";
            }

            return
            names;
        }
    }

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726809

If you cannot change the signature of the method, your callers would need to do some casting. This is not optimal, but you can still do it:

public class NameWithEmail : Name {
    public string EMail {get;set;}
}
...
public Name LoadName() {
    ...
    return new NameWithEmail(); // OK because NameWithEmail extends Name
}

Now the caller would need to know the new type, do a cast, and access the email through it:

NameWithEmail name = Details.LoadName() as NameWithEmail;
if (name != null) {
    Console.WriteLine("{0} {1} : {2}", name.FirstName, name.LastName, name.EMail);
}

The trickiest part is binding the new property to a data grid. This answer explains how this can be done.

Upvotes: 3

Related Questions