此成员被多次定义,但为什么

本文关键字:为什么 定义 成员 | 更新日期: 2023-09-27 18:37:03

我有这样的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);

问题是:此成员被多次定义

在这里,红色下划线开始年份开始月份开始日和其他部分。

此成员被多次定义,但为什么

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

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

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

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

public DateTime StartDate { get; set; }

StartYear为例:

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

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

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

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

相关文章: