ためString.Format
を使用してみてください。 まあ...私の答えは: 各国のすべての3letterCodeは国の最初の3文字(例えばSPAin => SPA
)で作成されていると仮定していたと思います。アプローチの間違った、ここを見て: 戻るhttp://www.worldatlas.com/aatlas/ctycodes.htm
コードに:
public class CountriesFactory
{
private static List<Country> _countries = new List<Country>()
{
new Country("Spain", "Spa"),
new Country("Germany", "Ger")
}
public static Country GetCountryByName(string countryName)
{
return _countries.Where(p => p.CountryName == countryName).FirstOrDefault() ?? Country.NONE;
}
}
public class Country
{
public string CountryName { get; private set; }
public string ThreeLetterCode { get; private set; }
public const Country NONE = new Country("", "");
public Country (string countryName, string threeLetterCode)
{
this.CountryName = countryName;
this.ThreeLetterCode = threeLetterCode;
}
}
public class Person
{
public string Name { get; set; }
public string Surname { get; set; }
public Country Country { get; set; }
public override string ToString()
{
return string.Format("{0} {1}. ({2})", Name, Surname[0].ToString(), Country.ThreeLetterCode);
}
}
は、追加すべきいくつかの条件がありますが、それは今重要ではありません。コード内
使用:あなたの例による
Person person = new Person()
{
Name = "Endi",
Surname = "Marrei",
Country = CountriesFactory.GetCountryByName("Spain")
}
Console.WriteLine(person.ToString());
:
var firstString = "Endi Marrei";
var SecondString = "Spain";
var person = new Person()
{
Name = firstString.Split(' ')[0].ToString(),
Surname = firstString.Split(' ')[1].ToString(),
Country = CountriesFactory.GetCountryByName(SecondString)
}
は、私は私のアプローチは、将来的にはより有用であろうと思います。
出典
2016-06-19 15:26:46
W92
"私のやり方が働いているにもかかわらず、" - それはありますか?あなたの質問に「Marrei E.(Spa)」と尋ねると、あなたは「Marrei E.(???)」を得るでしょうか? – hvd
私はVisual Studioでコードを実行しています。それを実行してみてください – Yoseph