类初始化错误

时间:2012-12-14 04:12:19

标签: c# xna

我正在使用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)
    {

    }
}

这是我遇到问题的地方,当运行Initialize Method时出现错误:

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)的最佳重载方法匹配有一些无效的参数。

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

1 个答案:

答案 0 :(得分: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参数 - 最好从Initlialize()方法中删除ref关键字:

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