这可能是一个愚蠢的问题,但我如何与其他方法分享变量ammount和price?
[Command("sellweed")]
public void sellWeed(Client sender, Client target, int ammount, int price)
{
API.sendChatMessageToPlayer(sender, "~b~You have offered " + ammount +
" Weed to " + target + "for $" + price + ", wait for him to accept.");
API.sendChatMessageToPlayer(target, "~b~" + sender + " has offered you " + ammount +
" Weed for a price of $" + price + ", type /acceptweed to buy.");
}
[Command("acceptweed")]
public void acceptWeed(Client player)
{
checkPlayerName = API.getPlayerName(player);
string checkMoney = "SELECT Wallet FROM [playerInfo] WHERE PlayerName='" +
checkPlayerName + "'";
con.Open();
SqlCommand checkMoneyCMD = new SqlCommand(checkMoney, con);
int playerMoney = Convert.ToInt32(checkMoneyCMD.ExecuteScalar());
con.Close();
if (playerMoney < price)
{
API.sendChatMessageToPlayer(player, "You don't have enough money.");
}
else
API.sendChatMessageToPlayer(player, "You received " + ammount + " of weed.");
}
答案 0 :(得分:0)
请确保您远离警察执行此业务。 如果不在托管环境中完成,则可能导致严重的违规操作异常。
答案 1 :(得分:0)
通常,值通过参数列表从一个方法传递到另一个方法。就像您当前将Client player
传递给acceptWeed
方法一样,您也可以将price
和amount
传递给该方法。
您没有显示调用此方法的位置,但可能是来自某个知道price
和amount
的地方。
此外,您应该养成使用参数化查询的习惯。您正在使用的当前语法可能会让您对SQL注入攻击持开放态度。我也把它包含在答案中。您可以阅读更多相关信息HERE。
[Command("acceptweed")]
public void acceptWeed(Client player, int amount, int price)
{
checkPlayerName = API.getPlayerName(player);
string checkMoney = "SELECT Wallet FROM [playerInfo] WHERE PlayerName = @playerName";
using (SqlConnection con = new SqlConnection(/* connection info */))
using (SqlCommand command = new SqlCommand(checkMoney, con))
{
var playerName = new SqlParameter("playerName", SqlDbType.NVarChar);
playerName.Value = API.getPlayerName(player);
command.Parameters.Add(playerName);
int playerMoney = Convert.ToInt32(checkMoneyCMD.ExecuteScalar());
}
if (playerMoney < price)
{
// It might be nice to tell the player how much money they have
API.sendChatMessageToPlayer(player, "You don't have enough money.");
}
else
{
// Do something to subtract 'price' from 'playerMoney'
// Also do something to subtract 'amount' from the seller's stash
// And do something to add 'amount' to the buyer's stash
API.sendChatMessageToPlayer(player, "You received " + amount + " of weed.");
}
}
答案 2 :(得分:-1)
你需要做一些吸气剂 ps lol ammount确实是拼写金额。
将此函数放入带变量的函数中。
public int getAmmount(){
return ammount;
}
public int getPrice(){
return price;
}
然后在您要从中检索值的函数中使用:
var amount = sellweed.getAmmount();
这将变量金额设置为amweed的价值。 getPrice也是如此。
var price = sellweed.getPrice();