Posts

Showing posts with the label Constructor

CA1062: ValidateArgumentsOfPublicMethods On Co-constructor Calls

Answer : Like this? public MyClass(SomeOtherClass source) : this(source, source == null ? null : source.Name) { } public MyClass(SomeOtherClass source, string name) { /* ... */ }

Can Kotlin Data Class Have More Than One Constructor?

Answer : A Kotlin data class must have a primary constructor that defines at least one member. Other than that, you can add secondary constructors as explained in Classes and Inheritance - Secondary Constructors. For your class, and example secondary constructor: data class User(val name: String, val age: Int) { constructor(name: String): this(name, -1) { ... } } Notice that the secondary constructor must delegate to the primary constructor in its definition. Although many things common to secondary constructors can be solved by having default values for the parameters. In the case above, you could simplify to: data class User(val name: String, val age: Int = -1) If calling these from Java, you should read the Java interop - Java calling Kotlin documentation on how to generate overloads, and maybe sometimes the NoArg Compiler Plugin for other special cases. Yes, but each variable should be initialized, so you may set default arguments in your data class constructo...

Class Constructor Type In Typescript?

Answer : Solution from typescript interfaces reference: interface ClockConstructor { new (hour: number, minute: number): ClockInterface; } interface ClockInterface { tick(); } function createClock(ctor: ClockConstructor, hour: number, minute: number): ClockInterface { return new ctor(hour, minute); } class DigitalClock implements ClockInterface { constructor(h: number, m: number) { } tick() { console.log("beep beep"); } } class AnalogClock implements ClockInterface { constructor(h: number, m: number) { } tick() { console.log("tick tock"); } } let digital = createClock(DigitalClock, 12, 17); let analog = createClock(AnalogClock, 7, 32); So the previous example becomes: interface AnimalConstructor { new (): Animal; } class Animal { constructor() { console.log("Animal"); } } class Penguin extends Animal { constructor() { super(); console.log("Penguin...

Async Constructor Functions In TypeScript?

Answer : A constructor must return an instance of the class it 'constructs'. Therefore, it's not possible to return Promise<...> and await for it. You can: Make your public setup async . Do not call it from the constructor. Call it whenever you want to 'finalize' object construction. async function run() { let topic; debug("new TopicsModel"); try { topic = new TopicsModel(); await topic.setup(); } catch (err) { debug("err", err); } } Readiness design pattern Don't put the object in a promise, put a promise in the object. Readiness is a property of the object. So make it a property of the object. The awaitable initialise method described in the accepted answer has a serious limitation. Using await means only one block of code can wait on the action. This is fine for code with guaranteed linear execution but in multi-threaded or event driven code it's unte...

Calling A Method In A Javascript Constructor And Accessing Its Variables

Answer : Yes, it is possible, when your constructor function executes, the this value has already the [[Prototype]] internal property pointing to the ValidateFields.prototype object. Now, by looking at the your edit, the errArray variable is not available in the scope of the CreateErrorList method, since it is bound only to the scope of the constructor itself. If you need to keep this variable private and only allow the CreateErrorList method to access it, you can define it as a privileged method , within the constructor: function ValidateFields(pFormID){ var aForm = document.getElementById(pFormID); var errArray = []; this.CreateErrorList = function (formstatid){ // errArray is available here }; //... this.CreateErrorList(); } Note that the method, since it's bound to this , will not be shared and it will exist physically on all object instances of ValidateFields . Another option, if you don't mind to have the errArray variable, as a pub...