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"
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:
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;
}
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;
}
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#
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.
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;
}
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.
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 ...
}
I saw there are lots of answers,WHAT IS THE BEST ANSWER ?
Related
I have an object that represents a scheduled payment. My database has a list of these payments, but I have one instance of a payment.
I need to write a method that gets the next payment after the one I have, as well as the previous date of the previous payment.
I'd like to write a method that return the two dates. But the return type of 'DateTime' only allows for one. I could return a List<DateTime> but that seems strange and ma be ambiguous. Which is the previous and which is the next?
I can also create a DTO object that has:
DateTime previousPayment {get; set;}
DateTime nextPayment {get; set;}
Tuple<DateTime, DateTime> might be another options, but it too is ambiguous. Unless I can name the properties of it?
But - is there a better way to allow for a method to return two dates? Anonymous types or something?
Use the "ref" modifier. (You may use "out" instead if you do not need to read the variable before it is assigned)
public void GetNextPayment(ref DateTime previousPayment, ref DateTime nextPayment){
// do stuff here
}
Usage:
DateTime previousPayment = DateTime.Now(); //Example
DateTime nextPayment = DateTime.Now(); // example
GetNextPayment(ref previousPayment, ref nextPayment); // Forgot to add "ref" when calling it
previousPayment and nextPayment will be modified in the function and maintain the value.
Update with Dictionary
As Anik mentioned, it might be better to use a Dictionary;
public Dictionary<string,DateTime> GetNextPayment(DateTime previousPayment, DateTime nextPayment){
// modify payments
Dictionary<string,DateTime> myDict = new Dictionary(string, DateTime);
myDict.Add("PreviousPayment", [date]);
myDict.Add("NextPayment", [date]);
return myDict;
}
Use Class
Ilya. N. mentioned to use a class. I would have to agree with this if you're going to have lots of payment objects going to be used more than once. But I firmly believe it's better to give you all the tools available at your disposal because you never know when you might want to use out parameters or Dictionaries.
public class Payment {
public string Name {get;set;}
public DateTime previousPayment {get;set;}
public DateTime nextPayment {get;set;}
public GetNextPayment(){
// code to get the next payment
this.previousPayment = //whatever
this.nextPayment = //whatever
}
}
If you only have ONE payment you are going to be using, like ever. (good to future proof with a class), then you might use a method or dictionary.
Besides the two options you listed, there are two more:
Return a Tuple<DateTime, DateTime>
Use out parameters
Why not simply return a class?
public class DateCombo {
DateTime PreviousPayment {get; set;}
DateTime NextPayment {get; set;}
}
try this ...
private void Form1_Load(object sender, EventArgs e)
{
DateTime previousPayment =new DateTime();
DateTime nextPayment=new DateTime();
getdate(ref previousPayment, ref nextPayment);
}
public void getdate(ref DateTime previousPayment, ref DateTime nextPayment)
{
previousPayment = System.DateTime.Now;
nextPayment = System.DateTime.Now.AddDays(1);
}
How do I Write C# code that will allow to compile the following code :
var date = 8.September(2013); // Generates a DateTime for the 8th of September 2013
You can use an extension method:
public static class MyExtensions
{
public static DateTime September(this int day, int year)
{
return new DateTime(year, 9, day);
}
}
However, this is generally bad practice, and I'd recommend against this kind of thing, especially for something as trivial as this—is new DateTime(2013, 9, 8) really so much more difficult than 8.September(2013)? There may be times where this kind of trick can be useful or fun for practice, but it should be used sparingly.
I would recommend against this, as it strikes me as very poor style. That said, if you really want to do this statically, you would need to define twelve different extension methods (one for each month name) like so:
public static class DateConstructionExtensions
{
public static DateTime January(this int day, int year)
{
return new DateTime(year, /* month: */1, day);
}
// equivalent methods for February, March, etc...
}
You could do this via extensions:
public static DateTime September(this int day, int year) {
return new DateTime(year, 9, day);
}
Of course, you'd need 12 such extensions, one for each month.
I think you can have the implementation like this:
public partial interface IMonth {
int Number {
get;
}
}
public partial class February: IMonth {
public int Number {
get {
return 2;
}
}
}
public static partial class Extensions {
public static DateTime OfMonth<T>(this int day, int year)
where T: IMonth, new() {
var month=new T();
var daysInMonth=DateTime.DaysInMonth(year, month.Number);
if(1>day||day>daysInMonth)
throw new ArgumentException();
return new DateTime(year, month.Number, day);
}
}
For the reason I declare months as classes, is because months might have different names in different cultures. You might want to provide different aliases for them.
Then, for the reason there's IMonth, is a contract that months must implement it. The extension method has the constraint new() is for avoiding IMonth itself or an abstract class be used.
This implementation also checks for the valid day number.
And you can assign the variable date as:
var date=(28).OfMonth<February>(2013);
Make sense?
I am attempting to move validation of input data into the get;set; of a class struct.
public void PlotFiles()
{
List<DTVitem.item> dataitems;
DTVitem.item i;
DateTime.TryParse("2012/01/01", out i.dt);
DateTime.TryParse("04:04:04", out i.t);
int.TryParse("455", out i.v);
dataitems.Add(i);
}
The struct is declared in a separate class (probably unnecessary):
public partial class DTVitem
{
public struct item
{
public DateTime dt;
public DateTime t;
public int v;
}
}
Every time I set DTVitem.item.dt, DTVitem.item.t, or DTVitem.item.v, I wish it to perform the relevant .TryParse() to validate the property contents.
However, when I attempt to use TryParse() as follows (attempting to wrap my head around this example from MSDN):
public partial class DTVitem
{
private DateTime _datevalue;
public string dt
{
get { return _datevalue; }
set { DateTime.TryParse(value, out _datevalue) ;}
}
}
I receive the error that _datevalue is a DateTime and cannot be converted to a string. The reason is obviously that the return path must return the type of dt in this instance (a string). However, I have attempted to massage this a few different ways, and am not able to hack it.
How do I achieve my goal of validating a string value as a DateTime when setting it as a property of an instance of the struct?
Is using set as I am attempting to the best way?
I can see that there is a lot of value in using get;set; for validation and would really like to understand it.
Thanks very much,
Matt
[edit]
Thanks to Jon Skeet below for pointing out the err of my ways.
Here's another thread on problems with mutable structs, and another speaking about instantiating a struct. Note structs are value types.
I believe the rest of what he was pointing out is sort of just agreeing that burying the struct way far away isn't necessary, and I should review why I'm doing it.
[solution]
I've taken into account some recommendations below and come up with the following:
public partial class DTVitem
{
private DateTime _dtvalue, _tvalue;
private int _vvalue;
public string dt
{
get { return _dtvalue.ToString(); }
set { DateTime.TryParse(value, out _dtvalue); }
}
public string t
{
get { return _tvalue.ToString(); }
set { DateTime.TryParse(value, out _tvalue); }
}
public string v
{
get { return _vvalue.ToString(); }
set { int.TryParse(value, out _vvalue); }
}
}
Inside my program class, I've instantiated and set with the following:
DTVitem item = new DTVitem();
item.dt = "2012/01/01";
item.t = "04:04:04";
item.v = "455";
So I opted not to use a struct, but a class; or really an instance of the class.
A property can only have one type. If you want the property to be of type string, then you can implement it this way:
public partial class DTVitem
{
private DateTime _datevalue;
public string dt
{
get { return _datevalue.ToString(); }
set { DateTime.TryParse(value, out _datevalue) ;}
}
}
However, using TryParse() will mean that the setter will not throw an exception if the DateTime is invalid. If you want it to do this, use DateTime.Parse() instead.
public partial class DTVitem
{
private DateTime _datevalue;
public string dt
{
get { return _datevalue.ToString(); }
set { DateTime.TryParse(value, out _datevalue) ;}
}
}
You're just missing a type convertion in the get. _datevalue is a DateTime but your property's a string.
get { return _datevalue.ToString(); } //or .toShortDateString() or ToShorttimeString()
The get;set; must have the same type. Your get returns a datetime when it expects a string, hence the error.
just use an explicit method bool setDate(String datestring) and put your code there. You can return a bool from the tryparse to let you know if it was successful.
Other (design mostly) issues aside, just getting to the problem of returning _datevalue as string, you can simply do something like:
public string dt
{
get { return _datevalue.ToString(); }
set { if(!DateTime.TryParse(value, out _datevalue)) /* Error recovery!! */ ;}
}
>>> You may also want to check the docs for DateTime.ToString() and see what format you want to get your string in when accessing the property.
I have a system that takes information from an external source and then stores it to be displayed later.
One of the data items is a date. On the source system they have the concept of a fuzzy date i.e. not accurate to a specific day or sometimes not to a month as well. So I get dates in the format:
dd/mm/yyyy
mm/yyyy
yyyy
I can parse these to DateTime objects and work with these but when rendering later I need to be able to determine the accuracy of the date since parsing "2010" will result in a date of "01/01/2010". I want to show just the year so need to know it's original accuracy.
I've mocked up a quick class to deal with this:
public class FuzzyDate
{
public DateTime Date { get; set; }
public DateType Type { get; set; }
}
public enum DateType
{
DayMonthYear,
MonthYear,
Year
}
This will do the job for me and I can do something on the parse to handle it but I feel like this is probably quite a common problem and there is probably an existing cleaner solution.
Is there something built into .Net to do this? I had a look at the culture stuff but that didn't quite seem right.
Any help would be appreciated.
To answer your question: There is nothing built into .NET to handle this gracefully.
Your solution is as valid as any I've seen. You will probably wish to embellish your class with overrides to the ToString() method that will render your date appropriately based on the DateType.
Here are a couple other threads that attempt to address this question:
Strategy for Incomplete Dates
Implementing a "Partial Date" object
Good luck!
If your data type will always handle specific periods of time (i.e. the year 1972 is a specific period of time, but the 4th of July is not specific), you can store your data as a start time and time span.
If your date was "1972", the start date would be 19720101 and the time span would be 366 days.
If your date was "07/1972", the start date would be 19720701 and the time span would be 31 days.
If your date was "04/07/1972", the start date would be 19720704 and the time span would be 1 day.
Here's a possible implementation:
public struct VagueDate
{
DateTime start, end;
public DateTime Start { get { return start; } }
public DateTime End { get { return end; } }
public TimeSpan Span { get { return end - start; } }
public VagueDate(string Date)
{
if (DateTime.TryParseExact(Date, "yyyy", null, 0, out start))
end = start.AddYears(1);
else if (DateTime.TryParseExact(Date, "MM/yyyy", null, 0, out start))
end = start.AddMonths(1);
else if (DateTime.TryParseExact(Date, "dd/MM/yyyy", null, 0, out start))
end = start.AddDays(1);
else
throw new ArgumentException("Invalid format", "Date");
}
public override string ToString()
{
return Start.ToString("dd/MM/yyyy") + " plus " + Span.TotalDays + " days";
}
}
As I started to read your problem, I rapidly came to the conclusion that the answer was to implement your own FuzzyDate class. Lo and behold, that's exactly what you've done.
I can imagine that you might want to add functionality to this over time (such as comparisons that take into account the DateType).
I don't believe there's anything that will inherently help you in the .NET Framework, so I think you're doing the right thing.
I think you're going down the right route. There is no concept of a 'fuzzy' date or partial date, you will need to build your own.
You will likely need more constructor methods, for example
public FuzzyDate(int year)
{
Date = new DateTime(year,1,1); // 1 Jan yy
Type = DateType.Year;
}
public FuzzyDate(int year, int month)
{
Date = new DateTime(year, month, 1); // 1 mm yy
Type = DateType.MonthYear;
}
public FuzzyDate(int year, int month, int day)
{
Date = new DateTime(year, month, day); // dd mm yy
Type = DateType.DayMonthYear;
}
Hope this helps,
Kevin
It seems to me that your approach is right. Its true that .NET DateTime support multiple formats but I guess that given that all of them are supported with a concept of steps (nanoseconds), then will be related to specific date AND time.
One thing I would do differently is use null-able values (or use -1 for null semantics) for month and day to indicate what data was collected. Then I would have a factory method that would take a DateType param and return a DateTime. This method would throw and exception if only the year was available and the client code tried to create a DateType.DayMonthYear.
public class FuzzyDate
{
int _year;
int? _month;
int? _day;
public DateTime Date { get; set; }
public DateType Type { get; set; }
public DateTime GetDateTime(DateType dateType) { // ...
}
public enum DateType
{
DayMonthYear,
MonthYear,
Year
}
This might seem a bit over the top but the approach would explicitly store the original data and only represent "faked" DateTime objects when requested. If you were to persist a DateTime object internally along with a DateType enum you would lose some resolution.
As far as I am aware there is nothing built into .NET for this, the solution I'd go for is one based upon nullable values, something like this.
public class FuzzyDate
{
private int Year;
private int? Month;
private int? Day;
public FuzzyDate(int Year, int? Month, int? Day)
{
this.Year = Year;
this.Month = Month;
this.Day = Day;
}
public DateType DateType
{
get
{
if(Day.HasValue && Month.HasValue)
{
return DateType.DayMonthYear;
}
else if(Month.HasValue)
{
return DateType.MonthYear;
}
else
{
return DateType.Year;
}
}
}
public DateTime Date
{
get
{
return new DateTime(Year, Month.GetValueOrDefault(1), Day.GetValueOrDefault(1));
}
}
}
public enum DateType
{
DayMonthYear,
MonthYear,
Year
}
You could create your own structure (user-defined type) based on the datetime that would allow 00 for month, and 00 for day... And then also implement icomparable, so you can do math/comparrisons on it.
http://msdn.microsoft.com/en-us/library/k69kzbs1%28v=vs.71%29.aspx
http://msdn.microsoft.com/en-us/library/system.icomparable.aspx
I have this enum:
public enum TimePeriod
{
Day = DateTime.Now.AddDays(-1),
Week = DateTime.Now.AddDays(-7),
Month = DateTime.Now.AddMonths(-1),
AllTime = DateTime.Now
}
but cant do
(DateTime)timePeriod
how can i get the date given an enum?
In C#, the default underlying type for an enum is int, and unfortunately is only support basic data types like int, short, uint and so on. Therefore, storing the DateTimes inside an enum is not possible in .NET.
You can of course make a static class with static properties that expose the DateTimes you need instead of making it like an enum like this:
public static class TimePeriod
{
public static DateTime Day{ get{return DateTime.Now.AddDays(-1);}},
public static DateTime Week{ get{return DateTime.Now.AddDays(-7);}},
public static DateTime Month{ get{return DateTime.Now.AddMonths(-1);}},
public static DateTime AllTime{ get{return DateTime.Now;}},
}
And use it like this:
myDateTime = TimePeriod.Month;
In addition to Oyvind's answer. Enums Values have to be constant values and not variables.
You should use a class, not an enum for this.