【问题标题】:Make Trigger Bool variable [closed]使触发器布尔变量[关闭]
【发布时间】:2014-02-01 22:54:07
【问题描述】:

是否有任何简单的算法可以在方法中首次运行(例如在这个例子中我们称之为 example())

    private bool trigger
    private int timer=0;

    public void main()
    {
       for (int i =0;i<5;i++){

          if (trigger==false)
               Console.WriteLine("loading...");
          else
               Console.WriteLine("hello world");
          example() 
       }
    }
public void example() 
   {
      if (timer==0)
           {
             //my loading data , like define value to variable or any thing else
             trigger = true ; // define a example variable trigger;
           }
        else
            {
            // after loading data do it at runtime
            trigger = false ;
            }
        timer++;

   }

【问题讨论】:

  • 这里要问什么?什么是“处理中的空白”?
  • 你可以这样写:trigger = timer++ == 0; 但它不会解决任何问题,真的。
  • 对不起,我不检查我的类型 - ( Void main ) 当 Void main 正在处理时 - 我想要触发器工作,当 Void main 关闭时,我的触发器被重置(非常感谢)

标签: c# triggers boolean


【解决方案1】:

在类中使用静态变量

public class trigger
{


      static bool isTriggered = false;
      static int timer = 0;

      public static void main()
      {//not a console app-- main used cause op used it

          if (timer==0) isTriggered=true;               
          else istriggered=false; 

          timer++;//happens no matter what

      }
}

这可能对事件有意义...让我们试试吧!

public class trigger
{          
      static int timer = 0;          

       //events
      public static event EventHandler<TriggerEventArgs> Trigger;
      static void onTrigger(object sender, TriggerEventArgs e){if(Trigger != null)Trigger(sender,e);}

      public static void main()// <--- no args?
      {//not a console app-- main used cause op used it


          if (timer==0)onTrigger(new object(),new TriggerEventArgs(/*stuff could go here*/));

          /*
          else
             istriggered=false; 
          */

          timer++;//happens no matter what
          //increment your life away

      }
}

public class TriggerEventArgs : EventArgs
{

  //what ever you could possibly need and more 
  //TODO: add constructor so /*stuff can go here*/

}

然后在你的项目中使用初始化函数,比如

void initializefunc()
{
     trigger.Trigger += subscription;
}

void subscription(object sender, TriggerEventArgs e)
{
    //meaningful code
}

或者我想你可以重载 ++ 运算符(完整代码示例)

using System;

namespace ConsoleApplication1
{
class Program
{
    static void Main(string[] args)
    {//the real console app

        //test the strange trigger class
        trigger.Trigger += trigger_Trigger;
        trigger t = new trigger();
        trigger a = new trigger();

        for (int x = 0; x <= 9; x++)
        {
            t++;
            a++;
        }
        Console.ReadLine();
    }

    static void trigger_Trigger(object sender, TriggerEventArgs e)
    {
        Console.WriteLine("once");
    }
}

public class trigger
{
    static int timer = 0;

    //standard event pattern  (static style)
    public static event EventHandler<TriggerEventArgs> Trigger;
    static void onTrigger(object sender, TriggerEventArgs e) { if (Trigger != null)Trigger    (sender, e); }


    //overload the ++ operator
    public static trigger operator ++ (trigger t)
    {
        trigger.ops_main();//yeah.. i know this is kinda strange
        //but so is the question
        return t;
    }



    public static void ops_main()// <--- no args?
    {//not a console app-- main used cause op used it


        if (timer == 0) onTrigger(new object(), new TriggerEventArgs());

        /*
        else
           istriggered=false; 
        */

        timer++;//happens no matter what
        //increment your life away

    }
}

public class TriggerEventArgs : EventArgs
{

  //what ever you could possibly need and more 

}   


}

【讨论】:

  • 我知道这不值得。但我得到了启发。
  • 非常感谢 - 你帮了我很多(你教了我所有的课程技巧,我找到了一种干净的方法来制作触发器)你的代码运行良好(但我改变了一点>:D
  • 随时。 -- 现在去睡吧。
【解决方案2】:

你在做什么似乎有点奇怪,但简单的答案是将你的 0 和 1 更改为 true 和 false

private bool trigger
private int timer;

public void main()
{
    if (timer==0)
        trigger=true;
    else
        trigger=false;
    timer++;
}

【讨论】:

  • 对于大型编程项目(我编写了一个战略性 AI 并经常使用它)不是一个好方法是变量类型(如 Bool 或.. 之类的 int)可以做到这一点(代码很小)跨度>
  • 那么,如果我误解了这个问题,我很抱歉,你想要达到的目标不是很清楚。
  • 感谢克里斯,我很抱歉提出不好的问题-我认为为它开设课程是解决这个问题的唯一方法:-? (顺便说一下坦克)我想投票赞成你的帮助,但我的观点非常不乐观)度过了愉快的时光
猜你喜欢
  • 1970-01-01
  • 2017-03-13
  • 1970-01-01
  • 2021-12-25
  • 2012-06-14
  • 1970-01-01
  • 2015-08-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多