2012-09-02 70 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