标签: initializer

C# - 字符串问题中的字符串?

我不确定这里究竟是什么问题.我正在使用2个字符串并且我一直在收到错误"字段初始化程序无法引用非静态字段,方法或属性'Captcha.Capture.CaptureTime'".

这是代码中的一个片段:

string CaptureTime = DateTime.Now.Month.ToString() + "-" + DateTime.Now.Day.ToString() + "-" + DateTime.Now.Year.ToString() + "-" + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();


string SaveFormat = Properties.Settings.Default.SaveFolder + "Screenshot (" + CaptureTime + ")." + Properties.Settings.Default.ImageFormat;
Run Code Online (Sandbox Code Playgroud)

我不会详细说明为什么我以这种特殊方式使用字符串.一切正常.我猜它与另一个字符串中的字符串有关?这可能是完全明显的,但我真的不知道.有任何想法吗?

c# string field initializer

2
推荐指数
1
解决办法
279
查看次数

无法访问枚举初始化程序中的静态字段

在这段代码中我得到一个编译器错误,请参阅注释:

 public enum Type {
   CHANGESET("changeset"),
   NEW_TICKET("newticket"),
   TICKET_CHANGED("editedticket"),
   CLOSED_TICKET("closedticket");

   private static final Map<String, Type> tracNameMap = new HashMap<String, Type>();

   private Type(String name) {
    tracNameMap.put(name, this); // cannot refer to static field within an initializer
   }

   public static Type getByTracName(String tn) {
    return tracNameMap.get(tracNameMap);
   }

  }
Run Code Online (Sandbox Code Playgroud)

有没有办法让这个工作,从Map其中一个字段获取枚举值?

java enums static initializer

2
推荐指数
2
解决办法
5078
查看次数

Rake任务和rails初始化器

有点Rails的新手,所以请对付我.我现在正在做的是后台处理一些Ruby代码使用Resque.为了启动Rescque rake任务,我一直在使用(在heroku上),我有一个resque.rake文件,其中推荐的代码附加到heroku的神奇(或奇怪)线程架构中:

require "resque/tasks"
require 'resque_scheduler/tasks'

task "resque:setup" => :environment do
  ENV['QUEUE'] = '*'
end


desc "Alias for resque:work (To run workers on Heroku)"
task "jobs:work" => "resque:work"
Run Code Online (Sandbox Code Playgroud)

由于我需要访问Rails代码,因此我引用:environment.如果我在heroku的后台设置至少1个工作dyno,我的Resque做得很好,被清除,一切都很开心.直到我尝试自动化东西......

所以我想要进化代码并每分钟左右自动填充相关任务的队列.这样做(不使用cron,因为heroku不适合cron),我声明了一个名为task_scheduler.rb的初始化程序,它使用Rufus调度程序来运行任务:

scheduler = Rufus::Scheduler.start_new

scheduler.in '5s' do
  autoprocessor_method
end

scheduler.every '1m' do
  autoprocessor_method
end
Run Code Online (Sandbox Code Playgroud)

事情看起来有点令人敬畏......然后rake进程就不再无法解决地从队列中恢复过来了.队列越来越大.即使我有多个工作人员dynos在运行,他们最终都会感到疲倦并停止处理队列.我不确定我做错了什么,但我怀疑在我的rake任务中引用Rails环境导致task_scheduler.rb代码再次运行,导致重复调度.我想知道如果有人知道如何解决这个问题,我也很好奇,如果这是rake任务停止工作的原因.

谢谢

rake ruby-on-rails initializer rufus-scheduler

2
推荐指数
1
解决办法
4058
查看次数

gcc中的"初始化元素不是常量"错误

我尝试使用make文件编译C代码.我收到以下错误:

/home/dev5/src/ermparselex.c:69: error: initializer element is not constant
/home/dev5/src/ermparselex.c:69: error: (near initialization for âyyinâ)
Run Code Online (Sandbox Code Playgroud)

代码段和行号:

65 int yyleng; extern char yytext[];
 66 int yymorfg;
 67 extern char *yysptr, yysbuf[];
 68 int yytchar;
 69 FILE *yyin = stdin, *yyout = stdout;
 70 extern int yylineno;
 71 struct yysvf {
 72         struct yywork *yystoff;
 73         struct yysvf *yyother;
 74         int *yystops;};
 75 struct yysvf *yyestate;
 76 extern struct yysvf yysvec[], *yybgin;
Run Code Online (Sandbox Code Playgroud)

此代码中的任何位置都未定义stdin和的值stdout.我无法从谷歌获得适当的解决方案.知道为什么会出现这个错误吗?

linux gcc initializer

2
推荐指数
1
解决办法
3773
查看次数

从初始化程序创建列表

两者之间有什么区别吗?

var list = new List<UserType>
{
    new UserType(...),
    new UserType(...),
};
Run Code Online (Sandbox Code Playgroud)

var list = new List<UserType>()
{
    new UserType(...),
    new UserType(...),
};
Run Code Online (Sandbox Code Playgroud)

我曾经常常使用第二个认为我只需要调用list的无参数(或任何其他)构造函数...

c# list initializer

2
推荐指数
1
解决办法
56
查看次数

在Swift中初始化UIGestureRecognizer作为属性定义的一部分?

我想初始化UIPanGestureRecognizer的一部分UIViewController的属性定义,这样我就不必声明其可选的(好像只有在发生初始化我会viewDidLoad).

以下两次尝试都在编译时失败(我使用的是最新版本的Xcode):

-- 1st attempt
class TestController: UIViewController {

    let panGestureRecognizer: UIPanGestureRecognizer

    required init(coder: NSCoder) {
        super.init(coder: coder)
        panGestureRecognizer = UIPanGestureRecognizer(  target: self, action: "handlePan:")
        // fails with "Property 'self.panGestureRecognizer' not initialized at super.init call' or
        // fails with "'self' used before super.init call'
        // depending on the order of the two previous statements
    }
}

-- 2st attempt
class TestController: UIViewController {

    let panGestureRecognizer = UIPanGestureRecognizer(target:self, action: "handlePan:")
    // fails with "Type 'TestController …
Run Code Online (Sandbox Code Playgroud)

initializer uigesturerecognizer ios swift

2
推荐指数
1
解决办法
1828
查看次数

为AVAudioRecorder调用初始化程序 - Swift 2.0

这似乎是我向Swift 2.0过渡的一个问题:

我正在尝试初始化我的AVAudioRecorder并且设置参数(我曾经给出nil)将不再接受这个.思考?

*var session = AVAudioSession.sharedInstance()
        do{
            try session.setCategory(AVAudioSessionCategoryPlayAndRecord)
        } catch{
        }
        audioRecorder = AVAudioRecorder(URL: filePath, settings: nil)
        audioRecorder.meteringEnabled = true
        audioRecorder.prepareToRecord()
        audioRecorder.record()*
Run Code Online (Sandbox Code Playgroud)

它给了我错误:**

"无法使用参数列表类型'(URL:NSURL,settings:nil)'调用类型'AVAudioRecorder'的初始值设定项."

**

顺便提一下,filePath的类型是NSURL.谢谢你们每一个人的帮助!

initializer avaudiorecorder swift swift2

2
推荐指数
1
解决办法
765
查看次数

具有初始化程序引用实例成员的自动属性

这是工作代码

public class SomeViewModel
{
    public DelegateCommand CommandA { get; }
    public DelegateCommand CommandB { get; }

    public bool SomeProperty { get; set; }        

    // stupid constructor
    public SomeViewModel()
    {
        CommandA = new DelegateCommand(OnCommandA);
        CommandB = new DelegateCommand(o => SomeProperty = true;);
    }
    void OnCommandA(object obj) { ... }
}
Run Code Online (Sandbox Code Playgroud)

而这一个不是

public class SomeViewModel
{
    public DelegateCommand CommandA { get; } = new DelegateCommand(OnCommandA); // error
    public DelegateCommand CommandB { get; } = new DelegateCommand(o => SomeProperty = true;); // …
Run Code Online (Sandbox Code Playgroud)

c# properties initializer c#-6.0

2
推荐指数
1
解决办法
457
查看次数

在C++中初始化一个向量类成员

我试图设置长度并初始化一个类的向量成员.但似乎唯一可能的是,初始化行是不合适的.你喜欢哪个 ?(谢谢)

//a vector, out of class set size to 5. initilized each value to Zero
vector<double> vec(5,0.0f);//its ok

class Bird{

public:
    int id;
    //attempt to init is not possible if a vector a class of member
    vector<double> vec_(5, 0.0f);//error: expected a type specifier
}
Run Code Online (Sandbox Code Playgroud)

c++ vector initializer

2
推荐指数
2
解决办法
2683
查看次数

为什么在实例初始值设定项内不允许递增(`x ++;`)尚未声明的字段,但是如果将其包装到匿名类中就可以了吗?

我很困惑,为什么x++;不允许在实例初始值设定项内增加尚未声明的字段(),但是如果包装到匿名类中,则允许在实例初始值设定项内进行递增(是的,匿名类可以访问类字段,但未初始化该字段! )。

class Test {

    { x++; }  // ERR: Cannot reference a field before it is defined

    Object anonFld = new Object() {     
        { x++; }     // fine! Sets x field below to 1 !

        void f() {
            x++; // fine!
        }
    };
    int x;

    // now x = 1     // anon constructor has set it!    

}
Run Code Online (Sandbox Code Playgroud)

java constructor field initializer anonymous-class

2
推荐指数
1
解决办法
71
查看次数