3

我正在使用 Visual Studio C# Express 和 Xna 4.0

好吧,我是一名业余程序员,我可能不是这样做的最好方法,所以如果你想向我展示一个更好的方法并解决我的问题,那就太好了。

我的问题是我在运行此代码时遇到了一个我不明白的错误。

这是我正在使用的课程,问题不在这里,但这很重要:

class ShipType
{
    public Vector2 Size;
    public int LogSlots;
    public int DefSlots;
    public int OffSlots;
    public int SpecSlots;

    public int HullBase;
    public int TechCapacity;
    public int EnergyCapacity;
    public int WeightBase;
    public string Class;
    public string Manufacturer;
    public string Description;

    public void Initialize(
        ref Vector2 Size,
        ref int LogSlots, ref int DefSlots, ref int OffSlots, 
        ref int SpecSlots, ref int HullBase, ref int TechCapacity, 
        ref int EnergyCapacity, ref int WeightBase,
        ref string Class, ref string Manufacturer, ref string Description)
    {

    }
}

这是我遇到问题的地方,运行初始化方法时出现错误:

class AOSEngine
{
    public Player PlayerA = new Player();
    public List<ShipType> ShipTypeList = new List<ShipType>(10);

    public void Initialize()
    {
        //Build Ship Type List
        ShipTypeList = new List<ShipType>(10);
        ShipTypeList.Add(new ShipType());
        ShipTypeList[0].Initialize(new Vector2(0, 0), 4, 4, 4, 4, 
                                   100, 100, 100, 10, "Cruiser", 
                                   "SpeedLight", "");

    }
}

运行 Initialize Line 时出现错误,如下:

最佳重载方法匹配AOS.ShipType.Initialize(ref Vector2 Size, ... ref string Description)有一些无效参数。

再一次,我可能没有做得很好,所以如果你有任何做得更好的建议,我很想听听。

4

1 回答 1

2

由于您已将参数声明为ref,因此在传入变量时需要使用ref关键字:

ShipTypeList[0].Initialize(ref new Vector2(0, 0),ref 4,ref 4,ref 4,ref 4,ref 100,ref 100,ref 100,ref 10,ref "Cruiser",ref "SpeedLight",ref "");

也就是说,这里没有充分的理由使用 ref 参数 - 你最好ref从 Initlialize() 方法中删除关键字:

public void Initialize(
     Vector2 Size,
     int LogSlots,  int DefSlots,  int OffSlots,  int SpecSlots,
     int HullBase,  int TechCapacity,  int EnergyCapacity,  int WeightBase,
     string Class,  string Manufacturer,  string Description)
{
    this.LogSlots = LogSlots;
    this.DefSlots = DefSlots;
    //...etc...
}

有关为什么以及如何使用 ref 的详细信息,请参阅 Jon Skeets 文章:http ://www.yoda.arachsys.com/csharp/parameters.html

于 2012-12-14T04:15:56.370 回答