C
C#2y ago
TopN

❔ ✅ Prints System.Func`1[System.Int32] instead of actual int.

I'm trying to return a private field member variable and instead of giving me the actual integer value it gives me the thing in the title. Please help.
11 Replies
canton7
canton72y ago
Please share your code
TopN
TopNOP2y ago
This is in program.cs (the default class)
using ObjectAndClasses;

food meat = new food("meat", 4);
food spaghetti = new food("spaghetti",3);

spaghetti.eat();
meat.eat();
Console.WriteLine("\n-----------------------------------------\n");
Console.WriteLine("Press \"1\" to stop eating.");
string UserInput = Console.ReadLine();

if (UserInput.Equals("1"))
{
meat.stop();
spaghetti.stop();
}
else
{
Console.WriteLine("Food eaten indefinitely");
}

meat.info();
spaghetti.info();

Console.WriteLine("The name of my food is " + spaghetti.getName);
using ObjectAndClasses;

food meat = new food("meat", 4);
food spaghetti = new food("spaghetti",3);

spaghetti.eat();
meat.eat();
Console.WriteLine("\n-----------------------------------------\n");
Console.WriteLine("Press \"1\" to stop eating.");
string UserInput = Console.ReadLine();

if (UserInput.Equals("1"))
{
meat.stop();
spaghetti.stop();
}
else
{
Console.WriteLine("Food eaten indefinitely");
}

meat.info();
spaghetti.info();

Console.WriteLine("The name of my food is " + spaghetti.getName);
This is food.cs (the class I created)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;

namespace ObjectAndClasses
{
internal class food
{

//member variables
private string _name;
private int _heal;

//constructor method
public food(string name, int heal = 0)
{
_name = name;
_heal = heal;
Console.WriteLine("\nA new food called "+ _name + " was created!");
}

//member methods
public void eat()
{
Console.WriteLine("\nEating {0}!", _name);
Console.WriteLine("\nIt healed {0} healthpoint(s)!", _heal);
}

public void stop()
{
Console.WriteLine("\nStopped eating {0}!",_name);
}

public void info()
{
Console.WriteLine("\nThe food {0} heals {1} hp.",_name,_heal);
}

public string getName()
{
return _name;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;

namespace ObjectAndClasses
{
internal class food
{

//member variables
private string _name;
private int _heal;

//constructor method
public food(string name, int heal = 0)
{
_name = name;
_heal = heal;
Console.WriteLine("\nA new food called "+ _name + " was created!");
}

//member methods
public void eat()
{
Console.WriteLine("\nEating {0}!", _name);
Console.WriteLine("\nIt healed {0} healthpoint(s)!", _heal);
}

public void stop()
{
Console.WriteLine("\nStopped eating {0}!",_name);
}

public void info()
{
Console.WriteLine("\nThe food {0} heals {1} hp.",_name,_heal);
}

public string getName()
{
return _name;
}
}
}
and even with the name my problem is that it returns a func something string i changed it a bit to test it with the name
canton7
canton72y ago
Note that your output is "My food meat heals...", but the code has "The food meat heals...". Are you sure you're running your latest code?
TopN
TopNOP2y ago
TopN
TopNOP2y ago
yeah i know i changed it a bit This is still what it gives out with a string now for the last code i gave u
canton7
canton72y ago
That's not helpful. Please make sure that the code and error you give us actually match up
TopN
TopNOP2y ago
Alright Here, let me give you the previous code i used for the picture Program.cs
canton7
canton72y ago
Anyway, getName it a method. You need to call it: getName()
TopN
TopNOP2y ago
Yeah thats why im dumb i got confused because it seemed to be working It didnt give me an error but because i didnt call it it just gave me what type it returned my bad im stupid thanks
canton7
canton72y ago
👍
Accord
Accord2y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server