如何将变量声明为与c#中返回的方法相同的数据类型?

时间:2011-10-08 02:09:32

标签: c# methods

所以我是新的而不是c#,我没有想法尝试这个。我花了几个小时研究,但还没有找到解决方案。

我的问题是针对函数getUpcomingDates():如何将变量 myDates 声明为与 getAllDates 方法返回?

我试过的方法之一是:MyDate[] myDates = new getAllDates();但是我在getAllDates下面有一条红线说:'这是一种方法,但用作类型。'

这样做的正确方法是什么?

[WebMethod]
private MyDate[] getAllDates()
{
    System.Collections.ArrayList myDates = new System.Collections.ArrayList();
    MyDate thisDate;


    thisDate = new MyDate(DateTime.Parse("9/1/2011"),
                          "Get classes ready", "Begin of fall semester");
    myDates.Add(thisDate);

    thisDate = new MyDate(DateTime.Parse("1/1/2011"),
                          "New Year", "Happy 2011!");
    myDates.Add(thisDate);

    thisDate = new MyDate(DateTime.Parse("12/16/2011"),
                          "BUS ADM 531 Final Exam", "See study guide on course Web pages");
    myDates.Add(thisDate);

    return (MyDate[])myDates.ToArray(typeof(MyDate));
}

[WebMethod]
private MyDate[] getUpcomingDates()
{
    MyDate[] myDates = new getAllDates();
    System.Collections.ArrayList upcomingDates = new System.Collections.ArrayList();


    foreach (MyDate thisDate in myDates)
    {
        if (thisDate.CalendarDate > DateTime.Now)
        {
            //date is upcoming, add to output list of dates
            upcomingDates.Add(thisDate);
        }
        else
        {
            //not an upcoming date, do not add to output list
        }

        return (MyDate[])upcomingDates.ToArray(typeof(MyDate));

1 个答案:

答案 0 :(得分:5)

要调用某个函数,您只需要(从行中删除new

 MyDate[] myDates = getAllDates();