-1

I have an Error when I'm running the code, instead of the array i get System.string[] can you help me?

this is the class (I removed some parts that wasn't necessarily such as the other constructors)

class food
    {
        public int amount;
        public double weight;
        public bool stock;
        public string[] name = new string[3];

>this is the constructors

        public void set(int amount, double weight, bool stock, string[] name)
        {
            this.amount = amount;
            this.weight = weight;
            this.stock = stock;
            this.name = name;
        
        public string[] doName()
        {
            return name;
        }
    }

this is the main

    class MainClass
    {
        public static void Main(string[] args)
        {
            food food1 = new food();
            string[] name = new string[3];
            int amount = 0;
            double weight = 0.0;
            bool stock = false;

>I think the problem might be here but i'm not sure..

            Console.Write("What Kind Of Food? ");
            food1.name[0] = Console.ReadLine(); > i think the problem is somewhere here...
            food1.set(amount, weight, stock, name);
            Console.WriteLine(food1.doName());
        }
    }
Bob
  • 9
  • 1
    See [printing all contents of array in C#](https://stackoverflow.com/q/16265247) – greenjaed Feb 18 '21 at 18:02
  • `set` is not the constructor. In C# a constructor has the same name as the class or struct and has no return type: `public food(int amount, double weight, bool stock, string[] name)` – Olivier Jacot-Descombes Feb 18 '21 at 18:10
  • Your `food1.doName()` method returns a string array. You can pass a string or an object to `Console.WriteLine`. When it gets passed an object, the virtual method ToString gets called on the object to convert it to a string. The default implementation simply returns the name of the type of the object. In this case, it's a string array, or `System.string[]` – Flydog57 Feb 18 '21 at 18:19

2 Answers2

0

I am not sure what you are doing, but your code looks very non-standard.

Try something like this.

public class Food {
    public int Amount{get;set;}
    public double Weight{get;set;}
    public bool Stock{get;set;}
    public string[] Name {get;set;}

    public Food(int amount, double weight, bool stock, string[] name){
        Amount = amount;
        Weight = weight;
        Stock = stock;
        Name = name;
    }
}

And if you want Name property just ask for it via Getter. like

Var name = Food.Name;

Also you seems to miss how constructor works. It creates object in some defined state. So when you have all parameters in constructor why you use food food1 = new food(); Your code just creates blank object food and additional properties which are not part of your object. Your syntax should be

public static void Main(string[] args)
{
    food food1 = new food();
    food1.name = new string[3];
    food1.amount = 0;
    food1.weight = 0.0;
    food1.stock = false;
}

OR

public static void Main(string[] args)
{
    food food1 = new food(0,0.0,false,new string[3]);
}

OR default constructor using setters

public static void Main(string[] args)
{
    food food1 = new food(){Amount = 0,Weight = 0.0,Stock = false,Name = new String[3]};
}
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
  • Even when the array is initialized correctly, printing `Console.WriteLine(food1.name);` will not produce the expected result. An array always prints as the array type. – Olivier Jacot-Descombes Feb 18 '21 at 18:17
0

When Console.WriteLine() is executed on an object, the object ToString() method is executed.

This method on an array doesn't return the contained values.

If you want to get a string with all values, you have to use a function to flatten them, for example:

Console.WriteLine(string.Join(", ", food1.doName()));
Sledgy
  • 21
  • 2