Initializers are called to create a new instance of a particular type. In its simplest form, an initializer is like an instance method with no parameters
Questions tagged [initializer]
684 questions
15
votes
5 answers
Declare a List and populate with values using one code statement
I have following code
var list = new List();
list.Add(new MyCustomTypeOne());
list.Add(new MyCustomTypeTwo());
list.Add(new MyCustomTypeThree());
this of course works, but I'm wondering: how can I declare the list and populate it…

user1765862
- 13,635
- 28
- 115
- 220
15
votes
4 answers
Java double brace initialization works always?
I know that this code:
Set set = new HashSet() {{
add("test1");
add("test2");
}};
is really:
Set set = new HashSet() {
{//initializer
add("test1");
add("test2");
}
};
The initializer block is being…

user926958
- 9,355
- 7
- 28
- 33
14
votes
1 answer
Taking address of temporary (compound literal) parameter in C
I can't imagine this isn't already duplicate, but I can't easily find the answer since the more complex scenarios specifically to C++ seem to dominate the discussion0.
Is it legal to take take the address of a temporary constructed in the parameter…

BeeOnRope
- 60,350
- 16
- 207
- 386
14
votes
4 answers
Creating an array of two-dimensional arrays in C#
I simply want to create an array of two dimensional arrays to store coordinate points.
So I want an array where each index returns a two dimensional array which I would use as x and y.
Here's what I've tried:
waypoints = new int[4][,] {
…

MysticPing
- 183
- 2
- 2
- 7
14
votes
4 answers
How to create initializer to create and migrate mysql database?
I have been learning how to use EF for a week or so now and am stuck on the issue of creating/updating my database. I am able to create an initializer to create the database if it is not there:
static class Program
{
static void Main()
{
…

Matt
- 1,328
- 1
- 16
- 28
13
votes
3 answers
Rails 3.1 Deployment to Heroku Error
I'm trying to deploy my app to Heroku, I've done this before on my Windows machine, and now I am currently using a mac.
I'm trying to use Postgresql for the first time.
I have the following in my Gemfile:
gem 'pg'
EDIT:
AndrewDavis-OSX:lunchbox…

ardavis
- 9,842
- 12
- 58
- 112
13
votes
1 answer
auto with parentheses and initialiser list
Coming up from another question:
Since C++17, auto x0{1, 2, 3, 4};, previously deducing an initialiser list, is not allowed any more (sure, we can use auto x0 = {1, 2, 3, 4}; instead...). Now as always avoiding uniform initialisation (e. g.…

Aconcagua
- 24,880
- 4
- 34
- 59
13
votes
2 answers
Swift: "failable initializer 'init()' cannot override a non-failable initializer" vs. default parameters
If I declare
public class A: NSObject {
public class X { }
public init?(x: X? = nil) { }
}
all is fine. When using it like let a = A(), the initializer is called as expected.
Now, I'd like to have the nested class X private, and the…

Stefan
- 1,036
- 10
- 32
13
votes
2 answers
Designated Initializer of UITextView
When I create a new subclass of UITextView in the Xcode 6 Beta, the following code is automatically provided.
import UIKit
class TerminalView: UITextView {
init(frame: CGRect) {
super.init(frame: frame)
// Initialization code
…

Brian Tracy
- 6,801
- 2
- 33
- 48
13
votes
3 answers
Static Initializers vs Instance Initializers vs Constructors
I am studying for an exam about Java. While I was studying, I have encountered syntaxes in java which are unfamiliar to me. Such as a curly braces({}) unside a class body without a name, some has a static keyword. I have found out that they are…

steven0529
- 1,513
- 1
- 18
- 28
13
votes
2 answers
Calling a class method upon creation of Python classes
I'd like to automatically run some code upon class creation that can call other class methods. I have not found a way of doing so from within the class declaration itself and end up creating a @classmethod called __clsinit__ and call it from the…

Simon
- 133
- 1
- 5
12
votes
3 answers
Does Rails initializers gets called when I run rails console
I want to put my chargify conf inside the initializer,
but I found the initializer won't execute in my rails c, is there a way to invoke my initializers so I can test in my console?
Chargify.configure do |c|
c.api_key = "keykey"
…

user1883793
- 4,011
- 11
- 36
- 65
12
votes
2 answers
What causes this initializer inheritance issue with UIBarButtonItem?
I'm trying to create a simple Swift subclass of UIBarButtonItem:
class LabelBarButtonItem: UIBarButtonItem {
let label: UILabel
init(text: String) {
self.label = UILabel(frame: CGRectZero)
self.label.tintColor = UIColor.grayColor()
…

Bill
- 44,502
- 24
- 122
- 213
12
votes
3 answers
Rails initializers are running while migrating database
It is very surprising that Rails's initializers run while running any rake task, including db:migrate and db:seed.
An initializer in my app starts a background thread (a kind of worker process), and it should be executed only when the application is…

Paul
- 25,812
- 38
- 124
- 247
12
votes
3 answers
C# dictionary initializer compilation inconsistency
The following code compiles, but fails with a NullReferenceException:
class Test
{
public Dictionary Dictionary { get; set; }
}
static void Main(string[] args)
{
var x = new Test
{
Dictionary = // fails
…

Ben M
- 22,262
- 3
- 67
- 71