initialization

creating a object in javascript that has a 'init' method with optional name/value pairs

I've seen many javascript objects that have a 'init' method that you pass in values to in order to setup the object. How do they internally handle the initializations of their private variables when passed in a array of name/value pairs like: myObject.init( {prop1: "blah", prop2: "asdf", ..., propn: "n"} ); Specifically, some ...

C/C++ private array initialization in the header file

I have a class called Cal and it's .cpp and .h counterpart Headerfile has class Cal { private: int wa[2][2]; public: void do_cal(); }; .cpp file has #include "Cal.h" void Cal::do_cal() { print(wa) // where print just itterates and prints the elements in wa } My question is how do I initialize the array ...

Build OnetwoThree linked list with minimum assignment operators

I came across this problem while preparing for an interview and curious to know the diffrent ways it can be written. I found this at http://cslibrary.stanford.edu/103/ and have given the problem as it is. here is a code to build the list {1,2,3} struct node* BuildOneTwoThree() { struct node* head = NULL; struct node* second = N...

Default constructors and inheritance in Java

Hello, I have a question about default constructors and inheritance in Java. Generally, if you write a class and do not include any constructor, Java provides automatically for you a default constructor (one without parameters), which initializes all instance variables of the class (if there are any) with some default values (0, null,...

Initialising an instance variable with a method from the class (Java)

Hello, can I initialize an instance variable in Java, when I initialise it when I declare it, and initialise it with the return value of a method, which I define later in the class. Something like this: public class MyClass { integers[] myArray = new integers[length()]; int length() { .... } } length() gives me...

C# Equivalent of Java anonymous inner classes with init blocks

In Java, i like to use constructs such as List<String> list = new ArrayList<String>() {{add("foo");}}; Is there a way to do this in 1 line in C#, too? ...

InitializeComponent in both constructors, or in one with constructor inheritance?

Is there any practical difference in terms of effects on the component model between: class MyComponent : Component { public MyComponent() { InitializeComponent(); } public MyComponent(IContainer container) { container.Add(this); InitializeComponent(); } } and: class MyComponent : Component { ...

Should I keep instance variables in Java always initialized or not?

I recently started a new project and I'm trying to keep my instance variables always initialized to some value, so none of them is at any time null. Small example below: public class ItemManager { ItemMaster itemMaster; List<ItemComponentManager> components; ItemManager() { itemMaster = new ItemMaster(); components = new...

Array of pointers problem

I have tried this example of array of pointers. I am getting the error "Illegal initialisation in function main" int main() { int a[]={1,2,3,4,5}; int b[]={1,2,3,4,5}; int c[]={1,2,3,4,5}; int *p[3]={a,b,c}; int i; clrscr(); for(i=0;i<3;i++) printf("%d - %u\n",*p[i],p[i]); getch(); } If I use stati...

Initialize variables from stored procedure results

Hello Group, I am still fairly green in SQL, and I have a question. I am creating a lookup using reporting services that finds an account based on lat/long. My question is how can I get results from a stored procedure to initialize variables in the following sp? Example: --This sp will go out and get the minlat, maxlat, minlong, maxlo...

Silverlight control in XAML is null at runtime

I've created a silverlight app with a DeepEarth map control on it. Here is my base XAML <Grid x:Name="LayoutRoot" > <!-- Map Control --> <DeepEarth:Map x:Name="map" Margin="0,0,0,0"> <Controls:CoordControl VerticalAlignment="Bottom" HorizontalAlignment="Right" /> <Controls:ScaleControl VerticalAlignment="Bottom" HorizontalAlignmen...

Default value of a property

Hi, If you have an object like this: public class Foo { private int _id; public int id { get { return _id; } set { _id = value; } } private string _name; public string name { get { return _name; } set { _name = value; } } } And you want to initialize id to -1 and name to String.Empty what is the best practice? (an...

What are the best practices for determining the tasks of Constructor, Initialization and Reset methods

This is a general OOP question although I am designing in Java. I'm not trying to solve a particular problem, just to think through some design principles. From my experience I have reached the habit segregating object setup into three phases. The goal is to minimize: extra work, obfuscated code and crippled extensibility. Construction...

How to initialize an NSObject's subclass on iPhone?

I want to write some methods in a class so that other classes can call these methods using [instance methodName:Parameter]. If the class is a subclass of UIViewController, I can use initWithNibName to initialize it. But I want to write the methods in an NSObject's subclass, how can I initialize it? ...

What is the best way to initialize a bitfield struct in C++?

In C++, I have a class which contains an anonymous bitfield struct. I want to initialize it to zero without having to manually write out all fields. I can imagine putting the initialization in three places: Create a constructor in the bitfield Zero out in the initializer list of the constructor for the containing class Zero out in th...

Assigning C strings

Hello. I’ve got the following code: #include <iostream> using namespace std; int main() { char* a = "foo"; char* b = "bar"; a = b; cout << a << ", " << b << endl; return 0; } This compiles and works, ie. prints bar, bar. Now I would like to demonstrate that what goes on here is not copying a string. I would like to...

How to use a class for xml related logic taking xml file "uri" as initializer?

Hi , i have a project in which i created two classes TreeDisplay(Form.cs) and MytreeNode class in same namespace. TreeDisplay class contains all the GUI related stuff like Browse button textbox,label,progress bar and TReeView. I want the user to select a XML file through browse button which will be displayed in textbox.The Xml file sele...

How to check if an element in array exists in Java

If I have an int array structured like this: private int[][] map = new int[400][400]; And I try to retrieve map[100][200] And that element isn't initialized, will i get a compiler/runtime error or will it return null? And is there any function to check if a given element/index exists/has been set? ...

Javascript - object initializer?

I've realized you can have a property in an object run automatically like this: var obj = { init:(function(){ alert('loaded');})(); } I'm trying to use this method as an initializer for the object. The problem I'm running into is passing a reference to 'obj' to the init property. I suspect it generates errors because the obj has...

How to initialize nested structures in C++?

Hi, I have created a couple of different structures in a program. I now have a structure with nested structures however I cannot work out how to initalize them correctly.The structures are listed below. Btw, thanks in advance for reading my post or posting an answer :-) : /***POINT STRUCTURE***/ struct Point{ float x; //x ...