I am creating instance of the same object to populate its properties twice. I need to give this object to another developer who just don't need constructor at all, as he is sending me object through WCF service.
var a = new Earth();
a.ID= 11;
a.UpdateUniverse= true;
a = new Earth(a); // use age and Taxrate to populate rest of Earth's properties
// and call dataAccess as well to get some records
ManagerEarth.Add(a);
Here is the class you want to see:
[DataContract(Name = "Earth")]
public class Earth
{
[DataMember]
public int ID;
[DataMember]
public int PlanetID;
[DataMember]
public string PlanetUrl;
[DataMember]
public string UserID;
[DataMember]
public string Environment;
[DataMember]
public string Status;
[DataMember(Name = "StartDate")]
public string StartDateString
{
get
{
if(this.StartDate.HasValue)
return this.StartDate.Value.ToString(Utils.C_DateFormat);
else
return null;
}
set
{
this.StartDate = Utils.ToNullableDateTime(value);
}
}
public DateTime? StartDate;
[DataMember(Name = "EndDate")]
public string EndDateString
{
get
{
if(this.EndDate.HasValue)
return this.EndDate.Value.ToString(Utils.C_DateFormat);
else
return null;
}
set
{
this.EndDate = Utils.ToNullableDateTime(value);
}
}
public DateTime? EndDate;
[DataMember]
public bool UpdateUniverse;
[DataMember]
public string AuditAddress;
public Earth()
{
}
public Earth(Earth m)
{
if(SPContext.Current == null || SPContext.Current.Web == null)
return;
var planetX = new EarthDataAccess().GetByURL(SPContext.Current.Web.Site.Url);
if(planetX == null || planetX.PlanetID < 1)
return;
this.PlanetID = planetX.PlanetID;
this.PlanetUrl = planetX.PlanetHomeURL;
this.AuditAddress = planetX.GoHomeUrl;
this.ID = m.ID;
this.StartDate = m.StartDate;
this.EndDate = m.EndDate;
this.Status = !string.IsNullOrEmpty(m.Status) ? m.Status : "Active";
if(this.Status != "Destroyed" && this.EndDate != null)
this.Status = "Ended";
this.Environment = !string.IsNullOrEmpty(m.Environment) ? m.Environment : "1";
this.UserID = m.UserID;
this.UpdateUniverse = m.UpdateUniverse;
}
}
Some other methods:
public static string C_DateFormat = "dd/MM/yyyy HH:mm:ss";
public static DateTime? ToNullableDateTime(string dateTime)
{
if(string.IsNullOrEmpty(dateTime))
return (DateTime?) null;
var ci = CultureInfo.InvariantCulture;
var formats = new[] { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "M-d-yyyy", "dd-MM-yyyy", "dd/MM/yyyy", "dd/MM/yyyy HH:mm:ss", "MM-dd-yyyy", "M.d.yyyy", "dd.MM.yyyy", "MM.dd.yyyy" }
.Union(ci.DateTimeFormat.GetAllDateTimePatterns()).ToArray();
var dt = DateTime.ParseExact(dateTime, formats, ci, DateTimeStyles.None);
return dt;
}