WolfRiderGames
WolfRiderGames

Reputation: 33

Class Initialization Error

I'm Using Visual Studio C# Express and Xna 4.0

Alright, I'm an amateur programmer and I'm probably not doing this the best way, so If you would like to show me a better way and fix my problem that would be great.

My problem is I'm getting an error I do not understand when I run this code.

This is the class I'm using, the problem isn't here but this is important:

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)
    {

    }
}

Here is where I get the problem, When running the Initialize Method I get an error:

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", "");

    }
}

The error occurs when running the Initialize Line and is as follows:

The Best Overloaded Method Match for AOS.ShipType.Initialize(ref Vector2 Size, ... ref string Description) has some invalid arguments.

Once again, I probably didn't do this very well, so if you have any suggestions for doing it better I would like to hear it.

Upvotes: 3

Views: 144

Answers (1)

Greg Sansom
Greg Sansom

Reputation: 20810

Since you have declared your parameters as ref, you need to use the ref keyword when you pass in the variables:

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 "");

That said, there is no good reason to use ref parameters here - you're better off removing the ref keyword from the Initlialize() method:

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...
}

For detail about why and how to use ref, see Jon Skeets article: http://www.yoda.arachsys.com/csharp/parameters.html

Upvotes: 2

Related Questions