2012-09-02 71 views
1

非常简单的问题,我刚刚忘记了正确的编码。我设置了一个空白,并且我希望它在单击按钮时运行。激活按钮上的无效按

虚空我想执行:

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 

    } 
+0

'giveWeapon'与'button14_Click'属于同一类吗? –

+0

它在同一班,是 – Matt

+1

为什么你很难打电话给它? – codingbiz

回答

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

请注意,使用implicitly typed local variables将使您的代码可读性受益匪浅:

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); 
} 
+0

为了显示错误,我编写了这样的代码,如果你给我一个你的意思的例子,我愿意给它一个镜头 – Matt

1

只需进入:

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

只要giveWeapon是与button14相同的类,那么它将工作。

希望这会有所帮助!

1

然后调用它

private void button14_Click(object sender, EventArgs e) 
{ 

    giveWeapon(10, "Armoured Tank"); 
} 
+0

谢谢!我会尽快接受这个答案 – Matt