initializer

Is there any way to use an extension method in an object initializer block in C#

The simple demo below captures what I am trying to do. In the real program, I have to use the object initialiser block since it is reading a list in a LINQ to SQl select expression, and there is a value that that I want to read off the database and store on the object, but the object doesn't have a simple property that I can set for that...

Dependency on fixture data in rails initializer

I have an initializer which sets a default that is used throughout the app. The value is an ActiveRecord model, I'm essentially caching it for the lifetime of the app: @@default_region = Region.find_by_uri("whistler") The record is guaranteed to be in the database: it's fixture data which is referenced by other models. This works fine...

Is it possible to use a c# object initializer with a factory method?

I have a class with a static factory method on it. I want to call the factory to retrieve an instance of the class, and then do additional initialization, preferablly via c# object initializer syntax : MyClass instance = MyClass.FactoryCreate() { someProperty = someValue; } vs MyClass instance = MyClass.FactoryCreate(); instance....

What's the difference between an object initializer and a constructor?

What are the differences between the two and when would use an "object initializer" over a "constructor" and vice-versa? I'm working with C#, if that matters. Also, is the object initializer method specific to C# or .NET? ...

Ruby on Rails: Error when running a rake task from initializer file

I have the file config/initializers/custom.rb In the file, there is only one line: `rake thinking_sphinx:start` I thought this was supposed to just execute the line like when typing it from a command line. With this line of code, when I run "ruby script/server", the server freezes and outputs no error messages. Am I missing somethi...

Use of Initializers vs Constructors in Java

So I've been brushing up on my Java skills as of late and have found a few bits of functionality that I didn't know about previously. Static and Instance Initializers are two such techniques. My question is when would one use an initializer instead of including the code in a constructor? I've thought of a couple obvious possibilities: ...

Debugging a C# Object Initializer

Does anyone have any tips for debugging exceptions in a C# object initializer block? The object initializer syntax is basically all or nothing, which can make it especially difficult to troubleshoot inside of a LINQ query. Short of breaking the object creation out to a separate method, is there anything I can do to see which property set...

Static constructor equivalent in Objective-C?

I'm new to Objective C and I haven't been able to find out if there is the equivalent of a static constructor in the language, that is a static method in a class that will automatically be called before the first instance of such class is instantiated. Or do I need to call the Initialization code myself? Thanks ...

TCPL 5.9.9 (C++): Where would it make sense to use a name in its own initializer?

This is a question from the most recent version of Stroustrup's "The C++ Programming Language". I've been mulling this over in my head for the past couple days. The only thing I can come up with, (and this is probably incorrect) is something like this: int* f(int n) { int* a = &a - n * sizeof(int*); return a; } My intent is to g...

static constructors in C++? need to initialize private static objects

I want to have a class with a private static data member (a vector that contains all the characters a-z). In java or C#, I can just make a "static constructor" that will run before I make any instances of the class, and sets up the static data members of the class. It only gets run once (as the variables are read only and only need to be...

Environment-specific intializers for rails?

Can you configure rails to only run an initializer under certain environments? In my case I had to hack paperclip to work with Imagemagick on my dev box, so I have monkeypatched code I want only to apply to the development environ, not the production environment. That monkeypatch is saved as a file in config\initializers. The guides.rub...

C# dictionary initializer compilation inconsistency

The following code compiles, but fails with a NullReferenceException: class Test { public Dictionary<string, string> Dictionary { get; set; } } static void Main(string[] args) { var x = new Test { Dictionary = // fails { { "key", "value" }, { "key2", "value2" } } }; } If you repla...

C# - String within a string issue?

I am not sure what exactly the issue is here. I am working with 2 strings and I keeping getting the error "A field initializer cannot reference the non-static field, method, or property 'Captcha.Capture.CaptureTime'". Here's a snippet from the code: string CaptureTime = DateTime.Now.Month.ToString() + "-" + DateTime.Now.Day.ToString()...

Can I create an anonymous, brace-initialized aggregate in C++?

One can create an anonymous object that is initialized through constructor parameters, such as in the return statement, below. struct S { S(int i_, int j_) : i(i_), j(j_) { } int i, j; }; S f() { return S(52, 100); } int main() { cout << f().i << endl; return 0; } However, can one similarly create an anonymous aggregate th...

C++: newbie initializer list question

Hi, Newbie here. I am looking at company code. It appears that there are NO member variables in class A yet in A's constructor it initializes an object B even though class A does not contain any member variable of type B (or any member variable at all!). I guess I don't understand it enough to even ask a question...so what's goin...

How to handle a static final field initializer that throws checked exception

Hello all, I am facing a use case where I would like to declare a static finalfield with an initializer statement that is declared to throw a checked exception. Typically, it'd look like this: public static final ObjectName OBJECT_NAME = new ObjectName("foo:type=bar"); The issue I have here is that the `ObjectName` constructor may thr...

C++ constructor and an initializer list

does C++ allow to initialize struct using initializer list, {}, if struct has a defined constructor? could not find an answer, but g++ does not seem to allow it. struct r { int a; }; struct s { int a; s() : a(0) {} }; r = { 1 }; // works s = { 1 }; // does not work ...

How can I access the Rails logger from an initializer?

Following the advice from my previous question, I placed my background process in an initializer named scheduler.rb. However, I'm having a hard time getting the newly-scheduled processes to log to the Rails logs. Is there a simple way for me to access the same logs from the initializer, preferably by accessing Rails' default logger metho...

C++: constructor initializer for arrays

I'm having a brain cramp... how do I initialize an array of objects properly in C++? non-array example: struct Foo { Foo(int x) { /* ... */ } }; struct Bar { Foo foo; Bar() : foo(4) {} }; array example: struct Foo { Foo(int x) { /* ... */ } }; struct Baz { Foo foo[3]; // ??? I know the following syntax is...

Are Objective-C initializers allowed to share the same name?

I'm running into an odd issue in Objective-C when I have two classes using initializers of the same name, but differently-typed arguments. For example, let's say I create classes A and B: A.h: #import <Cocoa/Cocoa.h> @interface A : NSObject { } - (id)initWithNum:(float)theNum; @end A.m: #import "A.h" @implementation A - (id)ini...