【问题标题】:Passing a bool in c#在 C# 中传递一个布尔值
【发布时间】:2021-07-19 02:30:53
【问题描述】:

嘿,我正在制作一个不和谐的机器人,我正在尝试设置它,以便通过命令将布尔值设置为真/假(它将进一步启动不同的响应)但是当我这样做时,它保持不变false 尽管它被设置为 true。我相当确定它是因为没有通过变量。在这种情况下我将如何去做? bool 被称为 (opse)

namespace ConsoleApp5
{
    
    

    public class Commands : ModuleBase<SocketCommandContext>
    {
        bool opse; 
        [Command("opsset")]
        public async Task trueset(string op)
        {
            
            if (op == "true")
            {
                opse = false;
                Console.WriteLine("Operations is set to active! Set by " + 
                Context.Message.Author.Username);
            }
            if (op == "false")
            {
                opse = true;
                Console.WriteLine("Operations is set to inactive! Set by " + 
                Context.Message.Author.Username);
            }


        }
        [Command("operations")]
        public async Task ops()
        {
             if (opse = true)
            {
                await Context.Channel.SendMessageAsync("Operations are not currently active. Check your 
                designated schedule to see when operations are active");
            }
            if (opse = false)
            {
                await Context.Channel.SendMessageAsync("Operations are currently active. Message you SO 
                or file an absent report");
            }
        }


       
    }
    
}

【问题讨论】:

    标签: c# discord boolean bots


    【解决方案1】:

    您使用单个= 来检查opse 的值,这是不正确的。单个= 用于分配新值。

    要比较值,您应该使用==

    if (opse == true)
    {
      ...
    }
    if (opse == false)
    {
      ...
    }
    

    【讨论】:

      猜你喜欢
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-13
      相关资源
      最近更新 更多