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

관련 문제