1

I need some help in coding this in C#

I have a list of Baskets and their Global_Weights in overall Basket in Kilograms:

Basket1, Basket2, Basket3, Basket4

0.1, 0.3, 0.9,0.6

Each of the Basket consist of the following thing:

Name,Level1, level2,Local_Weights

Name: String type

Level1:Double Type

Level2:Double Type

Local_Weights:Double Type

Each Basket will consist of N number of (Name,Level1, level2,Local_Weights) and there are M Baskets N and M are variable which will be user Input

Should I be using List of list for this or something else any pointer on how to go about coding this?

How will I be accessing the members inside each of the Basket and perform calculation on them.

2
  • 1
    What is the larger problem? Depending on what you are doing there are multiple ways to correctly create the list/other structures. For instance if you are doing a sorting algorithm it my be better to use arrays for immediate access. Commented May 17, 2011 at 13:41
  • Create a Basket class and use a List or a Hashtable. Commented May 17, 2011 at 13:41

3 Answers 3

2

You would use a class Basket that contains a list of objects of the class BasketInfo and create a list of that Basket class:

class Basket
{
    public Basket()
    {
        BasketInfos = new List<BasketInfo>();
    }

    public double GlobalWeight
    {
        get
        {
            return BasketInfos.Sum(x => x.LocalWeight); 
        }
    }

    public IList<BasketInfo> BasketInfos { get; set; }
}

class BasketInfo
{
    public string Name { get; set; }
    public double Level1 { get; set; }
    public double Level2 { get; set; }
    public double LocalWeight { get; set; }
}

var baskets = new List<Basket>();
Sign up to request clarification or add additional context in comments.

Comments

2

Something like this perhaps?

class Basket
{
    private List<Payload> _payload = new List<Payload>();

    public List<Payload> Payload
    {
        get { return _payload; }
    }

    public double Weight
    { 
        get { /* calculate total weight */; }
    }
}

class Payload
{
    public string Name { get; set; }
    public double Level1 { get; set; }
    public double Level2 { get; set; }
    public double Local_Weight { get; set; }
}

And perhaps some usage, like this:

var baskets = new List<Basket>();

// Create a new basket with some payload
var newBasket = new Basket();
newBasket.Payload.Add(   
    new Payload {Name = "MyName", Level1 = 1, Level2 = 2, Local_Weight = 3});
baskets.Add(newBasket);

// Access all payloads in all baskets for example
foreach (var payload in baskets.SelectMany(basket => basket.Payload))
{
    Console.WriteLine(payload.Name);
}

Perhaps a book like Head First C# could also be of use. Good luck!

Comments

0

You could use something of this sort.

Dictionary<string, List<MyClass>> var = new Dictionary<string, List<MyClass>>();//where string key is the basket name, MyClass is a class containing properties like Name,Level1, level2,Local_Weights

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.