我有这样的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&amp;还有其他部分。
答案 0 :(得分:5)
由于您已定义公开字段StartDate
和属性StartDate
,因此无法执行此操作
public DateTime StartDate;
public DateTime StartDate
{
get { return _startDate;}
set { _startDate = value;}
}
如果您只想使用StartDate
作为属性,那么您可以摆脱公共字段,即public DateTime StartDate;
。
您似乎正在针对每个公共字段定义属性。这是一个语法错误。您可以删除所有公共字段,因为您没有在属性中执行任何操作,您可以将它们定义为:
public DateTime StartDate { get; set; }
答案 1 :(得分:5)
以StartYear
为例:
// Field declaration
public string StartYear;
// Property declaration with the same name
public string StartYear
{
get { return _startYear;}
set { _startYear = value;}
}
为什么你有公共字段,因为你还获得了公共属性支持的私有字段?摆脱那些公共领域。
更广泛地说,我不会尝试从另一个平台执行逐字的代码端口 - 我会尝试找出意图,然后在您的内容中以惯用的方式实现.NET代码。
答案 2 :(得分:1)
您之前已经宣布过StartDate
。您正试图在显示错误的部分重新声明它。