Click here to Skip to main content
16,015,623 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
HI
I Use Asp.net core MVC 2 PDF for Learn asp.net core 2

in this book Used Session for store List Of product in the Card. Now i have two codes with the following :

first code :
C#
private List lineCollection = new List();

second :
C#
Public List lineCollection = new List();


when i Used Firs Code (private) all of Things work Correctly ... But I don't know what happened when i Use Second Code (public) List not work correctly .. when i click Button "add product to cart" then one product is added to list correct . but when i want to see List of CartLine in the index of CartController i see two product in the list but i add just one product no two product or when i want to remove that product from list it does not Removed from List ...

the complete Class Cart :
C#
public class Cart
    {
        
       // public List lineCollection = new List();
        private List lineCollection = new List();
        public virtual void AddItem(Product product, int quantity)
        {
            CartLine line = lineCollection
            .Where(p => p.Product.ProductID == product.ProductID)
            .FirstOrDefault();
            if (line == null)
            {
                lineCollection.Add(new CartLine
                {
                    Product = product,
                    Quantity = quantity
                });
            }
            else
            {
                line.Quantity += quantity;
            }
        }

        //Remove All Products in Cart
        public virtual void RemoveLine(Product product)
            => lineCollection.RemoveAll(l => l.Product.ProductID == product.ProductID);
        //Compute Total in Cart
        public virtual decimal ComputeTotalValue()
            => lineCollection.Sum(e => e.Product.Price * e.Quantity);

        public virtual void Clear() => lineCollection.Clear();

        public virtual IEnumerable Lines => lineCollection;
    }


and the complete Controller Class Cart:
C#
public class CartController : Controller
    {

        private IProductRepository repository;
        private Cart Cart;
        public CartController(IProductRepository repo,Cart cart)
        {
            repository = repo;
            Cart = cart;
        }

        public ViewResult Index(string returnUrl)
        {
            return View(new CartIndexViewModel
            {
                //Cart = GetCart(),
                Cart = Cart,
                ReturnUrl = returnUrl
            });
        }
        public RedirectToActionResult AddToCart(int productId,string returnUrl)
        {
            Product product = repository.Products.FirstOrDefault(p=>p.ProductID==productId);
            if (product!=null)
            {
                //Cart cart = GetCart();
               
                //cart.AddItem(product, 1);
               // SaveCart(cart);
               Cart.AddItem(product, 1);
            }
            return RedirectToAction("Index", new { returnUrl });
        }

        public RedirectToActionResult RemoveFromCart(int productId,string returnUrl)
        {
            Product product = repository.Products
            .FirstOrDefault(p => p.ProductID == productId);
            if (product != null)
            {
                //Cart cart = GetCart();
                //cart.RemoveLine(product);
                //SaveCart(cart);
                Cart.RemoveLine(product);
            }
            return RedirectToAction("Index", new { returnUrl });
        }
     

    }


and Session Extension class:
C#
public static class SessionExtensions
    {
        public static void SetJson(this ISession session,string key,object value)
        {
            session.SetString(key, JsonConvert.SerializeObject(value));

        }

        public static T GetJson(this ISession session,string key)
        {
            var sessiondata = session.GetString(key);
            return sessiondata == null ? default(T) : JsonConvert.DeserializeObject(sessiondata);
        }
    }


...................... ??? Why ??? i dont know what is it happen between private and Public in this piece of code

What I have tried:

i just changed Public to private and all of code work correct i don't know why?
Posted
Updated 23-Nov-17 3:34am

If you declare anything (not just a List, anything: an integer, a method, even a whole class) as private then it is only available inside the class in which it is defined - the world outside the class cannot see it, or access it directly in any way.
C#
public class MyClass
    {
    private List<string> myList = new List<string>();
    public string GetAt(int index)
        {
        if (index >= 0 && index < myList.Count)     // Fine, it's in the same class.
            {
            return myList[index];                   // Fine, it's in the same class.
            }
        return null;
        }
    }
public class MyOtherClass
    {
    public void DoSomething()
        {
        MyClass mc = new MyClass();                 // Fine, MyClass is public
        mc.GetAt(1);                                // Fine, MyClass.GetAt is public
        mc.myList.Add("Hello");                     // Error: "'GeneralTesting.frmMain.MyClass.myList' is inaccessible due to its protection level"
        }
    }

When you declare something as public the whole world can see and access it.

There are other access modifiers (internal, protected , and protected internal but you'll meet those later.
 
Share this answer
 
Tanks for Answer

i know about puplic,private,protect .....

But in my Code i Never Use List<cartline> in other Class i Just Used Them in itself class

look my sample code : PackagesStore
 
Share this answer
 
Comments
Richard Deeming 23-Nov-17 13:59pm    
If you want to reply to a solution, click the "Have a Question or Comment?" button under the solution.

DO NOT post your comment as a new solution!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900