constructor

Exception throwable constructors & initialization. Best practice

I have DataProvider class (DAL) that requires "year" as parameter. It used like this: using (var provider = new DataProvider(year)) { provider.SomeRepostitory.DoSomethingUsefull(); } DataProvider constructor code deals with configuration - so it can throw exceptions. And exception throwable constructors are not recommended. So I a...

Objective-C Class Constructors

In my Objective-C project I have a Country class (NSManagedObject subclass). When comparing a country to another its important for me to know the total world population. I'm doing this operation a lot and it's getting expensive. I decided to put the population in a static variable of Country. However I want to initialize this value befor...

Parameterless Constructors

In C#, is there way to enforce that a class MUST have a parameterless constructor? ...

Doubt based on program in SCJP(EXAM 310-065)

class Top{ public Top(String s){System.out.print("B");} } public class Bottom2 extends Top{ public Bottom2(String s){System.out.print("D");} public static void main(String args[]){ new Bottom2("C"); System.out.println(" "); } } In the above program, I guessed the output must be BD, but in the book they said the...

Doubt based on program in SCJP(EXAM 310-065)

class Building{ Building(){ System.out.print("b "); } Building(String name){ this(); System.out.print("bn "+name); } } public class House extends Building{ House(){ System.out.print("h "); } House(String name){ this(); System.out.print("hn "+name); } publ...

If my class has a property with a private field for storage, should i use the private field or the property to initialize it in the constructor?

Which of the following two is better? Why? Ex. 1: public class TestClass { public TestClass(CoolClass cool) { this.Cool = cool; } CoolClass _cool public CoolClass Cool { get { return _cool; } set { _cool = value; } } } Ex. 2:...

C# Default optional parameter chooser

public ClassType(string type) { Type = type; } public ClassType(string type,bool isArray=false) { Type = type; IsArray = isArray; } ClassType ct = new ClassType("adsf"); Which constructor is chosen? ...

class to class conversion using constructor+inheritance in C++

Hello Every one. Here I can do conversion from emp class object to emp class object. But I can't do conversion from employee class object to emp class object - I have added comment where I am getting error - 'setEmpID' is not a member of 'employee'. what should I do to resolve this error ? ( I am just preparing for C++ exam & this is t...

difference between initializing on declaration and on object construction

Possible Duplicate: Best Practice: Initialize class fields in constructor or at declaration? Is there any difference between these two classes? public class Test { public Guid objectId = Guid.NewGuid(); } public class Test2 { public Guid objectId; public Test2() { objectId = Guid.NewGuid(); } } ...

Constructor constraint on generic types or simply check for constraint within my generic type constructor?

I have a generic class DirectorySource<T> which depends on an interface IDirectorySearch<T>. Both are generics and have the same type constraint: public class DirectorySource<T> where T : IDirectoryEntry { } public interface IDirectorySearcher<T> where T : IDirectoryEntry { } So, for instance, when I want a source to manipulate grou...

Java: Create static class member whose constructor could throw an exception

I have a static setter that is used to set all instances of MyClass: public class MyClass { .... protected static final Setter setter = new Setter(); ... } However this does not compile since the setter constructor throws an exception: public class Setter { public Setter() throws FileNotFoundException { ...

a way to omit taking names for objects that are used only to construct a final object

Suppose we have following two classes: class Temp{ public: char a; char b; }; class Final{ private: int a; char b; char c; public: Final(Temp in):b(in.a),c(in.b){} //rest of implementation }; suppose the only use of objects of Temp class is to construct objects of Final class, so I wonder if in current standard of c++...

Echo Return construct method;

<?php class DBFactory { function __construct(){ return 'Need to echo'; } } $db = new DBFactory; echo $db; ?> Not works :( ...

using c# reflection to call a constructor

i have the following senario: class Addition{ public Addition(int a){ a=5; } public static int add(int a,int b) {return a+b; } } i am calling add in another class by: string s="add"; typeof(Addition).GetMethod(s).Invoke(null, new object[] {10,12}) //this returns 22 i need a way similar to the above reflection statement to create ...

When returning an object from a JavaScript constructor function (avoiding 'new') how do I implement public members?

I have begun writing my 'class' type JavaScript functions like the Module Pattern or Revealing Module patten. This avoids the use of 'new' and allows me to define which functions are public in a single place. However, I have been unable to see how to use and access public data members in this pattern. e.g. the member 'id' below is ac...

will be this initialization syntax valid in upcoming c++0x standard ?

Suppose we have following two classes: class Temp{ public: char a; char b; }; class Final{ private: int a; char b; char c; public: Final(Temp in):b(in.a),c(in.b){} //rest of implementation }; can we initialize an object of the Final class with following syntax in upcoming c++0x standard: Final obj(Temp{'a','b'}); ...

C++ - Superclass constructor producing an instance of a subclass

Hi. I have two classes, one of which is a subclass of another, and differs only by the fact that it contains an additional member variable to its parent. I am not using the default constructor, passing in a reference to a single object as the constructors parameter. What I would like is for the constructor of the parent to examine this ...

How can I tell the inheriting class to not call its base class' parameter-less constructor?

I was surprised to find out that the parameter-less constructor of my base class is called any time I call any constructor in a derived class. I thought that is what : base() was for, to explicitly call the base constructor if and when I want to. How can I prevent the base constructor from being called when I instantiate a derived cla...

How to make a constructor return a subclassed object.

I just read a book on object-oriented programming patterns. It describes a Factory pattern by which you can make a call to a static factory method of an object and the object will return a new object of expected type, but it will be subclassed appropriately. My question, can this functionality be provided by a constructor for the cla...

Abstract class' constructor asks for a return type

I don't understand what's happening here, I've copied this code from another project (which compiled without problems), but as soon as I got it into my own, I got a compiler error on the constructor definition saying the method needs a return type. public abstract class BaseSqlRepository<T, InterfaceT, PrimaryKeyT> where T : cla...