-1

我有一个这样的 php 代码,我需要将其转换为 .NET

function jaktDate2()
{
Global $nameofselectbox,$startYear,$endYear,$year,
$startDate,$endDate,$startMounth,$endMounth,$startDay,$endDay;
$today = getdate();
$year=$today['year'];
$mounth=$today['mon'];
$day=$today['mday'];
//$year="2004";
//$mounth="7";
//$day="1";

if(round($mounth)<="6"){$startYear=$year-1;$startMounth="-07";$startDay="-01";
$endYear=$year;$endMounth="-06";$endDay="-30";}
elseif(round($mounth)>="7"){$startYear=$year;$startMounth="-07";$startDay="-01";
$endYear=$year+1;$endMounth="-06";$endDay="-30";}

$startDate=$startYear.$startMounth.$startDay;
$endDate=$endYear.$endMounth.$endDay;

return $startDate.";".$endDate;
}

这是我的转换:

  public class HuntingDate
    {
        public string StartYear;
        public string EndYear;
        public string Year;
        public DateTime StartDate;
        public DateTime EndDate;
        public string StartMonth;
        public string EndMonth;
        public DateTime StartDay;
        public DateTime EndDay;

        private string _startYear;
        public string StartYear
        {
            get { return _startYear;}
            set { _startYear = value;}
        }

        private string _endYear;
        public string EndYear
       {
        get{return _endYear;}
        set { _endYear = value; }
       }


        private DateTime _startDate;
        public DateTime StartDate
        {
            get { return _startDate;}
            set { _startDate = value;}
        }

        private DateTime _endDate;
        public DateTime EndDate
        {
            get { return _endDate;}
            set { _startDate = value;}
        }

        private string _year;
        public string Year
        {
            get { return _year;}
            set { _year = value;}
        }


        private string _startMonth;
        public string StartMonth
        {
            get { return _startMonth;}
            set { _startMonth = value;}
        }

        private string _endMonth;
        public string EndMonth
        {
        get { return _endMonth;}
        set { _endMonth = value;}
        }

        private DateTime _startDay;
        public DateTime StartDay
        {
            get { return _startDay;}
            set { _startDay = value;}
        }

        private DateTime _endDay;
        public DateTime EndDay
        {
            get { return _endDay; }
            set { _endDay = value; }
        }

        public HuntingDate(){
        DateTime today = DateTime.Now;
        int year = today.Year;
        int month = today.Month;
        DateTime day = today;

        DateTime StartDate = (StartYear+StartMonth+StartDay);
        DateTime EndDate   = (EndYear+EndMonth+EndDay);

        if (month <= 6)
        {
            //string StartYear = Convert.ToString(year-1);
            string StartYear = (year - 1).ToString();
            string StartMonth = Convert.ToString(7);
            string EndYear = Convert.ToString(year);
            string EndMonth = Convert.ToString(6);
            string EndDay = Convert.ToString(30);
        }
        else if(month >= 7)

        {
            string StartYear =Convert.ToString(year);
            string StartMonth = Convert.ToString(7);
            string StartDay = Convert.ToString(1);
            string EndYear = Convert.ToString(year+1);
            string EndMonth = Convert.ToString(6);
            string EndDay = Convert.ToString(30);

        }
        }
        }

        }

我在这里发现的问题:

        DateTime StartDate = (StartYear+StartMonth+StartDay);
        DateTime EndDate   = (EndYear+EndMonth+EndDay);

问题是:这个成员被定义了不止一次

在这里红色下划线的 startyear startmonth startday & 以及其他部分。

4

3 回答 3

5

因为您已经定义了一个公共字段StartDate和一个属性StartDate,所以您不能这样做

public DateTime StartDate;

public DateTime StartDate
        {
            get { return _startDate;}
            set { _startDate = value;}
        }

如果您只打算StartDate用作财产,那么您可以摆脱公共领域,即public DateTime StartDate;.

您似乎正在针对每个公共字段定义属性。那是语法错误。您可能会摆脱所有公共字段,并且由于您没有在属性中执行任何操作,因此您可以将它们定义为:

public DateTime StartDate { get; set; }
于 2012-06-14T08:21:07.230 回答
5

好吧,看StartYear一个例子:

// Field declaration
public string StartYear;

// Property declaration with the same name
public string StartYear
{
    get { return _startYear;}
    set { _startYear = value;}
}

既然您拥有由公共属性支持的私有字段,为什么还要拥有公共字段?只需摆脱那些公共领域。

在更广泛的意义上,我不会尝试从另一个平台逐字移植代码 - 我会尝试找出意图,然后在您的 .NET 代码中以惯用的方式实现它。

于 2012-06-14T08:21:35.530 回答
1

您之前已经声明StartDate过。您正试图在显示错误的部分重新声明它。

于 2012-06-14T08:21:39.587 回答