Well, I would do some refactoring of the code.
Imagine he would have 10 other types to implement :)
The solutions provided above are workable but not very elegant in terms of extensibility.
So, here is my solution:
1) Implement a base class with the common contact properties
public abstract class BaseContact
{
public string Name { get; set; }
public abstract string Url { get; set; }
}
2) Implement the concrete types
public class FbContact : BaseContact
{
private string _baseUrl = "http://facebook.com/{0}";
private string _url = string.Empty;
public override string Url
{
get { return _url; }
set { _url = string.Format(_baseUrl, value); }
}
}
public class LinkedInContact : BaseContact
{
private string _baseUrl = "http://linkedin.com/{0}";
private string _url = string.Empty;
public override string Url
{
get { return _url; }
set { _url = string.Format(_baseUrl, value); }
}
}
3) This is just a helper class for setting the navigation url
public static class NavigationCreator
{
public static void SetUrl(BaseContact contact, HyperLink link)
{
link.NavigateUrl = contact.Url;
}
}
4) Some test code to visualize the result
List<BaseContact> items = new List<BaseContact>();
for (int i = 0; i < 5; i++)
{
BaseContact item;
if (i % 2 == 0) item = new FbContact(); else item = new LinkedInContact();
item.Url = "My name " + i;
items.Add(item);
}
foreach (var contact in items)
{
HyperLink link = new HyperLink();
NavigationCreator.SetUrl(contact, link);
Console.WriteLine(link.NavigateUrl);
}
Console.Read();