Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have this code below:
Foood[] food = new Foood[types];
            food[0] = new Foood("protein", 2.0);
            food[1] = new Foood("vitamins", 3.0);
            food[2] = new Foood("Carbonhydrate", 4.0);

and then this
List<Foood> foods = new List<Foood>();

     foods.Add(food[0]);
     foods.Add(food[1]);
     foods.Add(food[2]);



and then this
for (int i = 0; i < days; i++)
{

    UpdateCalories(foods);

}



public void UpdateCalories(List<Foood> foodItem)
        {

            for (int i = 0; i < foodItem.Count; i++)
            {
                foodItem[i].calories = foodItem[i].calories + 1.1;
            }
        }


i want to be able to have values for different days before printing

What I have tried:

I have written and pasted the code
got this:
Day0
protein,5.3
vitamins,6.3
Carbonhydrate,7.3

Day1
protein,5.3
vitamins,6.3
Carbonhydrate,7.3

Day2
protein,5.3
vitamins,6.3
Carbonhydrate,7.3

Day3
protein,5.3
vitamins,6.3
Carbonhydrate,7.3
----------------------------------------

But I want this:
Day0
protein,2.0
vitamins,3.0
Carbonhydrate,4.0

Day1
protein,3.1
vitamins,4.1
Carbonhydrate,5.1

Day2
protein,4.2
vitamins,5.2
Carbonhydrate,6.2

Day3
protein,5.3
vitamins,6.3
Carbonhydrate,7.3

-----------------

I still want to have access to the values of the previous days not just the last day.

this is my output code
----------------------------
C#
using (StreamWriter sw = new StreamWriter(@"U:\Food\food.txt"))
{
	sw.WriteLine("Day" + "{0}", 0);
	for (int ii = 0; ii < types; ii++)
	{
		sw.WriteLine("{0},{1}", food[ii].Type, food[ii].Calories);
	}
	sw.WriteLine("");
	for (int i = 0; i < days; i++)
	{
		{
			sw.WriteLine("Day" + "{0}", i + 1);
			for (int ii = 0; ii < types; ii++)
			{
				sw.WriteLine("{0},{1}", food[ii].Type, food[ii].Calories);
			}
			sw.WriteLine("");
		}
	}
}
Posted
Updated 31-Mar-17 13:22pm
v5
Comments
CHill60 31-Mar-17 17:15pm    
Your question is not clear. Use the Improve question link to describe your problem
[no name] 31-Mar-17 17:17pm    
You have already been told what to do. A bit of logical thinking will help you. If you update all of your objects at the same time, what else would you expect to happen?
imeyal0 31-Mar-17 17:19pm    
please help and assist. I also tried to update at different days but the result is the same
j snooze 31-Mar-17 17:21pm    
well I can't see what your UpdateCalories does, but if you don't have a method called updatecalories i'd say that's your problem. Also your code should be written to be more maintainable. Where you have
foods.Add(food[0]);
foods.Add(food[1]);
foods.Add(food[2]);
what happens when there are 50 food types? are you going to write 50 lines of code and recompile for every food type added?
It should be
for(int i=0; i<food.length;i++){
foods.Add(food[i]);
}
if the above is just a sample thats fine but if you plan on making a real app out of this, I highly recommend the for loop .
imeyal0 31-Mar-17 17:23pm    
updatecalories is there.
yes..It is just a sample.
please help and assist. I also tried to update at different days but the result is the same

public class Foood
       {
           public string Type { get; set; }
           public double Calories { get; set; }

           public Foood(string type, double calories)
           {
               Type = type;
               Calories = calories;
           }
       }

       public class Days
       {
          public List<Foood> foods = new List<Foood>();
       }

       private void testit()
       {
           int types = 3;
           List<Foood> foods = new List<Foood>();
           List<Days> foodDays = new List<Days>();
           int days = 3;


           Foood[] food = new Foood[types];
           food[0] = new Foood("protein", 2.0);
           food[1] = new Foood("vitamins", 3.0);
           food[2] = new Foood("Carbonhydrate", 4.0);

           for (int i = 0; i < food.Length; i++)
           {
               foods.Add(food[i]);
           }

           Days day = new Days();
           day.foods = foods;
           foodDays.Add(day);

           for (int i = 1; i <= days; i++)
           {
               day = new Days();
               day.foods = UpdateCalories(foodDays[i-1].foods);
               foodDays.Add(day);
           }

       }

       public List<Foood> UpdateCalories(List<Foood> foodItem)
       {
           List<Foood> newFood = new List<Foood>();
           Foood item;

           for (int i = 0; i < foodItem.Count; i++)
           {
               item = new Foood(foodItem[i].Type, foodItem[i].Calories + 1.1);
               newFood.Add(item);
           }

           return newFood;
       }
   }
 
Share this answer
 
v2
Comments
imeyal0 1-Apr-17 5:05am    
thanks for your help. This also gives the same result as before. Please i need further help
try this code
C#
using (StreamWriter sw = new StreamWriter(@"U:\Food\food.txt"))
{
	for (int i = 0; i < days; i++)
	{
		sw.WriteLine("Day" + "{0}", i);
		for (int ii = 0; ii < types; ii++)
		{
			sw.WriteLine("{0},{1}", food[ii].Type, food[ii].Calories);
		}
		sw.WriteLine("");
		//	UpdateCalories here
	}
}


[Update]
Another way to see the problem is:
day 0, use good values
day 1, use good values + 1.1
day 2, use good values + 1.1 * 2
day 3, use good values + 1.1 * 3
...
and you don't change food values.
C#
sw.WriteLine("{0},{1}", food[ii].Type, food[ii].Calories + 1.1 * i);
 
Share this answer
 
v4
Comments
imeyal0 1-Apr-17 5:03am    
thanks for your help. I really need the calories for all the days before i get to the stage of printing out the output.
imeyal0 1-Apr-17 10:03am    
I think the assistance i need is to be able to work with a duplicate Object from one day to the next without altering/changing the changing Original object

imeyal0 1-Apr-17 13:15pm    
please kindly assist - the Designer View for form 1 in CSharp Visual Studio 2015 is missing. It only appears when i click the start button. Please how do i bring it back in order to edit the form.
I think it got missing whilst copying from one VS version to the other and using different computers.
Patrice T 1-Apr-17 13:49pm    
I gave you help on things I know.
I don't use VS
imeyal0 1-Apr-17 16:06pm    
Many thanks. I really appreciate.

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

  Print Answers RSS


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