Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Update Shorthand?

Update Shorthand?

Scheduled Pinned Locked Moved C#
questionannouncement
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    eddieangel
    wrote on last edited by
    #1

    Is there a shorthand for updating an object? Something like an initializer?

    var item = new FooViewModel { id = "test", name = "test" };

    Something like that but instead:

    Record myRecord = Context.RecordRepository.GetById(1234);

    myRecord {
    id = 123,
    name = "blah"
    }

    Cheers, --EA

    D A J 3 Replies Last reply
    0
    • E eddieangel

      Is there a shorthand for updating an object? Something like an initializer?

      var item = new FooViewModel { id = "test", name = "test" };

      Something like that but instead:

      Record myRecord = Context.RecordRepository.GetById(1234);

      myRecord {
      id = 123,
      name = "blah"
      }

      Cheers, --EA

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      No, there isn't.

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak

      1 Reply Last reply
      0
      • E eddieangel

        Is there a shorthand for updating an object? Something like an initializer?

        var item = new FooViewModel { id = "test", name = "test" };

        Something like that but instead:

        Record myRecord = Context.RecordRepository.GetById(1234);

        myRecord {
        id = 123,
        name = "blah"
        }

        Cheers, --EA

        A Offline
        A Offline
        Abhinav S
        wrote on last edited by
        #3

        The best you can do is try and using LINQ to fetch and update records in the same line. However, what you are tyring to do is not directly possible.

        Apps - Color Analyzer | Arctic | XKCD | Sound Meter | Speed Dial

        1 Reply Last reply
        0
        • E eddieangel

          Is there a shorthand for updating an object? Something like an initializer?

          var item = new FooViewModel { id = "test", name = "test" };

          Something like that but instead:

          Record myRecord = Context.RecordRepository.GetById(1234);

          myRecord {
          id = 123,
          name = "blah"
          }

          Cheers, --EA

          J Offline
          J Offline
          Jean A Brandelero
          wrote on last edited by
          #4

          In VB.net its called "WITH" C# dont have this trick... But, you can use this:

          /// /// C# implementation of Visual Basics With statement
          ///
          public static void With(this T _object, Action _action)
          {
          _action(_object);
          }

          Usage:

          LongInstanceOfPersonVariableName.With(x => {
          x.AgeIntVar = 21;
          x.NameStrVar = "John";
          x.NameStrVar += " Smith";
          //etc..
          });

          from http://stackoverflow.com/a/5964848[^] I dont think its the best solution... But... It may work.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups