2012-12-15 41 views
0

我是新来的编程和尝试做一个练习,以各种格式格式化日期到泰国文化这是我迄今为止我的代码:返回文化格式化日期数组C#MVC3

public String[] FormatAsSpecified(DateTime theDate, String theCulture, String[] formats) 
    { 
     String[] dateResults = new String[formats.Length]; 

     CultureInfo culture = CultureInfo.GetCultureInfo(theCulture); 

     for (int i = 0; i < formats.Length; i++) 
     { 
      String culture_formatted_date = theDate.ToString(formats[i], culture); 
      dateResults[i] = culture_formatted_date; 
     } 

     return dateResults; 
    } 

这是测试方法与它去:

[TestMethod] 
    public void FormatAsSpecifiedReturnsDateLiteralsInSpecifiedFormatForAllStandardFormatStrings() 
    { 
     //Arrange 
     var controller = new DateController(); 

     var theDate = new DateTime(2014, 2, 14, 9, 15, 32, 376); 
     String theCulture = "th-TH"; 
     // Array of all supported standard date and time format specifiers. 
     String[] formats 
      = { "d", "D", "f", "F", "g", "G", "m", "o", "r", "s", "t", "T", "u", "U", "Y" }; 
     //Corresponding date literals for the standard Thai regional settings 
     String[] expectedResults 
      = {"14/2/2557" 
       , "14 กุมภาพันธ์ 2557" 
       , "14 กุมภาพันธ์ 2557 9:15" 
       , "14 กุมภาพันธ์ 2557 9:15:32" 
       , "14/2/2557 9:15" 
       , "14/2/2557 9:15:32" 
       , "14 กุมภาพันธ์" 
       , "2014-02-14T09:15:32.3760000" 
       , "Fri, 14 Feb 2014 09:15:32 GMT" 
       , "2014-02-14T09:15:32" 
       , "9:15" 
       , "9:15:32" 
       , "2014-02-14 09:15:32Z" 
       , "วันศุกร์ที่ 14 กุมภาพันธ์ 2014 9:15:32" 
       , "กุมภาพันธ์ 2557"}; 

     //Act 
     String[] actualResults = new String[15]; 
     for (int i = 0; i < formats.Length; i++) 
     { 
      actualResults[i] 
       = controller.FormatAsSpecified(theDate, theCulture, formats[i]); 
     } 

     //Assert 
     CollectionAssert.AreEqual(expectedResults, actualResults); 
    } 

我得到在 'controller.FormatAsSpecified(theDate,theCulture,格式[I]);' 的测试方法的错误说:“参数3,不能从'字符串'转换为'字符串[]'” 我做错了什么?

回答

2

变化

controller.FormatAsSpecified(theDate, theCulture, formats[i]); 

controller.FormatAsSpecified(theDate, theCulture, formats); 

我想你应该将代码从

//Act 
String[] actualResults = new String[15]; 
for (int i = 0; i < formats.Length; i++) 
{ 
    actualResults[i] 
     = controller.FormatAsSpecified(theDate, theCulture, formats[i]); 
} 

改变

String[] actualResults = 
      controller.FormatAsSpecified(theDate, theCulture, formats); 
+0

嗨,谢谢,但是当我这样做时,它说,“不能隐式转换类型的字符串[]'为'字符串'” – user1875797

+0

@ user1875797在哪一行你会得到这个错误。我想,这是在不同的路线。 – I4V

+0

与之前相同的行: “= controller.FormatAsSpecified(theDate,theCulture,formats)” – user1875797