please bear with me as I’m just learning C#. Just messing around with C# I decided to come up with an inventory system to test out but I have one problem in my script:
using System;
using System.Collections.Generic;
public class Item
{
public String name;
public int pesos;
public int getPesos()
{
return pesos;
}
public String getName()
{
return name;
}
}
public class statuseffect
{
statuseffect(string Effect,int Amount,int Duration)
{
string effect = Effect;
int amount = Amount;
int duration = Duration;
}
}
public class Potion : Item
{
public int hpeffect;
public int mpeffect;
List<statuseffect> effects = new List<statuseffect>();
public Potion(int hp,int mp)
{
hpeffect = hp;
mpeffect = mp;
}
public void addEffect(statuseffect eff)
{
effects.Add(eff);
}
}
class game
{
public static void Main()
{
Potion healthPotion = new Potion(200,50);
healthPotion.pesos = 23;
Console.WriteLine(healthPotion.hpeffect);
statuseffect slow = new statuseffect("slow",10,30);
}
}
in the last line the compiler tells me that statuseffect does not contain a constructor that takes 3 arguments. From what I can tell, it does contain 3 arguments. Is there something I am missing here?
as a side note. if you guys have any comments or suggestions for my script, that would be helpful as well.
Your constructor is
privateand therefore “invisible” to code outside the class itself. Try adding the keywordinternalbefore the constructor. Or, if it needs to be visible from other projects as well, addpublicinstead.Another issue: In your class
statuseffect, you declare three local variable inside your constructor. Those variable’s only scope is the constructor. You must move their declarations out of the constructor (then they become instance fields of the class). The constructor can still assign to them.