initialization

Object initialization in C# (.Net)

Is there a difference (performance, overhead) between these two ways of initializing an object: MyTypedDataSet aDataSet = new MyTypedDataSet(); aDataSet .Merge(anotherDataSet); aDataSet .Merge(yetAnotherDataSet); and MyTypedDataSet aDataSet = anotherDataSet; aDataSet .Merge(yetAnotherDataSet); Which do you recommend? ...

Initializing an array on arbitrary starting index in c#

Is it possible in c# to initialize an array in, for example, subindex 1? I'm working with Office interop, and every property is an object array that starts in 1 (I assume it was originally programed in VB.NET), and you cannot modify it, you have to set the entire array for it to accept the changes. As a workaround I am cloning the orig...

Collection initialization syntax in VB 2008?

I'm trying to determine if there's a way in Visual Basic 2008 (Express edition if that matters) to do inline collection initialization, a la JavaScript or Python: Dim oMapping As Dictionary(Of Integer, String) = {{1,"First"}, {2, "Second"}} I know VB2008 supports array initialization like this, but I can't seem to get it to work for co...

What does `$hash{$key} |= {}` do in Perl?

I was wrestling with some Perl that uses hash references. In the end it turned out that my problem was the line: $myhash{$key} |= {}; That is, "assign $myhash{$key} a reference to an empty hash, unless it already has a value". Dereferencing this and trying to use it as a hash reference, however, resulted in interpreter errors about ...

initialize a const array in a class initializer in C++

I have the following class in C++: class a { const int b[2]; // other stuff follows // and here's the constructor a(void); } The question is, how do I initialize b in the initialization list, given that I can't initialize it inside the body of the function of the constructor, because b is const? This doesn't work: a...

Initializing C# auto-properties

I'm used to writing classes like this: public class foo { private string mBar = "bar"; public string Bar { get { return mBar; } set { mBar = value; } } //... other methods, no constructor ... } Converting Bar to an auto-property seems convenient and concise, but how can I retain the initialization without adding a cons...

Initializing private static members

This feels like a dumb question, but what is the best way to initialize a private, static data member in C++? I tried this but it gives me weird linker errors: class foo { private: static int i; }; int foo::i = 0; I'm guessing this is because I can't initialize a private member from outside the class. So what's the best...

How to initialise a rather complex char array in C?

Assuming Visual C/C++ 6, I have a complex data structure of 22399 elements that looks like this: { { "(SAME", "AS", "U+4E18)", "HILLOCK", "OR", "MOUND"}, { "TO", "LICK;", {1, 1, 0}, "TASTE,", "A", "MAT,", "BAMBOO", "BARK"}, { "(J)", "NON-STANDARD", "FORM", "OF", "U+559C", ",", {1, 1, 0}, "LIKE,", "LOVE,", "ENJOY;", {1, 1, 4}, "JOYFUL", ...

How to initialize an array in C

I have a large array in C (not C++ if that makes a difference). I want to initialize all members to the same value. I could swear I once knew a simple way to do this. I could use memset() in my case, but isn't there a way to do this that is built right into the C syntax? ...

Difference initializing static variable inline or in static constructor in C#

I would like to know what is the difference between initializing a static member inline as in: class Foo { private static Bar bar_ = new Bar(); } or initializing it inside the static constructor as in: class Foo { static Foo() { bar_ = new Bar(); } private static Bar bar_; } ...

Can initialization list in constructors be used in template classes?

I find that most books concerning C++ templates don't tell anything about whether it's possible or not to use initialization list in constructor of a template class. For example, I have code like this: template <class T> class Stack { T* data; std::size_t count; std::size_t capacity; enum {INIT = 5}; public: Stack()...

C# error: Use of unassigned local variable

I'm not sure why I'm getting this error, but shouldn't this code compile, since I'm already checking to see if queue is getting initialized? public static void Main(String[] args) { Byte maxSize; Queue queue; if(args.Length != 0) { if(Byte.TryParse(args[0], out maxSize)) queue = new Queue(){MaxSize ...

Initializing a class using superclass initializer

Hello! I have got two classes, one a subclass of the other (say Animal and Dog). The superclass has got some initializers (say initAnimal), the subclass has some initializers (say initDog). The problem is that it is perfecly legal (from the compiler’s viewpoint) to do something like Dog *adog = [[Dog alloc] initAnimal], ie. initialize a ...

How do you declare arrays in a c++ header?

This is related to some other questions, such as: this, and some of my other questions. In this question, and others, we see we can declare and initialise string arrays in one nice step, for example: const char* const list[] = {"zip", "zam", "bam"}; //from other question This can be done in the implementation of a function with no bo...

Memory Usage on convenience method vs init method

Recently when I looked into iPhone memory management, I tried to compare the convenience method and init method on the same object. For example, I have UIImageView where it displays a downloaded NSData: Convenience method: imageView.image = [UIImage imageWithData:[downloads dataAtIndex:0]]; init method: UIImage *aImage = [[UIImage a...

Declarations, definitions, initializations in C, C++, C#, Java and Python

What do the terms mean in each of the above languages? Why do the languages differ (wherever they do, if at all they do) in this respect? ...

Initialising Java Web App

I have a simple web app, with a few jsp pages, servlets and pojo's. I want to initialise the connection pool before any requests are made. What is the best way to do this? Can it be done when the app is first deployed or do you have to wait till the first request comes in? ...

Python - Create a list with initial capacity

Code like this often happens: l = [] while foo: #baz l.append(bar) #qux This is really slow if you're about to append thousands of elements to your list, as the list will have to constantly be re-initialized to grow. (I understand that lists aren't just wrappers around some array-type-thing, but something more complicated....

Why do I need to cast self to id?

I have an init method that takes an (id) argument: -(id) initWithObject:(id) obj; I'm trying to call it like this: [[MyClass alloc] initWithObject:self]; But XCode is complaining about the argument being a "distinct Objective-C type" (which usually indicates a type mismatch or level of indirection error). If I explicitly...

Is it possible to initialise a New System.Collections.Generic.Dictionary with String key/value pairs?

Is it possible to create and initialise a System.Collections.Generic.Dictionary object with String key/value pairs in one statement? I'm thinking along the lines of the constructor for an array of Strings.. e.g. Private mStringArray As String() = {"String1", "String2", "etc"} In case this is turns out to be a syntactic sugar kind of...