[英]How do I declare a variable as the same data type as the method returns in c#?
所以我是完全不是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));
要調用一個函數,您只需要(從該行中刪除new
的)
MyDate[] myDates = getAllDates();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.