Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The problem is that I am a beginner and my program displays System.String[] instead of the ingredients . I tried many codes but it did not solve my problem so I can not understand why this is happening and what I'm doing wrong. Thank you very much!

Below is my code:

What I have tried:

<pre>public class Recipe
{
    public string Name { get; set; }
    public string[] Ingredients { get; set; }
    public string Description { get; set; }
    public FoodCategory FoodCategory { get; set; }
    public int MaxNumOfIngredients { get; private set; }

  
    public int CurrentNumOfIngredients => Ingredients.Where(i => i != null).Count();

   
    private int VacantPos => Array.FindIndex(Ingredients, i => i == null);

   
    public Recipe(int max)
    {
        MaxNumOfIngredients = max;
        Ingredients = new String[max];
    }

    
    public void DefaultValues()
    {
        for (int i = 0; i < Ingredients.Length; i++)
            Ingredients[i] = null;

        Name = string.Empty;
        Description = string.Empty;
        FoodCategory = FoodCategory.Meat;
    }

   
    /// Adds ingredients to recipe as long as MaxNumOfIngredients isn't reached
    
    public bool AddIngredient(params string[] ingredients)
    {

        foreach (var ingredient in ingredients)
        {
            if (CurrentNumOfIngredients >= MaxNumOfIngredients || (string.IsNullOrWhiteSpace(ingredient)))
                return false;
            if (!IngredientExists(ingredient))
                Ingredients[VacantPos] = ingredient;
         
        }
        return true;
    }



  public override string ToString()
    {
        var chars = Math.Min(Description.Length, 15);
        var descriptionText = Description.Substring(0, chars);
        if (string.IsNullOrEmpty(descriptionText))
            descriptionText = "No Description";

        return $"{Name,-20} {Ingredients ,30} {FoodCategory.ToString(),30} 
  {CurrentNumOfIngredients,50} ";
    }
Posted
Updated 27-Feb-21 22:54pm

The Ingredients variable is an array, whose default ToString method is based on the object.ToString method, which just prints its type. You will have to create the code to print out the individual items of the array. Use a StringBuilder Class (System.Text) | Microsoft Docs[^] to create a printable string of all the ingredients. You can then copy the first 30 characters (or any other amount) into your final printable string.
 
Share this answer
 
you should change
C#
Ingredients

in
C#
return $"{Name,-20} {Ingredients ,30} {FoodCategory.ToString(),30}
 {CurrentNumOfIngredients,50} ";

to
C#
string.Join(",", Ingredients)

and it will work as you want.

Added remark: I think Richards solution below is the better one, so this might be a point to start:

C#
private string GetIngredients()
{
   var sb = new System.Text.StringBuilder();
   for (int i = 0; i < Ingredients.Length; i++ )
   {
     if (!string.IsNullOrEmpty(Ingredients[i]))
     {
        sb.Append($"{Ingredients[i]}|");
     }
   }
   // check sb.Length() if above 30 if you want
   return sb.ToString();
}
 
Share this answer
 
v3
Comments
Rash Abs 28-Feb-21 5:35am    
thank for your help but it returns ,,,,,,,,,,,,,,,,,,, only
any advice ?
FranzBe 28-Feb-21 5:42am    
please see remark above

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