2012-06-12 53 views
0

我有這樣的PHP代碼,將它轉換爲C#。標識符預期,字符串是關鍵字

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

這是我嘗試(我試圖用枚舉此)

public enum HuntingDate{string StartYear,string EndYear,string Year,string StartDate,string EndDate,string StartMonth,string EndMonth,stirng StartDay,string EndDay} 

我可以做thisone與枚舉?我得到了錯誤「標識符預期,String是一個關鍵字」

+1

這是什麼'stirng StartDay' ... – dtsg

+2

爲什麼一切都是'串'?你有這麼多有趣的數據類型可用......另外,這似乎與「enum」無關。這可能是你誤解了'enum'關鍵字。 –

回答

3

無不是與enum,你應該使用這個class

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

那麼你有進一步的事情要考慮:

  1. 字符串對於日期類型數據並不理想,因此考慮使用DateTime - 藉此,您可以將年,月和日值合併爲一個property

    public class HuntingDate 
    { 
        public DateTime StartDateTime; 
        public DateTime EndDateTime; 
    } 
    
  2. 類是用來定義對象的結構,我的例子代表你需要create an instance of the class爲了使用它:

    HuntingDate huntingDate = new HuntingDate(); 
    

    這個你必須要考慮,你想擁有訪問它。如果你需要一個全局可訪問的情況下,你可以在全球範圍內級別初始化類,或考慮使用static class(但應注意,這些值將在整個應用程序被持久化):

    public static class HuntingDate 
    { 
        public static string Something; 
    } 
    
  3. 我強烈建議做C#的一些閱讀(獲得a book!)如果你想這樣做,更嚴重的是你應該得到的C#

+0

+1作爲一個繆斯粉絲 –

+0

那麼它應該是公共類HuntingDate(){StartYear datetime}我是否正確? – TechGuy

+1

@ChathuraRanasinghe:不確定你的意思?有編輯點1作爲我最好的猜測你的問題是什麼 – musefan

1

沒有基礎知識的紮實掌握,你可以d這與枚舉。默認情況下,枚舉中每個元素的基礎類型是int。 可以通過使用一個冒號指定另一個integral numeric型,如下面的例子所示 。有關可能類型的完整列表,請參見枚舉(C# 參考)。

例子:

enum Months : byte { Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec }; 

您可以使用structclass類型用於這一目的。

public struct HuntingDate 
{ 
    int StartYear; 
    int StartMonth; 
    int StartDay;   
    int Year;    
    DateTime StartDate; // it smells like you are storing date then why do 
         // you not use DateTime rather than these.. 
    DateTime EndDate; 
    int EndYear; 
    int EndMonth;   
    int EndDay; 
} 

如果您Year它不是必需的話,就可以縮短爲:

public struct HuntingDate 
{ 
    public DateTime StartDate;       
    public DateTime EndDate; 
} 
其他

明智的去爲完整的價值觀,包括Year

1

我想你想(字符串)值「StartYear」,「EndYear」等作爲一個枚舉的值。 你不能這樣做:枚舉總是基於某種整數類型。

1

我想你會過得更好使用DateTime類型的開始和結束日期,並將其包裝都在一個班是這樣的:

public class HuntingDate 
{ 
    public HuntingDate(DateTime start, DateTime end) 
    { 
     _start = start; 
     _end = end; 
    } 

    public DateTime End 
    { 
     get 
     { 
      return _end; 
     } 
    } 

    public DateTime Start 
    { 
     get 
     { 
      return _start; 
     } 
    } 

    private readonly DateTime _start; 
    private readonly DateTime _end; 
} 
1

熊我心目中,我不知道從PHP ......那麼PHP ...

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

    ... rest of code 
} 

我guesstimating你需要一個類(而不是enum

public class HuntingDate()  
    { 
    string NameOfSelectbox; 
    DateTime endDate; 

    rest of code ... 
    } 
-1

我看到有很多答案,最好的答案是什麼?

+0

仍然我試圖做一個一個,如果任何人你能告訴我什麼是寶石我可以直接嘗試它,無論如何,我會嘗試並通知在這裏 – TechGuy

相關問題