0

I have a number of objects in my project which have fields that will contain matching values when they're initialized - see the code snippet below for an example, in particular the AddedDateTime and ModifiedDateTime properties:

User user = new User()
{
    FirstName = "Tex",
    Surname = "Murphy",
    AddedDateTime = DateTime.Now,
    ModifiedDateTime = DateTime.Now
};

Is it possible to set the value of ModifiedDateTime to the value of AddedDateTime, within the {...} block once it has been set?

I have a feeling it's not possible but thought it worth asking for confirmation.

Mathew Thompson
  • 55,877
  • 15
  • 127
  • 148
Bern
  • 7,808
  • 5
  • 37
  • 47
  • 1
    Why don't you try and let us know?! – banging May 03 '12 at 18:44
  • 1
    What happens if you do `ModifiedDateTime = AddedDateTime`? – Mathew Thompson May 03 '12 at 18:44
  • You can also try `AddedDateTime = ModifiedDateTime = DateTime.Now` if you'd like.. – banging May 03 '12 at 18:45
  • Tried it and it won't compile. The compiler throws the following error : `The name 'AddedDateTime' does not exist in the current context` – Bern May 03 '12 at 18:46
  • 1
    why don't you initialize that in the constructor of `User`? – Daniel A. White May 03 '12 at 18:47
  • I could but I want to keep the code as short as possible, and would rather avoid having to call the same property/method (in this case DateTime.Now) more than once. The thing is if I wasn't using object initialization as shown above then I could do it but I want the best of both worlds. – Bern May 03 '12 at 18:52
  • @banging - doesn't work, same error thrown at compile time. – Bern May 03 '12 at 18:52

5 Answers5

1

I'm not sure if this what your after or not, but is this appropriate?

var now = DateTime.Now;
User user = new User
{
     FirstName = "Tex",
     Surname = "Murphy",
     AddedDateTime = now,
     ModifiedDateTime = now
};

If you want to have them to initialize to the same value then you must set to a common variable value.

Reddog
  • 15,219
  • 3
  • 51
  • 63
  • Based on the responses (thanks everyone) it seems like this is the only viable option - not the answer I was hoping for but the next best thing. I thought this might be the case. Real shame the .Net object initializer doesn't allow you to get the value of a property that has been set within the same initialization block. Thanks @Reddog – Bern May 03 '12 at 19:03
1

You could maybe put this logic into the implementation of the properties:

public class User{
  private DateTime added;
  private DateTime? modified;

  string FirstName {get;set;}
  string SurName {get;set;}

  DateTime AddedDateTime { 
   get { return added; } 
   set { added = value;
         modified = modified ?? value;
       }
   }

  DateTime? ModifiedDateTime {
    get { return modified }
    set { modified = value; }
  }
}
Sheldon Warkentin
  • 1,706
  • 2
  • 14
  • 31
0
User user = new User
            {
                FirstName = "Tex",
                Surname = "Murphy",
                AddedDateTime = DateTime.Now < Something? DateTime.Now : SomeOtherValue,
                ModifiedDateTime = DateTime.Now < Something? DateTime.Now : SomeOtherValue
            };
Mathew Thompson
  • 55,877
  • 15
  • 127
  • 148
  • Not really what I'm aiming for. I want to set AddedDateTime as it is then set ModifiedDateTime to AddedDateTime. That's if it's possibe. – Bern May 03 '12 at 18:49
0

As mentioned in a comment, you could make constructors that contain just the data that you need, i.e.:

public User(string firstName, string sirname)
{
    FirstName = firstName,
    Surname = sirname,
    AddedDateTime = DateTime.Now,
    ModifiedDateTime = DateTime.Now
};

You could also have helper methods to create instances of the class. They could be inside of the class or not (this is useful if you can't control the class itself)

public static User CreateUser(string firstName, string sirname)
{
    User newUser = new User();
    newUser.FirstName = firstName,
    newUser.Surname = sirname,
    newUser.AddedDateTime = DateTime.Now,
    newUser.ModifiedDateTime = DateTime.Now

    return newUser;
};

You could also have something like:

public static User CreateUser(string firstName, string sirname, string otherdata)
{
    User newUser = new User();
    newUser.FirstName = firstName,
    newUser.Surname = sirname,
    newUser.AddedDateTime = DateTime.Now,
    newUser.ModifiedDateTime = DateTime.Now,
    newUser.SomeField = otherdata,
    newUser.SomeOtherField = otherdata

    return newUser;
};
Servy
  • 202,030
  • 26
  • 332
  • 449
  • my aim is to keep the code as abbreviated as possible, and avoid calling DateTime.Now twice, as ideally I want it's value to be reflected in ModifiedDateTime on initialisation. Sorry your above suggestions are appreciated but don't address the issue. – Bern May 03 '12 at 18:56
  • How don't they fix the issue? With the first two of these methods you don't need to enter `DateTime.Now` even once, let alone twice. The third code snippet covers the general case where you have some piece of data not common among every create object, but that gets set to two (or more) properties. If this wouldn't solve your exact problem, then perhaps you need to provide an example of what you want the finished product to be able to do. – Servy May 03 '12 at 19:00
  • On answer to your first question you're responses are not addressing the initial problem I raised which is to avoid accessing the DateTime.Now property twice. – Bern May 04 '12 at 07:51
  • Why do you assume that it's a problem to access DateTime.Now twice? Do you think accessing the current time is an expensive operation? It's not. Given any of these methods you can trivially store the datetime in a variable, or simply set the added date to the modified date the way that you couldn't in an object initializer, or you can make it a single parameter that is set to two fields. I intentionally showed you the general case so that you could extrapolate the details for yourself. That way you'd be able to solve this problem every time it came up, rather than just copy/pasting my code. – Servy May 04 '12 at 13:42
0

You could put logic into the setter of your AddedDateTime that checked for a minimum value on the ModifiedDateTime and set accordingly. e.g.

public DateTime ModifiedDateTime { get; set; }
private DateTime _addedDateTime;
public DateTime AddedDateTime {
    get { return _addedDateTime; }
    set 
    {
        if (ModifiedDateTime == DateTime.MinValue)
        {
            ModifiedDateTime = _addedDateTime = value;
        };
    }
}

...

var test = new Test 
{ 
   AddedDateTime = DateTime.Now
};
George Johnston
  • 31,652
  • 27
  • 127
  • 172