1

非常直截了当的问题,我刚刚忘记了正确的编码。我有一个 void 设置,我希望它在我单击按钮时运行。

无效我想执行:

    public void giveWeapon(int clientIndex, string weaponName)
    {

        uint guns = getWeaponId(weaponName);

        XDRPCExecutionOptions options = new XDRPCExecutionOptions(XDRPCMode.Title, 0x822728F8);  //Updated
        XDRPCArgumentInfo<uint> info = new XDRPCArgumentInfo<uint>(getPlayerState(clientIndex));
        XDRPCArgumentInfo<uint> info2 = new XDRPCArgumentInfo<uint>((uint)guns);
        XDRPCArgumentInfo<uint> info3 = new XDRPCArgumentInfo<uint>((uint)0);
        uint errorCode = xbCon.ExecuteRPC<uint>(options, new XDRPCArgumentInfo[] { info, info2, info3 });
        iprintln("gave weapon: " + (guns.ToString()));
        giveAmmo(clientIndex, guns);
        //switchToWeapon(clientIndex, 46);

    }

我只希望它在单击按钮时运行:

    private void button14_Click(object sender, EventArgs e)
    {
     // Call void here

    }
4

3 回答 3

3

void是一个关键字,表示您的函数 giveWeapon不返回值。所以你的正确问题是:“我怎样才能调用一个函数?”

答案:

private void button14_Click(object sender, EventArgs e)
{
    int clientIndex = 5; // use correct value
    string weaponName = "Bazooka"; // use correct value
    giveWeapon(clientIndex, weaponName);
}

如果giveWeapon在不同的类中定义,则需要创建一个实例并在该实例上调用该方法,即:

ContainingClass instance = new ContainingClass();
instance.giveWeapon(clientIndex, weaponName);

作为旁注,您的代码可读性将从使用隐式类型的局部变量中受益匪浅:

public void giveWeapon(int clientIndex, string weaponName)
{
    uint guns = getWeaponId(weaponName);

    var options = new XDRPCExecutionOptions(XDRPCMode.Title, 0x822728F8);  //Updated
    var info = new XDRPCArgumentInfo<uint>(getPlayerState(clientIndex));
    var info2 = new XDRPCArgumentInfo<uint>(guns); // guns is already uint, why cast?
    var info3 = new XDRPCArgumentInfo<uint>(0); // same goes for 0
    uint errorCode = xbCon.ExecuteRPC<uint>(options, new XDRPCArgumentInfo[] { info, info2, info3 });
    iprintln("gave weapon: " + guns); // ToString is redundant
    giveAmmo(clientIndex, guns);
    //switchToWeapon(clientIndex, 46);
}
于 2012-09-02T04:31:42.460 回答
1

简单地去:

private void button14_Click(object sender, EventArgs e)
{
    giveWeapon(clientIndex, weaponName);
}

只要giveWeapon是在同一个班级,button14那么它就可以工作。

希望这可以帮助!

于 2012-09-02T04:29:40.723 回答
1

然后调用它

private void button14_Click(object sender, EventArgs e)
{

   giveWeapon(10, "Armoured Tank");
}
于 2012-09-02T04:30:43.297 回答