TechGuy
TechGuy

Reputation: 4570

Identifier expected,String is a keyword

I have a php code like this,going to convert it in to 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'];

Here is my try( I tried to use enum for this)

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

Can i do thisone with enum ? i got the error "Identifier expected,String is a keyword"

Upvotes: 0

Views: 6995

Answers (6)

Rohan Büchner
Rohan Büchner

Reputation: 5403

bear i mind i don't know php from... well 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
}

I'm guesstimating you'll need a class (and not an enum)

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

     rest of code ...
  }

Upvotes: 1

Niranjan Singh
Niranjan Singh

Reputation: 18280

No, You can do this with enum. By default the underlying type of each element in the enum is int. You can specify another integral numeric type by using a colon, as shown in the example below. For a full list of possible types, see enum (C# Reference).

Example:

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

You can use struct or class type for this purpose.

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;
}

If your Year it not necessary then it can be shorten as:

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

other wise go for the full values including Year.

Upvotes: 1

Learner
Learner

Reputation: 563

I saw there are lots of answers,WHAT IS THE BEST ANSWER ?

Upvotes: -1

musefan
musefan

Reputation: 48445

No not with enum, you should use a class for this:

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

you then have further things to consider:

  1. Strings are not ideal for date type data, for this consider using DateTime - with this you can merge the year, month and day values into one property:

    public class HuntingDate
    {
        public DateTime StartDateTime;
        public DateTime EndDateTime;
    }
    
  2. Classes are used to define the structure of an object, as my example stands you would need to create an instance of the class in order to use it:

    HuntingDate huntingDate = new HuntingDate();
    

    with this you have to consider where you want to have access to it. If you need a global accessible instance you could initialise the class at a global scope level, or consider using a static class (though it should be noted that these values would be persisted across the whole application):

    public static class HuntingDate 
    {
        public static string Something;
    }
    
  3. I would strongly suggesting doing some reading on C# (get a book!) if you want to do this more seriously you should get a solid grasp of the basics of C#

Upvotes: 3

Matthew Watson
Matthew Watson

Reputation: 109822

I think you would be better off using the DateTime type for the start and end dates, and wrap it all in a class something like this:

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;
}

Upvotes: 1

Hans Kesting
Hans Kesting

Reputation: 39338

I guess you want the (string) values "StartYear", "EndYear" and so on as values of an enum. You cannot do that: an enum is always based on some integer type.

Upvotes: 1

Related Questions