Posts

Showing posts with the label Constructor

Constructors In Kotlin

Answer : Well init is not body of constructor. It is called after primary constructor with the context of primary constructor. As given in Official documentation: The primary constructor cannot contain any code. Initialization code can be placed in initializer blocks, which are prefixed with the init keyword: class Customer(name: String) { init { logger.info("Customer initialized with value ${name}") } } Note that parameters of the primary constructor can be used in the initializer blocks. They can also be used in property initializers declared in the class body: class Customer(name: String) { val customerKey = name.toUpperCase() } In fact, for declaring properties and initializing them from the primary constructor, Kotlin has a concise syntax: class Person(val firstName: String, val lastName: String, var age: Int) { // ... } As per your question you can add a constructor to accept one parameter like following: class Person(name: String, surn...

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) { /* ... */ }

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...

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...