0

got a tricky one here me thinks!

I want to understand how if this Cart object gets passed by value using the model binder, then how can it effect the actual value in the session?

  public RedirectToRouteResult AddToCart(Cart cart, int productId, string returnUrl) {
        Product product = repository.Products
            .FirstOrDefault(p => p.ProductID == productId);

        if (product != null) {
            cart.AddItem(product, 1);
        }
        return RedirectToAction("Index", new { returnUrl });
    }

Hope this explains my issue!

Thanks for any assistance

details

this bold code doesn't effect the customprofile in session

   [AllowAnonymous]
    [HttpPost]
    public ActionResult Register(RegisterModel model, CustomProfile customProfile)
    {
        if (User.Identity.IsAuthenticated)
            return RedirectToAction("Index", "Home");
        if (ModelState.IsValid)
        {
            // Attempt to register the user
            MembershipCreateStatus createStatus;


            membershipProvider.CreateUser(model, null, null, true, null,
                                          out createStatus);

            if (createStatus == MembershipCreateStatus.Success)
            {
                const string message = "You have successfully reigestered, and you are now logged in.";
                authProvider.SetAuthCookie(model.UserName, false /* createPersistentCookie */);

                var newCustomProfile = new CustomProfile(model.UserName);
                var db = new CrowdFundingDB();

                **customProfile = customProfileProvider.Create(newCustomProfile);**
Nikos
  • 7,295
  • 7
  • 52
  • 88
  • 1
    It is unclear what you're asking. – Dennis Traub Nov 26 '12 at 17:36
  • Basically if its passed by value, its my understanding that it creates a copy of the original value, and then if this copy is modified it won't effect the original value in the session. – Nikos Nov 26 '12 at 17:43

1 Answers1

1

Cart is not a value type, it's a reference type.

Passing it as an argument doesn't make a copy of the object, the method receives a copy of the reference to the actual object. It will access the same object through the copied reference.

See also: ByRef vs ByVal Clarification

Community
  • 1
  • 1
Dennis Traub
  • 50,557
  • 7
  • 93
  • 108
  • thx, what I dont get is how this is pass by reference: public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { // get the Cart from the session Cart cart = (Cart)controllerContext.HttpContext.Session[sessionKey]; // create the Cart if there wasn't one in the session data if (cart == null) { cart = new Cart(); controllerContext.HttpContext.Session[sessionKey] = cart; } // return the cart return cart; } – Nikos Nov 26 '12 at 21:49
  • so how come this doesn't effect the custom profile in session: public ActionResult Register(RegisterModel model, CustomProfile customProfile) { customProfile = customProfileProvider.Create(newCustomProfile); – Nikos Nov 26 '12 at 22:09
  • @Quantum please post your code in your question. I'm not able to read it in the comments. – Dennis Traub Nov 26 '12 at 22:20