6 Replies - 695 Views - Last Post: 15 December 2014 - 09:43 PM Rate Topic: -----

#1 athrun5   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 161
  • Joined: 24-March 14

Made up class in a class list array

Posted 15 December 2014 - 11:17 AM

First off I am not to sure if this should be in asp.net or just C# so excuse me if I am wrong about where I post this, since I am posting about a project that uses asp.net but talking about the Classes that will make up its total code works.


My question is, if you make a class that holds many values, then you take that class and shove it into another class to define those values.

After that you make a list like this

List<Class> list = new List<Class>();

and then you take the values stored in the class of the class in the array, so you can finalize them.

A better way to think of it would be a shopping cart. Where you have a class that gets, makes and deletes items. Then you have the Cartitem class that stores the item you want and the list class which is the shopping cart class to hold that set of values.

So what I am asking is how do you get the values inside each of the classes that are in the array? I have only ever used list class with strings and ints but now I am using a class I made which is why I am having trouble thinking about what it will look like as I can't test it.

Here is my code to see what I am talking about

    public class ShoppingCart
    {
        public List<CartItem> Items { get; set; }
        CartItem itemtoadd { get; set; }

        // Removing items, adding items or figuring out the total shipping or price in general.

        public void AddItem()
        {
            Items.Add(itemtoadd);
        }

        public void RemoveItem()
        {
            Items.Remove(itemtoadd);
        }

        public void PriceTotal()
        {
            
        }


 public class CartItem
    {
        public Product product { get; set; }
        public int Quantity { get; set; }
        public double SubTotal { get; set; }

        public double Total()
        {
            SubTotal = product.Price * Quantity;
            SubTotal += product.DetermineShippingCosts();

            return SubTotal;
        }

        public int amountof(int amount)
        {
            Quantity = amount;
            return Quantity;
        }
    }

public class Product
    {
        public int ProductID { get; set; }
        public string Name { get; set; }
        public double Price { get; set; }
        public string type { get; set; }
        public int Sold { get; set; }
        public string Description { get; set; }
        public string Image { get; set; }
        public int Quantity { get; set; }
        public int Popularity { get; set; }
        public int Category { get; set; }
        public Type typy { get; set; }


I didn't show you the whole code for product as that wasn't all to needed since it only shows how I got, made and deleted my values.

Is This A Good Question/Topic? 0
  • +

Replies To: Made up class in a class list array

#2 rgfirefly24   User is offline

  • D.I.C Lover
  • member icon


Reputation: 449
  • View blog
  • Posts: 2,186
  • Joined: 07-April 08

Re: Made up class in a class list array

Posted 15 December 2014 - 11:34 AM

Since ShoppingCart.Items is a List of CartItems, you can iterate over them like you would any list.

foreach(var item in ShoppingCart.Items)
{

//Do stuff related to a specific CartItem

}



now to get at the Product all you do is this inside the foreach

item.product.ProductID



To get at a specific ProductID with out looping you can access that index in the items List like so:

ShoppingCart.items[i].product.ProductID



you will want to make sure that items[i] exists and that product is initialized or else you will get an Object reference error or an index out of bounds error.

Also since people will probably want to get at a specific product within their cart you can pull it out directly using Linq

var item = ShoppingCart.items.FirstOrDefault(i => i.product.ProductID = p_ProductID);



you could call this within a method where the ProductID of the item they want to pull up is passed in.

public Product getProductByID(int p_ProductID)


This post has been edited by rgfirefly24: 15 December 2014 - 11:41 AM

Was This Post Helpful? 1
  • +
  • -

#3 athrun5   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 161
  • Joined: 24-March 14

Re: Made up class in a class list array

Posted 15 December 2014 - 11:38 AM

I think this was very useful to what I was thinking of and now I can get a better idea of how I am to go about doing this. Thank you very much.
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw   User is online

  • blow up my boots
  • member icon

Reputation: 6549
  • View blog
  • Posts: 26,557
  • Joined: 12-December 12

Re: Made up class in a class list array

Posted 15 December 2014 - 12:01 PM

You'll need to revisit your code because, in ShoppingCart, AddItem() should typically be passed the new item to be added:
public void AddItem(CartItem newItem)

similarly for RemoveItem. In which case this is likely to be redundant:
CartItem itemtoadd { get; set; }

In CartItem your amountof() method simply returns the value (amount) that it is passed. It does also set the value of Quantity, but it still needs reviewing.

You may also want to revise CartItem to add a constructor that accepts a Product (and probably hide the default constructor), because a CartItem without a Product is unhelpful but will also cause an error (at some point..) from your ShoppingCart. That is, if you try to reference the Product of a CartItem when the Product hasn't been set, as rgfirefly24 alludes to.

This post has been edited by andrewsw: 15 December 2014 - 12:02 PM

Was This Post Helpful? 1
  • +
  • -

#5 athrun5   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 161
  • Joined: 24-March 14

Re: Made up class in a class list array

Posted 15 December 2014 - 03:19 PM

Thanks for your help both of you, I am now able to start doing my css design so that after I am done that I can do testing on my classes. So thanks very much.

This post has been edited by athrun5: 15 December 2014 - 03:19 PM

Was This Post Helpful? 0
  • +
  • -

#6 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5103
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Made up class in a class list array

Posted 15 December 2014 - 04:41 PM

View Postandrewsw, on 15 December 2014 - 01:01 PM, said:

You'll need to revisit your code because, in ShoppingCart, AddItem() should typically be passed the new item to be added:
public void AddItem(CartItem newItem)

similarly for RemoveItem. In which case this is likely to be redundant:
CartItem itemtoadd { get; set; }

In CartItem your amountof() method simply returns the value (amount) that it is passed. It does also set the value of Quantity, but it still needs reviewing.


QFT. The way you're currently doing it, you're adding "state" where there was none previously. Instead of the call being "atomic", you have to make another pre-call. If you don't, you could be re-adding a previously stored value, or worse trying to add a null reference, which can be added to a collection. You wouldn't see the problem until later, when you tried to inspect that value (making it difficult to debug, because you might not know where that came from).

Make your methods as atomic as possible. Give them the values they need to do their job as parameters, not as some stateful property on the class itself.
Was This Post Helpful? 0
  • +
  • -

#7 athrun5   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 161
  • Joined: 24-March 14

Re: Made up class in a class list array

Posted 15 December 2014 - 09:43 PM

Don't worry I made some much need changes and I have already planned out everything I am going to do with the code so there won't be any null values. Thanks for your warning though as it would be very hard to debug as I don't do that very well at the moment but I am improving.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1